6

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);
                }
            }
        }
    }
};
nakiya
  • 14,063
  • 21
  • 79
  • 118
  • 1
    Also looking for a solution like this, did you ever figure this out? – Michael Sep 23 '13 at 21:26
  • duplicate of http://stackoverflow.com/questions/15446413/usbconnection-bulktransfer-returns-1-what-does-it-mean – ihm Dec 11 '13 at 00:05
  • When printing to a thermal printer, I get this message when the device is unable to accept more data into its buffer, as is the case when printing cannot continue because the paper door is not fastened properly. – CJBS Mar 21 '16 at 17:02

0 Answers0