1. 程式人生 > >熱敏列印PrinterInstance

熱敏列印PrinterInstance

public class MainActivity extends AppCompatActivity {
    private static final String ACTION_USB_PERMISSION = "com.android.usb.USB_PERMISSION";
    private PrinterInstance lll;
    private UsbManager manager;
    private int printingStatus;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        UsbDevice usbDevice = showUsbList();
        Log.e("213123","123333333333333");
        if (usbDevice!=null){
            //廣播
                    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
                    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
                    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
                    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
                    manager.requestPermission(usbDevice, pendingIntent);
            //PrinterInstance初始化
            lll = PrinterInstance.getPrinterInstance(this, usbDevice, new Handler() {
                @Override
                public void handleMessage(Message msg) {
                    super.handleMessage(msg);
                    int what = msg.what;
                    Log.d("lll", what + "");
                }

                @Override
                public void dispatchMessage(Message msg) {
                    super.dispatchMessage(msg);
                    int what = msg.what;
//                    Object obj = msg.obj;
//                    String s = obj.toString();
//                    Log.e("shiiii",s);
                    switch (what){
                        case PrinterConstants.Connect.SUCCESS:
                            Toast.makeText(MainActivity.this,"連結成功",Toast.LENGTH_LONG).show();
                            initPrint();
                            break;
                        case PrinterConstants.Connect.FAILED:
                            Toast.makeText(MainActivity.this,"連結失敗",Toast.LENGTH_LONG).show();

                            break;
                        case PrinterConstants.Connect.CLOSED:
                            Toast.makeText(MainActivity.this,"連結關閉",Toast.LENGTH_LONG).show();

                            break;
                        case PrinterConstants.Connect.NODEVICE:

                            break;

                    }
                }
            });

            boolean b = lll.openConnection();
            if (b){
                Log.e("印表機","連結成功");


            }else {
                Log.e("印表機","連線失敗");
            }
        }
    }

    /**
     * 列印的api
     */
    private void initPrint() {
        if (lll==null){
            return;
        }
        lll.initPrinter();
        lll.setFont(0,3,3,0,0);
        lll.setPrinter(PrinterConstants.Command.ALIGN, PrinterConstants.Command.ALIGN_CENTER);
        //獲得印表機的狀態
        int currentStatus = lll.getCurrentStatus();
        switch (currentStatus){
            case 0:  //正常"
                Log.e("再列印","再列印");
                lll.printText("12312312312312312312312312321321\r\n");
//                PrinterConstants.PRotate pRotate = PrinterConstants.PRotate.valueOf("10");
//                lll.print(pRotate,0);
                //切紙
                lll.cutPaper(65, 1);
                //獲得列印是否完成
                printingStatus =lll.getPrintingStatus(30 * 1000);
                break;
            case -1:  //接收資料異常
                break;
            case -2:  //缺紙
                break;
            case -3:  //紙將打盡
                break;
            case -4:  //印表機開蓋
                break;
            case -5:  //傳送資料異常
                break;
        }

        switch (printingStatus){
            case 0:  //列印完成
                Toast.makeText(MainActivity.this,"列印完成",Toast.LENGTH_LONG).show();

                break;
            case -1:  //未知異常
                Toast.makeText(MainActivity.this,"未知異常",Toast.LENGTH_LONG).show();

                break;
            case -2:  //當前印表機正在列印
                Toast.makeText(MainActivity.this,"當前印表機正在列印",Toast.LENGTH_LONG).show();

                break;
            case -3:  //列印未完成,缺紙
                Toast.makeText(MainActivity.this,"列印未完成,缺紙 ",Toast.LENGTH_LONG).show();

                break;
            case -4:  //列印未完成,紙艙蓋開蓋
                Toast.makeText(MainActivity.this," 列印未完成,紙艙蓋開蓋 ",Toast.LENGTH_LONG).show();

                break;
            case -5:  //列印未完成,與印表機通訊失敗
                Toast.makeText(MainActivity.this," 列印未完成,與印表機通訊失敗 ",Toast.LENGTH_LONG).show();

                break;
            case -6:  //資料傳送失敗,通訊異常
                Toast.makeText(MainActivity.this,"  資料傳送失敗,通訊異常 ",Toast.LENGTH_LONG).show();

                break;
            case -7:  //接收資料格式不正確
                Toast.makeText(MainActivity.this,"接收資料格式不正確 ",Toast.LENGTH_LONG).show();

                break;
        }
    }

    /***
     * 獲得USB的api
     * @return
     */
    public UsbDevice showUsbList() {
        manager = (UsbManager) getSystemService(Context.USB_SERVICE);
        HashMap<String, UsbDevice> deviceList = manager.getDeviceList();
        Log.e("集合",deviceList.size()+"");

        UsbDevice vv=null;
        Iterator<UsbDevice> deviceIterator = deviceList.values().iterator();

        StringBuilder sb = new StringBuilder();
        while (deviceIterator.hasNext()) {
            UsbDevice usbDevice = deviceIterator.next();
            sb.append("DeviceName=" + usbDevice.getDeviceName() + "\n");
            sb.append("DeviceId=" + usbDevice.getDeviceId() + "\n");
            sb.append("VendorId=" + usbDevice.getVendorId() + "\n");
            sb.append("ProductId=" + usbDevice.getProductId() + "\n");
            sb.append("DeviceClass=" + usbDevice.getDeviceClass() + "\n");
            int deviceClass = usbDevice.getDeviceClass();
            if (deviceClass == 0) {
                UsbInterface anInterface = usbDevice.getInterface(0);
                int interfaceClass = anInterface.getInterfaceClass();

//                sb.append("device Class 為0-------------\n");
//             sb.append("Interface.describeContents()=" + anInterface.describeContents() + "\n");
//                sb.append   ("Interface.getEndpointCount()=" + anInterface.getEndpointCount() + "\n");
//                sb.append("Interface.getId()=" + anInterface.getId() + "\n");
                //http://blog.csdn.net/u013686019/article/details/50409421
                //http://www.usb.org/developers/defined_class/#BaseClassFFh
                //通過下面的InterfaceClass 來判斷到底是哪一種的,例如7就是印表機,8就是usb的U盤
                sb.append("Interface.getInterfaceClass()=" + anInterface.getInterfaceClass() + "\n");
                Log.e("anInterface","="+anInterface.getInterfaceClass());
                if (anInterface.getInterfaceClass() == 7) {
                    sb.append("此裝置是印表機\n");
                    Log.e("此裝置是印表機","此裝置是印表機");
                    vv=usbDevice;

                    //廣播
//                    PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, new Intent(ACTION_USB_PERMISSION), 0);
//                    IntentFilter filter = new IntentFilter(ACTION_USB_PERMISSION);
//                    filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
//                    filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
//                    manager.requestPermission(usbDevice, pendingIntent);
                    return usbDevice;
                } else if (anInterface.getInterfaceClass() == 8) {
                    sb.append("此裝置是U盤\n");
                    Log.e("此裝置是U盤","此裝置是U盤");
                    vv=null;
                }
//                sb.append("anInterface.getInterfaceProtocol()=" + anInterface.getInterfaceProtocol() + "\n");
//                sb.append("anInterface.getInterfaceSubclass()=" + anInterface.getInterfaceSubclass() + "\n");
//                sb.append("device Class 為0------end-------\n");
            }
//
//            sb.append("DeviceProtocol=" + usbDevice.getDeviceProtocol() + "\n");
//            sb.append("DeviceSubclass=" + usbDevice.getDeviceSubclass() + "\n");
//            sb.append("+++++++++++++++++++++++++++\n");
//            sb.append("                           \n");
        }


        //Log.d("liexingshi",sb.toString());
      //  textView.setText(sb);
        return vv;
    }


}