我写了一个应用程序,通过USB主机与我自己的设备通信。像这样的代码
UsbDeviceConnection connection = mUsbManager.openDevice(mDevice);
if (connection == null) {
Log.d(TAG, "open device failed");
break;
}
Log.d(TAG, "Opening the usb device success");
if (!connection.claimInterface(ui, true)) {
connection.close();
Log.d(TAG, "Usb claim interface failed");
break;
}
logcat显示
D/UsbhostModbusAdapter: Opening the usb device success
D/UsbDeviceConnectionJNI: close
D/UsbhostModbusAdapter: Usb claim interface failed
我注意到在openDevice调用后,设备被JNI关闭了。那么openDevice和claimInterface之间发生了什么?这是硬件问题吗?
PS:有时同一设备可以声称成功并发送/接收数据OK。它取决于插入手机时的开放操作。第一次打开时没问题,然后关闭并重新打开即可。但如果先打开不好,那么它永远不会打开。
答案 0 :(得分:0)
您尚未解释调用此代码的事件。
如果发生这种情况,
1.您的代码部分被连续两次调用,而不会关闭先前的连接。
2.当您的活动被破坏时,您将丢失对connection.close()的调用,因为先前的连接使设备处于占用状态。