0

I'm trying to read data from a USB device using UsbManager. The device I am trying to read the data from is a camera (but android recognizes it as a USB device) The problem that I currently encountered is that it doesn't let me ask for permission to access the USB device. I have the necessary uses-permissions and enabled host mode in the manifest.

Any suggestions why this happens and how to fix it?

Ive tried 2 methods for asking permissions

private fun requestPermission() {
        requestCameraPermissionIfMissing { granted ->
            if (granted){
                usbComm = Camera(this, intArrayOf(7758))

                usbComm.startUsbConnection()//find device from connected devices and establish connection
            }
            else
                Toast.makeText(this, "Please Allow the Permission", Toast.LENGTH_LONG).show()
        }
    }
    // Show popup to request permission to access camera if granted return true otherwise false
    private fun requestCameraPermissionIfMissing(onResult: ((Boolean) -> Unit)) {
        if (ContextCompat.checkSelfPermission(
                this,
                ACTION_USB_PERMISSION
            ) == PackageManager.PERMISSION_GRANTED
        )
            onResult(true)
        else
            registerForActivityResult(ActivityResultContracts.RequestPermission()) {
                onResult(it)
            }.launch(ACTION_USB_PERMISSION)

    }

The other one is:

val intent: PendingIntent = PendingIntent.getBroadcast(
                        context,
                        0,
                        Intent(ACTION_USB_PERMISSION),
                        PendingIntent.FLAG_MUTABLE
                    ) 
                    manager.requestPermission(m_device, intent)

Both dosnt show me the request on the device and also i dont see in debug that permission was granted

And also when i try doing the fallowing without permission my app crash

 var intf = m_device?.getInterface(0)!!
                    endpoint = intf.getEndpoint(0)
                    if (m_connection!!.claimInterface(intf, true)) { // Crashed here
                        Log.i("serial", "The interface was claimed successfully")
                    } else {
                        Log.i("serial", "The interface could not be claimed")
                    }
IdH
  • 41
  • 4

1 Answers1

0

I found a way to solve this in case anyone else has encountered it too, you need to ask permission for the app, in my case, it was

Manifest.permission.CAMERA
Manifest.permission.RECORD_AUDIO

and after that ask for permission to access the device and it will broadcast the intent.

bongo
  • 733
  • 4
  • 12
IdH
  • 41
  • 4