Skip to content

Instantly share code, notes, and snippets.

@tdcolvin
Last active January 4, 2024 18:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdcolvin/7aa3c7d31c0f2e840ac5527c07dff6eb to your computer and use it in GitHub Desktop.
Save tdcolvin/7aa3c7d31c0f2e840ac5527c07dff6eb to your computer and use it in GitHub Desktop.
val ALL_BLE_PERMISSIONS = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
arrayOf(
Manifest.permission.BLUETOOTH_CONNECT,
Manifest.permission.BLUETOOTH_SCAN
)
}
else {
arrayOf(
Manifest.permission.BLUETOOTH_ADMIN,
Manifest.permission.BLUETOOTH,
Manifest.permission.ACCESS_FINE_LOCATION
)
}
@Composable
fun GrantPermissionsButton(onPermissionGranted: () -> Unit) {
val launcher = rememberLauncherForActivityResult(
contract = ActivityResultContracts.RequestMultiplePermissions()
) { granted ->
if (granted.values.all { it }) {
// User has granted all permissions
onPermissionGranted()
}
else {
// TODO: handle potential rejection in the usual way
}
}
// User presses this button to request permissions
Button(onClick = { launcher.launch(ALL_BLE_PERMISSIONS) }) {
Text("Grant Permission")
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment