8

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

2 Answers 2

8

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 samo_reque@hotmail.com

5
  • I'll check your answer, but i already have the device_filter xml
    – Anibaru
    May 27, 2013 at 21:12
  • Only the first time you run the app requests permissions for the intent-filter, but do not require the code call the mManager.requestPermission (device, mPermissionIntent) is a partial solution. Jul 23, 2013 at 19:47
  • @SamuelReque: I have those manifest permissions under my Broadcast Receiver class instead of an activity,but I still get those popups everytime I re-attach the usb(checking the checkbox is also of no use).How to go about it?
    – Basher51
    Jul 3, 2014 at 7:04
  • @Basher51: check that no calling method mManager.requestPermission (device, mPermissionIntent) this causing the call to pop up Mar 11, 2015 at 20:52
  • Thanks for pointing me to the direction of the problem. I had already implemented a working android app for a USB device and I was trying to reuse the same code for another device, but it wouldn't work. It turns out that Android indeed parses the "vendor-id" and "product-id" as decimal by default, but automatically switches to hex if there is a A-F character in the number. My new device happened to use IDs with only 0-9 characters in the hex representation, and therefore the numbers were misinterpreted as decimal. Prefixing "0x" to the IDs fixed the issue.
    – Foaly
    Oct 20, 2020 at 19:14
0

I don't undersand your question well but I think solution is like this..

<activity ...>
<intent-filter >
            <action android:name="android.intent.action.MAIN" />                
            <action android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" />
            <action android:name="android.hardware.usb.action.USB_DEVICE_DETACHED" />
            <meta-data android:name="android.hardware.usb.action.USB_DEVICE_ATTACHED" android:resource="@xml/device_filter"/>
            <category android:name="android.intent.category.HOME" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
</activity>
1
  • I have exactly that in my project, but according to the documentation that should be enough to give permissions to access the USB device. But it doesn't work hence the question.
    – Anibaru
    Mar 7, 2013 at 13:47

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.