Search code examples
androidandroid-usb

Persisting USB permission after reboot without replugging device


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.


Solution

  • 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.