1. Are you ready for the Galaxy S20? Here is everything we know so far!

How to use USBManager without always asking the user permissions?

Discussion in 'Android Development' started by Swisshornet93, May 6, 2019.

  1. Swisshornet93

    Swisshornet93 Lurker
    Thread Starter


    I'm developing an application that use a USB device. Each time the device is reconnected, i must ask the user permission to connect to it.
    The problem is that the smartphone and the device should work without an user intervention. Sometimes, the USB connector move a little bit and all my application is blocked because the permission are not granted anymore...

    I found some tips for older versions of Android (...) but nothing for API 27...

    Someone have a solution or a workaround to use a USB device without the user permission ?


  2. Deleted User

    Deleted User Guest

    scary alien and GameTheory like this.
  3. Swisshornet93

    Swisshornet93 Lurker
    Thread Starter

    After an enumeration of the USB devices connected on the USB connector, I was requesting the USB permission with the function UsbManager.requestPermission(device, id) for a specific device.
    If i remove the requestPermission() function, I receive a runtime error.
    If i use the function hasPermission() of the class UsbManager before asking the permission, each time the USB device is disconnected, it forget that it was already granted...

    I finally found a workaround with this documentation: https://developer.android.com/guide/topics/connectivity/usb/host#permission-d
    I use now an Intent Filter that start my application when a USB device (matching with my filter) is connected. It automatically receive the permission and don't show a popup anymore.

    It's not the best way because I have to initialize all again in the function onResume() each time my USB device is detected.
    If you have other ideas, please let me know and i'll try it.
    scary alien likes this.

Share This Page