I have an issue with getting an USBInterface from an USBDevice when the device is plugged in in an USB hub with more than one device.
When I connect the USB device directly via OTG or via a hub which is connected via OTG everything works fine but as soon as I plug in an extra device in the hub I'm not able to open a connection.
I retrieve all usb device drivers and iterate trough them until i find the device i want to communicate with.
The following code Snippet contains my method for retrieving the usb devices:
String manufacturer = "MY_MANUFACTURER" // Dummy text for this snippet
int interfaceCount; // Number of USB interfaces
UsbManager manager = (UsbManager) getSystemService(Context.USB_SERVICE);
List<UsbSerialDriver> drivers UsbSerialProber.getDefaultProber().findAllDriver(manager);
for (UsbSerialDriver serialDriver : drivers) {
// Check for my specific USB device
if (serialDriver.getDevice().getManufacturerName().equals(manufacturer)) {
interfaceCount = driver.getInterfaceCount();
}
}
When my device is the only device in the hub the number of interfaces is 2, but as soon as i add an other device the count is 0 and the system throws an ArrayIndexOutOfBoundException on getInterfaceCount as soon as i try to open a connection.
Additional Info: I use the usb-serial-for-android library as a wrapper for communicating with the device. It's either an arduino (for testing) or an FTDI chip.
We are struggeling with the same problem. It seems to be a bug in Android 5 (at least for us, you did not mention your android version). You can find the bug at
https://code.google.com/p/android/issues/detail?id=159529
or
https://www.reddit.com/r/androiddev/comments/37v2c0/usbdevice_getinterface_seems_to_be_broken_in_5x/
or
https://code.google.com/p/android/issues/detail?id=159897
Be aware that it works with Android Versions < 5 and also with Android 6.0 again. So up or downgrading Android might be a solution.
regards,
Daniel