Proper way to close a USB accessory connection

僤鯓⒐⒋嵵緔 提交于 2019-12-03 18:51:28

问题


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.


回答1:


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.




回答2:


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!



来源:https://stackoverflow.com/questions/8275730/proper-way-to-close-a-usb-accessory-connection

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!