检测到USB打印机,但未打印

时间:2015-06-02 05:55:01

标签: android android-usb

遇到问题。尝试通过USB打印机从我的Android设备打印(HP 1108)。检测到打印机,但是当我尝试打印时,打印机没有响应。请帮助解决任何问题。

这是我的代码

@SuppressLint("NewApi")
public void printMessage(Context context,String msg) {
    final String printdata = msg;
    final UsbEndpoint mEndpointBulkOut;
    if (usbManager.hasPermission(printer)){
        UsbInterface intf = printer.getInterface(0);
        for (int i = 0; i < intf.getEndpointCount(); i++) {
            UsbEndpoint ep = intf.getEndpoint(i);
            if (ep.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK) {
                if (ep.getDirection() == UsbConstants.USB_DIR_OUT) {
                    mEndpointBulkOut = ep;
                    connection = usbManager.openDevice(printer);
                    if(connection!=null) {
                        makeToast("Device connected");
                    }
                    boolean forceClaim = true;
                    connection.claimInterface(intf, forceClaim );
                    final Handler handler = new Handler();
                    new Thread(new Runnable() {
                        @Override
                        public void run() {
                            byte[] bytes = printdata.getBytes();
                            final int b = connection.bulkTransfer(mEndpointBulkOut, bytes, bytes.length, 100000);
                            handler.post(new Runnable() {
                                @Override
                                public void run() {
                                }
                            });
                        }
                    }).start();

                    connection.releaseInterface(intf);
                    break;
                }
            }
        }
    } else {
        usbManager.requestPermission(printer, mPermissionIntent);
        Toast.makeText(context, "Device have no permission", Toast.LENGTH_SHORT).show();
    }
}

0 个答案:

没有答案