in my new android project, I have a broadcast receiver that obtains a usb device and requests permissions if needed. I also have an intent filter on the activity that has the vendor ids it should recognise.
`class UsbReceiver: BroadcastReceiver() {
private val ACTION_USB_PERMISSION = "com.app.com.USB_PERMISSION"
override fun onReceive(context: Context?, intent: Intent?) {
val device = intent?.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
val manager = context?.applicationContext!!.getSystemService(Context.USB_SERVICE) as UsbManager
val mIntent = Intent(context, UsbPermissionReceiver::class.java)
mIntent.action = ACTION_USB_PERMISSION
val pIntent = PendingIntent.getBroadcast(context, 0, mIntent, PendingIntent.FLAG_MUTABLE)
if (!manager.hasPermission(device))
manager.requestPermission(device, pIntent)
// do what I want with device here
}
}
class UsbPermissionReceiver: BroadcastReceiver() {
override fun onReceive(context: Context?, intent: Intent?) {
if (intent?.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED, false) == true) {
val device = intent.getParcelableExtra<UsbDevice>(UsbManager.EXTRA_DEVICE)
// do what I want with device here
}
}
}`
This correctly shows a permission window when required that has a box saying "Always open APP NAME when DEVICE NAME is connected". If you check this check box, then press allow, the message will still appear the next time you connect the device. In my previous project, this worked fine, and persisted the permission.
Any ideas? The code is the same with my other project and Is referenced correctly in the manifest. It's very strange! Thanks
- I've checked the manifest and broadcast receiver
- Intent filter is correct
You should get a dialog like the below image:
In this example, STM32 Virtual ComPort is a USB device.
It should not ask to for this permission if you check the checkbox. There may be the reasons for your problem.
If the device other than the permitted USB device(i.e STM32 Virtual ComPort in this case) is connected, this permission dialog shall be displayed.
If the application is force stopped, cache cleared, storage cleared, or in the case of permission reset, the dialog shall be displayed.
You can easily get help from or compare your code with UVCPermissionTest project.