I have a Bluetooth application in Android 10 and before I can start to scan for the devices I need to request the location permissions. When I install the application for the first time I get the popup for the location, but after that, if I turn them off it won't ask again and I need to ask the user to turn them on every time if they are off.
Here's my code:
override fun onStart() {
super.onStart()
requestPermissions(arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), PERMISSION_REQUEST_CODE)
}
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults)
if (requestCode == PERMISSION_REQUEST_CODE && permissions.contains(Manifest.permission.ACCESS_FINE_LOCATION)) {
if (grantResults.contains(PackageManager.PERMISSION_GRANTED)) {
if (!BluetoothAdapter.getDefaultAdapter().isEnabled) {
startActivityForResult(Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE), ENABLE_BLT_REQUEST_CODE)
} else {
viewModel.discoverBLTDevices()
}
} else {
showToast("Location permission needed to scan for devices.")
}
}
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == ENABLE_BLT_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
viewModel.discoverBLTDevices()
} else if (resultCode == Activity.RESULT_CANCELED) {
finish()
}
}
}