Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does UsbDeviceConnection claimInterface fail after openDevice

Tags:

android

I wrote a app to communcate with my own device via USB host. The code piece like this

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;
}

The logcat shows

D/UsbhostModbusAdapter: Opening the usb device success
D/UsbDeviceConnectionJNI: close 
D/UsbhostModbusAdapter: Usb claim interface failed

I noticed the device was closed by JNI after openDevice called. So what happed between openDevice and claimInterface? Is it a hardware problem?

PS: Sometime the same device can claim successful and send/receive data OK. It depend on the open operation when plug into the phone. When first open is ok, then close and reopen always ok. But if first open not ok, then it will never open ok.

like image 208
Hans Avatar asked Jan 20 '26 18:01

Hans


1 Answers

You have not explained the event of calling this code.
This can happen if
1. your part of code is being called twice back to back without closing previous connection.
2. When your activity is destroyed then you are missing call to connection.close() due to which previous connection is keeping the device occupied.

like image 197
chzahid Avatar answered Jan 23 '26 20:01

chzahid