当前,我正在开发一个需要两个硬件才能通过USB连接到设备的应用程序。我遇到一个错误,该应用程序可以看到两个设备,但该应用程序只能建立与插入USB集线器的最后一个硬件的连接。
Android OS版本是5.1,我使用了UsbManager类。
是否可以解决此问题?谢谢。
它发生在mReader.open()
以下是发生错误的代码段:
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();
}
}
}
}
};
这是从设备收到的错误:
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)