10

In an Android app, I need permission for a specific USB device (known vendor/product ID), which is permanently plugged on the Android device.

So far I've made the following observations:

  • when using UsbManager.requestPermission() the permission is dismissed when replugging/rebooting and the popup's checkbox seems to have no effect at all on this behavior
  • when using intent filters, the permission is properly re-granted when replugging the USB device (even when replugging after a reboot, if the popup's checkbox was checked) BUT the permission is not re-granted after reboot unless I unplug & replug the USB device.

Is it possible to make sure that the permission popup is displayed only once ? (especially in the scenario when the Android device is rebooted and the USB device remains plugged in)

NB: the app isn't installed in the system folder and therefore cannot use the MANAGE_USB permission.

sdabet
  • 18,360
  • 11
  • 89
  • 158

2 Answers2

14

The solution is to use intent filters and add the android:directBootAware="true" attribute on the associated activity so that the USB_DEVICE_ATTACHED event is correctly received after boot.

sdabet
  • 18,360
  • 11
  • 89
  • 158
  • If I just could give you one MILLION up votes :-) Thank you very much. How did you arrive at that piece of code `android:directBootAware="true"`? Magic. I spent hours looking for this. – W.M. Mar 19 '20 at 22:18
  • I gave up months ago trying to find a solution for this. By mere chance I re-searched about the topic and found your updated solution that works! – The Berga Jun 26 '20 at 09:38
  • It seems the issue was fixed on Android 13, it works without `directBootAware`, but we still need to use intent filters if the permission allowance persist needed. – thanhbinh84 Jul 26 '23 at 04:51
  • Working like a charm. You saved my day. Thank you very much. – Shahin ShamS Aug 13 '23 at 23:16
0

Persisting USB Permission is not possible, as the reboot option is stopping everything forcefully generally it makes the USB also unplugged.

There is one possibility to ask for replugging USB on reboot.

Is it possible to make sure that the permission popup is displayed only once? No

Because If your are connecting the usb which makes a deadlock situation for taking care of that they make it unplugged, rather than disturbing the system.

So it is not possible to make it USB Plugged-in.

Amjad Khan
  • 1,309
  • 15
  • 32
  • We can't really say that the reboot "makes the USB unplugged" since the USB device is actually visible with UsbManager after reboot. It just doesn't keep (or re-grant) the access permission. – sdabet Feb 05 '19 at 08:39
  • Yes, It removes the access which has given to the USB – Amjad Khan Feb 05 '19 at 09:25
  • do you know any way to programmatically trigger a unplug/replug ? – sdabet Feb 05 '19 at 10:48