I'm getting this error in several places even though I have requested BLUETOOTH_CONNECT permissions from the user earlier in my app.
Call requires permission which may be rejected by user: code should explicitly check to see if permission is available (with checkPermission) or explicitly handle a potential SecurityException
Why is this showing up if permission has already been granted? Do I need to ask for permissions every time I do something with bluetooth?
Here is how I'm currently asking for bluetooth permissions:
// Bluetooth permissions callback.
private var requestBluetooth =
registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result ->
if (result.resultCode == RESULT_OK) {
// Granted
Log.i("Bluetooth", "Bluetooth permission granted!")
} else {
// Denied
Log.e("Bluetooth", "Bluetooth permission denied!")
progressBar.visibility = GONE
}
}
if (!adapter.isEnabled) {
// Check if bluetooth is enabled on the device.
val enableBtIntent = Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE)
requestBluetooth.launch(enableBtIntent)
}
I actually figured it out. It seems that for
Build.VERSION_CODES.Sand up you are required to have code to manage permissions for specific requests such asBLUETOOTH_CONNECT, andBLUETOOTH_SCAN. Here is the code that fixed my issue.