android usb UsbDeviceConnection.bulkTransfer returns -1

徘徊边缘 提交于 2019-12-07 11:36:43

问题


I am trying to send commands to a POS printer from my android tablet. I have been able to get the basic connections wroking but now when I try to send data to the printer, the bulkTransfer returns -1. Please help me understand what is going on. The following is a modified broadcast receiver taken from the android site where I do all the data transfer.

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);
                }
            }
        }
    }
};

来源:https://stackoverflow.com/questions/17541465/android-usb-usbdeviceconnection-bulktransfer-returns-1

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!