kable
kable copied to clipboard
Support Android 12 Bluetooth Permissions
With Android 12 comes two new bluetooth permissions that will replace Location Permission:
-
Bluetooth Scan
-
Bluetooth Connect
Am trying to scan on Android 12 phone but nothing happens
withTimeoutOrNull(SCAN_DURATION_MILLIS) {
scanner.advertisements
.catch { cause -> _scanStatus.value = ScanStatus.Failed(cause.message ?: "Unknown error") }
.onCompletion { _scanStatus.value = ScanStatus.Stopped }
.distinctUntilChanged()
.collectLatest { advertisement ->
Logger.warn("$advertisement")
_scanStatus.value = ScanStatus.AdvertisementFound(advertisement)
}
}
I came across a solution
fun requestLocationPermission(requestMultiplePermissions: ActivityResultLauncher<Array<String>>) =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
requestAndroidSBlePermission(requestMultiplePermissions)
} else
requestMultiplePermissions.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION))
fun requestAndroidSBlePermission(requestMultiplePermissions: ActivityResultLauncher<Array<String>>) =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S)
requestMultiplePermissions.launch(
arrayOf(
Manifest.permission.ACCESS_FINE_LOCATION,
Manifest.permission.ACCESS_COARSE_LOCATION,
Manifest.permission.BLUETOOTH_SCAN,
Manifest.permission.BLUETOOTH_CONNECT
)
)
else {
Logger.info("Do nothing")
}
private val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {permissions ->
}
I came across a solution
fun requestLocationPermission(requestMultiplePermissions: ActivityResultLauncher<Array<String>>) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { requestAndroidSBlePermission(requestMultiplePermissions) } else requestMultiplePermissions.launch(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION)) fun requestAndroidSBlePermission(requestMultiplePermissions: ActivityResultLauncher<Array<String>>) = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) requestMultiplePermissions.launch( arrayOf( Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.BLUETOOTH_SCAN, Manifest.permission.BLUETOOTH_CONNECT ) ) else { Logger.info("Do nothing") } private val requestMultiplePermissions = registerForActivityResult(ActivityResultContracts.RequestMultiplePermissions()) {permissions -> }
@dononcharles I don't think the Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION would be required anymore on versions S and up.
0.21.0 includes improved permissions declarations as well as a Bluetooth.availability
flow (but it does not yet handle permissions — but it probably should take into account permissions for the value it emits; I'll leave this ticket open as a reminder to add that functionality).
Although, it isn't planned that Kable will request permissions, as that is very app specific; so the code example you provided is the correct approach (aside from what was pointed out — that you don't need location permission, for BLE at least, on newer versions of Android).
Actually, closing out this issue and rolling it into https://github.com/JuulLabs/kable/issues/420.