0
votes

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.

2
How often do you run the code above? You'll have to re-run the code now and then to check for new devices.Frank D.

2 Answers

1
votes

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

0
votes

I have achieved this problem without compiling kernel. As you know when application first created works fine but when attached new devices after application starts gets only last one and disable others. So I have created new activity that returns to mainactivity, but before app goes second activity I had killed all progress at mainactivity then goes to second activity which returns immediately to mainactivity and every thing works like firstly created.

Main Activiy Codes:

int pid = android.os.Process.myPid();
android.os.Process.killProcess(pid);

Bundle temp_bundle = new Bundle();
onSaveInstanceState(temp_bundle);
Intent intentRecreate = new Intent(MainActivity.this,SecondActivity.class);
intent.putExtra("bundle", temp_bundle);
tartActivity(intentRecreate);
finish();

Second Activity Codes:

Intent start;
start = new Intent(FreshStartActivity.this,MainActivity.class);
startActivity(start);
finish();

Thanks to: How to kill a process?