How to avoid explicit permission when trying to communicate with an accessory?

104 views Asked by At

I'm using platform APIs (not add-on library) to connect Android device with USB host hardware. I'm using this official documentation in order to perform connection and I can establish connection successfully and communicate with the accessory.

The only problem I have is that permission dialog you can see on the screenshot. It appears each time accessory is attached to the tablet. In my case user shouldn't be able to see the permission:

  1. For the user convenience. Plus If user by mistake clicks cancel, then the communication will not be established.
  2. The application always runs in the kiosk mode. Which means my app is the only one visible. As a result the pop up is never visible and user can't event grant the permission to connect to USB accessory event if it was allowed to grant it manually.

enter image description here

Please, if anyone knows how can I avoid this permission pop up, or maybe there is a way to grant the permission programmatically.

===

Note in documentation they say:

Note: If your application uses an intent filter to discover accessories as they're connected, it automatically receives permission if the user allows your application to handle the intent. If not, you must request permission explicitly in your application before connecting to the accessory.

From this quote it looks like there must be a way to automatically receive the permission, instead of requesting it explicitly. I can't get how to do that.

==

Some additional code.

In onCreate

fun startInitializingStreams() {
    permissionIntent = PendingIntent.getBroadcast(context, 0, Intent(ACTION_USB_PERMISSION), 0)
    val filter = IntentFilter(ACTION_USB_PERMISSION)
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_ATTACHED)
    filter.addAction(UsbManager.ACTION_USB_ACCESSORY_DETACHED)

    logd("CREATE: register ${android.os.Process.myPid()}")
    context.registerReceiver(usbReceiver, filter)
}

In onResume

  if (context.app.usbManager.hasPermission(accessory)) {
        openAccessory(accessory)
    } else {
        if (!permissionRequestPending) {
            context.app.usbManager.requestPermission(accessory, permissionIntent)
            permissionRequestPending = true
        }
    }
0

There are 0 answers