Proper way to close a USB accessory connection
Asked Answered
S

2

22

What is the proper way to close a connection to a UsbAccessory in Android? It seems the even in the stock Google example, if I connect and accessory, exit the app and then go back to it, the connection is not re-established.

Looking closely, it seems that after calling close() on the FileDescriptor, it won't open again, and a "could not open /dev/usb_accessory" log is emitted. NOT calling close() is a bad option, as a thread blocking on read() will not be released. Upon physical disconnection / reconnection of the device everything is OK.

It seems really surprising that the simple use-case of exiting the app and then opening it again does not work in the reference application and even more surprising if it is not feasible.

I'm using a Nexus S running stock Android 2.3.6.

Semicentennial answered 26/11, 2011 at 1:54 Comment(1)
I'm having this exact same problem, the "could not open /dev/usb_accessory" message but whenever I try to reconnect my device :( I still have my app open btw.Khmer
S
15

The problem is that the reading thread never exits, thus the file descriptor stays open, and cannot be opened again when the app is resumed.

This has been confirmed to be a bug: http://code.google.com/p/android/issues/detail?id=20545

Vote on this bug if you care about it.

Semicentennial answered 27/11, 2011 at 7:58 Comment(7)
Yeah great, I like those Bugs :( How did you circumvent this issue? Any workaround you were applying?Mingle
The way I worked around it is by sending a "soft close" command from the Android, which results in a "soft close" command back from the accessory to the Android, which in turn unblocks the read(). It will only work if the application exists gracefully and it the protocol doesn't lose sync. In practice, it is not perfect, but better than nothing.Semicentennial
Thanks! I think that's also the way I'll implement it.Mingle
So how do you implement a "soft close"?Aeroscope
@Leco I believe he is asking the accessory side to close the connection. Makes sense, I'll try that too. On that thought, I may also ask for a libusb_reset_device which should definitely force a cycle.Confederation
Probably the issue can be workarounded by using libusb instead of UsbManager, though I haven't tried myself: https://mcmap.net/q/591639/-compile-and-link-against-libusb-for-android It supports async mode, so a blocking call to native code can be avoided, IMHOHeavy
Also update on Android bug 20545: a fix was developed but haven't merged into Android trunk yet: code.google.com/p/android/issues/detail?id=61390.Heavy
S
0

try to reconnect twice it seams that the first time that is unsuccess will close the connection and then try again to open it will work! for me it worked!

Sexuality answered 15/1, 2015 at 8:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.