Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android USB Device Permission and Intent Filter

I have an Android App which will interact with an USB device, in the Android developer official documentation for Obtaining permission to communicate with a device it says:

Note: If your application uses an intent filter to discover USB devices as they're connected, it automatically receives permission if the user allows your application to handle the intent. If not, you must request permission explicitly in your application before connecting to the device.

The Intent Filter is to be declared in the AndroidManifest.xml and using a resource file which will include the vendor-id and product-id.

If i plug my device nothing happens, even with the declared Intent. So i have to use the method mentioned in the same official documentation which involves explicitly asking for permission.

This works and i can communicate with the device, but it happens that the user has to give the permission each time the app is started, or the device reconnected. In the end this will present a message to the user asking for confirmation each time the device is plugged and the application started, and giving the same permission each time is not desirable.

So if the "... automatically receives permission ..." part is not working.

How do i make the intent filter to work?
What am i missing?
Do i have any other alternatives?


This question: Android USB Permissions Dialog never appears had the opposite problem, but the solution won't work for me.
Making the app a System app and bypassing permissions won't work for me either as mentioned here: USB open accessory permissions through a service in android


Any help is greatly appreciated

like image 678
Anibaru Avatar asked Mar 06 '13 18:03

Anibaru


1 Answers

I have the same problem, in your AndroidManifest.xml in the Activity that use the Device write this:

<intent-filter>
<action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
</intent-filter>
<meta-data
android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED"
android:resource="@xml/device_filter" />

You also have to create a filter file in your xml resources, eg res/xml/device_filter:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
    <usb-device vendor-id="26214" product-id="26214" />
    </resources>

The vendor-id and product-id here have to be given in decimal - above both the VID and PID are 0x0123. you see USB device access pop-up supression?

you must not call mManager.requestPermission(device, mPermissionIntent), so you have to directly call mManager.openDevice(mDevice); and you can communicate normally without the need appear permission pop-up message.

Regards, Samuel Reque [email protected]

like image 65
Samuel Reque Zambrana Avatar answered Oct 02 '22 11:10

Samuel Reque Zambrana