Short answer: choose the permission list by Build.VERSION.SDK_INT, request it with rememberLauncherForActivityResult and RequestMultiplePermissions, and only start scanning once every permission in the list is granted.
Pick the right permissions
fun blePermissions(): Array<String> =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
arrayOf(
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT
)
} else {
arrayOf(Manifest.permission.ACCESS_FINE_LOCATION)
}
Composable that requests them
@Composable
fun BlePermissionGate(onGranted: () -> Unit) {
val context = LocalContext.current
val perms = remember { blePermissions() }
val launcher = rememberLauncherForActivityResult(
ActivityResultContracts.RequestMultiplePermissions()
) { result ->
if (result.values.all { it }) onGranted()
}
fun allGranted() = perms.all {
ContextCompat.checkSelfPermission(context, it) ==
PackageManager.PERMISSION_GRANTED
}
Button(onClick = {
if (allGranted()) onGranted() else launcher.launch(perms)
}) { Text("Scan for devices") }
}
Handle denial
If the user denies twice, Android stops showing the dialog. Explain why you need the permission and offer a button that opens app settings.
val intent = Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS)
.setData(Uri.fromParts("package", context.packageName, null))
context.startActivity(intent)
Manifest reminder
The permissions must also be declared in AndroidManifest.xml as shown in our BLE guide, otherwise the request is ignored.
FAQ
Do I still need location permission on Android 12+?
Not for scanning if you declare BLUETOOTH_SCAN with neverForLocation and do not derive location from results.
Where should this code live?
Keep permission UI in the composable and BLE logic in a ViewModel or repository.