6

我正在尝试从我的 android 平板电脑向 POS 打印机发送命令。我已经能够使基本连接正常工作,但是现在当我尝试将数据发送到打印机时,bulkTransfer 返回-1。请帮助我了解发生了什么。以下是从我进行所有数据传输的 android 站点获取的修改后的广播接收器。

private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (ACTION_USB_PERMISSION.equals(action)) {
            synchronized (this) {
                UsbDevice device = (UsbDevice)intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);

                if (intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
                    if(device != null){
                      //call method to set up device communication

                        if(device.getVendorId() != 1659)
                            return;

                        UsbInterface intf = device.getInterface(0);
                        UsbEndpoint endpoint = intf.getEndpoint(0);
                        int direction = endpoint.getDirection();                            
                        UsbDeviceConnection connection = mUsbManager.openDevice(device);
                        connection.claimInterface(intf, forceClaim);

                        bytes = new byte[]{27, 64};

                        //**This returns -1 always**
                        int ret = connection.bulkTransfer(endpoint, bytes, bytes.length, TIMEOUT); 

                        if(ret < 0)
                        {
                            Log.d("WTF", "Error happened!");
                        }
                        else if(ret == 0)
                        {
                            Log.d("WTF", "No data transferred!");
                        }
                        else
                        {
                            Log.d("WTF", "success!");
                        }
                   }
                } 
                else {
                    Log.d(TAG, "permission denied for device " + device);
                }
            }
        }
    }
};
4

0 回答 0