"...potential SecurityException" when trying to connect to BLE device

664 Views Asked by At

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)
        }
1

There are 1 best solutions below

0
Dylon Jaynes On

I actually figured it out. It seems that for Build.VERSION_CODES.S and up you are required to have code to manage permissions for specific requests such as BLUETOOTH_CONNECT, and BLUETOOTH_SCAN. Here is the code that fixed my issue.

// Enable scan permissions
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            enableScanPermissions()
        }
@RequiresApi(Build.VERSION_CODES.S)
    fun enableScanPermissions() {
        if (ContextCompat.checkSelfPermission(
                this@MainActivity,
                android.Manifest.permission.BLUETOOTH_SCAN
            ) !=
            PackageManager.PERMISSION_GRANTED
        ) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this@MainActivity,
                    android.Manifest.permission.BLUETOOTH_SCAN
                )
            ) {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(android.Manifest.permission.BLUETOOTH_SCAN), 1
                )
            } else {
                ActivityCompat.requestPermissions(
                    this@MainActivity,
                    arrayOf(android.Manifest.permission.BLUETOOTH_SCAN), 1
                )
            }
        }
    }
// Enable bluetooth connect permissions
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
            enableBluetoothConnect()
        }
@RequiresApi(Build.VERSION_CODES.S)
    private fun enableBluetoothConnect() {
        // Checks if location permissions are granted and asks for them if they are not.
        if (ContextCompat.checkSelfPermission(
                this@DeviceActivity,
                android.Manifest.permission.BLUETOOTH_CONNECT
            ) !=
            PackageManager.PERMISSION_GRANTED
        ) {
            if (ActivityCompat.shouldShowRequestPermissionRationale(
                    this@DeviceActivity,
                    android.Manifest.permission.BLUETOOTH_CONNECT
                )
            ) {
                ActivityCompat.requestPermissions(
                    this@DeviceActivity,
                    arrayOf(android.Manifest.permission.BLUETOOTH_CONNECT), 1
                )
            } else {
                ActivityCompat.requestPermissions(
                    this@DeviceActivity,
                    arrayOf(android.Manifest.permission.BLUETOOTH_CONNECT), 1
                )
            }
        }
    }