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.

César
  • 9,206
  • 5
  • 46
  • 69
Ytai
  • 631
  • 4
  • 9
  • 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. – Paulina D. Jan 14 '13 at 23:53

2 Answers2

16

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.

Ytai
  • 631
  • 4
  • 9
  • Yeah great, I like those Bugs :( How did you circumvent this issue? Any workaround you were applying? – Briareos386 Apr 11 '12 at 07:51
  • 2
    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. – Ytai Apr 11 '12 at 15:35
  • Thanks! I think that's also the way I'll implement it. – Briareos386 Apr 11 '12 at 21:09
  • 2
    So how do you implement a "soft close"? – Luis Jan 23 '13 at 23:45
  • @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. – svachalek Mar 13 '13 at 00:45
  • Probably the issue can be workarounded by using libusb instead of UsbManager, though I haven't tried myself: http://stackoverflow.com/a/16234205/1028256 It supports async mode, so a blocking call to native code can be avoided, IMHO – Mixaz Dec 16 '13 at 17:48
  • 1
    Also update on Android bug 20545: a fix was developed but haven't merged into Android trunk yet: https://code.google.com/p/android/issues/detail?id=61390. – Mixaz Dec 16 '13 at 17:49
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!

user1361207
  • 101
  • 1
  • 1
  • 6