I've developed a file manager application to explore android file system. How do I make it suitable to explore usb storage devices connected to any android device. I have tried providing intent filter for USB_DEVICE_ATTACHED action and also provided a device filter list. But, the problem is a dialog for permission is displayed only when user pluggs in a usb device whose details are mentioned in the filter list. How can I make a generic filter that asks for user permission as and when any usb storage device is attached to the android device(application should ask for permission for every usb device connected).
It's a bit of a tricky subject, unfortunately, because of the way most USB storage devices classify themselves. The <usb-device> filter tag does support options for class and subclass, but they are only evaluated at the device descriptor level.
Mass storage devices will declare USB_CLASS_MASS_STORAGE, but they will typically do so on the interface descriptor, which is something Android does not evaluate in the filter. So you have little resource for a filter that captures "only mass storage devices from any manufacturer".
You can always supply an empty device filter, which matches ALL devices connected. It's a bit of a shotgun approach, but it guarantees your app has a chance:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<usb-device />
</resources>
In the long run, you are banking on the fact that the average Android user will only attach very few USB devices, and they will most likely be storage devices if they have installed your application.
I have an example app that does just this to capture connect events from all USB devices.
Having said all that, your application does not need to filter and receive USB_DEVICE_ATTACHED events just to communicate with those devices. That event exists only so your app can be notified the moment a connection is made. You can still query the UsbManager at any time and connect to existing devices from your app when it is launched without handling these events.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With