USB_DEVICE_ATTACHED without a Device Filter in Android 10
Asked Answered
U

1

5

I'm developing an Android application which registers four intent filters for a BroadcastReceiver in the manifest. These are:

android.hardware.usb.action.USB_DEVICE_ATTACHED
android.hardware.usb.action.USB_DEVICE_DETACHED
android.hardware.usb.action.USB_ACCESSORY_ATTACHED
android.hardware.usb.action.USB_ACCESSORY_DETACHED

Note that there is currently no device-filter metadata registered alongside any of these intents. When I launch this app on Android 9 (specifically on a Samsung Galaxy S9), any and all USB devices are registered and the onReceive method is successfully called in the BroadcastReceiver. However, the same code being run in Android 10 (on a Galaxy S20) does not call onReceive. I realise that the device filter is listed as a requirement in the developer documentation, but Android 9 accepts it no questions asked.

In an ideal world, I would like not to use a device filter, since my app includes a dynamic device filtering option via user configuration where they input a PID/VID combination for the app to work with.

My question is: Is there any way of giving an "allow-all" device filter to an Android 10 application through the manifest? Is this change in device filter behaviour an intended feature of Android in general, or just Samsung's implementation?

In case it affects anything, this app is a device administrator.

Unrequited answered 9/11, 2020 at 12:45 Comment(0)
U
13

Answered my own question for any future googlers: Put a value of -1 for PID and VID in the device filter. So the device_filter.xml file looks like:

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

I had to trawl through the Android source files for this, nowhere else seems to mention the option of a wildcard.

Unrequited answered 9/11, 2020 at 14:29 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.