Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Proper way to close a USB accessory connection

Tags:

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.

like image 993
Ytai Avatar asked Nov 26 '11 01:11

Ytai


People also ask

What is the USB accessory?

USB accessory mode allows users to connect USB host hardware specifically designed for Android-powered devices. The accessories must adhere to the Android accessory protocol outlined in the Android Accessory Development Kit documentation.

How do I get USB permissions on Android?

When you connect USB the first time (with already installed app), system call permission dialog. Then another time at start app usb will get permission automatically.

What is accessory framework android?

Open Accessory API or Open Accessory Framework - this is the API/framework in the Android development environment that allows the Android applications to transmit data in and out of the available USB port. This is provided by Google through the Android SDK.


1 Answers

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.

like image 51
Ytai Avatar answered Sep 22 '22 21:09

Ytai