Currently I'm developing an app that requires two hardware to connect to the device via USB. I'm encountering an error where the app can see the two devices but the app can only establish a connection to the last hardware inserted to the USB hub.
The android OS version is 5.1 and I used the UsbManager class.
Is there a way to fix this? Thanks.
It happens at mReader.open()
Here is the code snippet where the error happens:
public BroadcastReceiver mReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (ACTION_USB_PERMISSION.equals(action)) {
synchronized (this) {
Log.d(TAG,"BroadcastReceiver onReceive");
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if(mReader.isSupported(device)){
if (intent.getBooleanExtra(
UsbManager.EXTRA_PERMISSION_GRANTED, false)) {
if (device != null) {
Log.d(TAG,"BroadcastReceiver Opentask");
mReader.open(device);
}
}
}
}
} else if (UsbManager.ACTION_USB_DEVICE_DETACHED.equals(action)) {
Log.d(TAG,"BroadcastReceiver ACTION_USB_DEVICE_DETACHED");
synchronized (this) {
UsbDevice device = (UsbDevice) intent
.getParcelableExtra(UsbManager.EXTRA_DEVICE);
if (device != null && device.equals(mReader.getDevice())) {
Log.d(TAG,"BroadcastReceiver Closetaks");
mReader.close();
}
}
}
}
};
Here is the error received from the device:
java.lang.RuntimeException: Error receiving broadcast Intent { act=com.android.example.USB_PERMISSION flg=0x10 (has extras) } in ph.com.healthpods.fragments.BaseFragment$3@3b8349d7
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:876)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5280)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:963)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:758)
Caused by: java.lang.IllegalArgumentException: Cannot claim interface.
at com.acs.smartcard.Reader.open(SourceFile:1613)
at ph.com.healthpods.fragments.BaseFragment$3.onReceive(BaseFragment.java:278)
at android.app.LoadedApk$ReceiverDispatcher$Args.run(LoadedApk.java:866)