admin管理员组

文章数量:1386490

I am trying to request Bluetooth permissions required for the Bluetooth discovery process. I have have been able to successfully request permission from the user, but when I check for permission, before calling startDiscovery(), it returns false for BLUETOOTH_CONNECT, even if the permissions were given by the user. Like the title says, is there a way around this?

this is the my instantiation of an activity result launcher:

val permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted: Boolean ->
    if (granted) {
        textView1.text = "permission granted"
    } else {
        textView1.text = "not granted"
    }
}

This is my code where I launch the activity result launcher with the permissions I need( I wasn't sure how to request multiple permissions at once):

permissionLauncher.launch(Manifest.permission.BLUETOOTH_CONNECT)
permissionLauncher.launch(Manifest.permission.BLUETOOTH_SCAN)

I am trying to request Bluetooth permissions required for the Bluetooth discovery process. I have have been able to successfully request permission from the user, but when I check for permission, before calling startDiscovery(), it returns false for BLUETOOTH_CONNECT, even if the permissions were given by the user. Like the title says, is there a way around this?

this is the my instantiation of an activity result launcher:

val permissionLauncher = registerForActivityResult(ActivityResultContracts.RequestPermission()) { granted: Boolean ->
    if (granted) {
        textView1.text = "permission granted"
    } else {
        textView1.text = "not granted"
    }
}

This is my code where I launch the activity result launcher with the permissions I need( I wasn't sure how to request multiple permissions at once):

permissionLauncher.launch(Manifest.permission.BLUETOOTH_CONNECT)
permissionLauncher.launch(Manifest.permission.BLUETOOTH_SCAN)
Share Improve this question edited Mar 19 at 7:11 tomerpacific 6,59818 gold badges41 silver badges60 bronze badges asked Mar 18 at 21:54 Zero_CoolV2.0Zero_CoolV2.0 133 bronze badges 1
  • 1 Start by switching to ActivityResultContracts.RequestMultiplePermissions and see if that helps. See the docs. – CommonsWare Commented Mar 18 at 21:59
Add a comment  | 

1 Answer 1

Reset to default 1

You should use ActivityCompat.requestPermissions() with an array of permissions to request permissions, it is likely that only one of your permissions is being granted.

本文标签: android bluetoothApp permissions for BLUETOOTHCONNECT returns false until app is restartedStack Overflow