Saturday, 10 August 2013

Android bulkTransfer to USB HID

Android bulkTransfer to USB HID

I've got USB HID device which is connected to my Android tablet. I have to
receive some information from HID and then send reply. Here is my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//................
mPermissionIntent = PendingIntent.getBroadcast(this, 0, new
Intent(ACTION_USB_PERMISSION), 0);
filter = new IntentFilter(ACTION_USB_PERMISSION);
mManager = (UsbManager) getSystemService(Context.USB_SERVICE);
deviceList = mManager.getDeviceList();
deviceIterator = deviceList.values().iterator();
if(deviceIterator.hasNext())
{
device = deviceIterator.next();
mManager.requestPermission(device, mPermissionIntent);
}
}
public void onConnectToDevice(View v)
{
intf = device.getInterface(0);
connection = mManager.openDevice(device);
if(connection!=null)
{
connection.claimInterface(intf, true);
}
}
public void onRunThread(View v)
{
Thread usbThread = new Thread (null, doBackgroundThreadProcessing,
"USBTHREAD");
usbThread.start();
}
private Runnable doBackgroundThreadProcessing = new Runnable()
{
public void run() {
backgroundThreadProcessing();
}
};
private void backgroundThreadProcessing()
{
UsbEndpoint endpointIn = null;
UsbEndpoint endpointOut = null;
for(int i = 0; i < intf.getEndpointCount(); i++)
{
endpointIn = intf.getEndpoint(i);
if(endpointIn.getType() == UsbConstants.USB_ENDPOINT_XFER_BULK)
{
if(endpointIn.getDirection() == UsbConstants.USB_DIR_IN)
{
endpointIn = intf.getEndpoint(i);
break;
}
else if(endpointIn.getDirection() == UsbConstants.USB_DIR_OUT)
{
endpointOut = intf.getEndpoint(i);
}
}
}
if(endpointIn != null)
{
byte[] buffer = new byte[64];
while(true)
{
if(connection.bulkTransfer(endpointIn, buffer, 64, 8000) >= 0)
{
//getting data from buffer
// here i'm trying to send reply
// filling outputBuffer
/*if(connection.bulkTransfer(endpointOut, outputBuffer, 5,
8000) >= 0)
{
}
}
}
}
My issue is that when i try to send reply i get some error in bulkTransfer
method.

No comments:

Post a Comment