1

卡住了一个问题。尝试通过 USB 打印机 (HP 1108) 从我的 android 设备打印。检测到打印机,但是当我尝试打印时,打印机没有响应。请为任何解决方案提供帮助。

这是我的代码

@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();
    }
}
4

0 回答 0