javaandroidbluetooth

Issue with startActivityForResult and BLUETOOTH_CONNECT Permission on Android 12 (API Level 31)


I encountered an issue in my app that enables Bluetooth. After updating to Android 12, startActivityForResult was deprecated, and the app crashed with a SecurityException stating that the BLUETOOTH_CONNECT permission is required.

Solution:

  1. Add permissions in the AndroidManifest.xml:

For Android 12 and higher, new Bluetooth permissions need to be added to the AndroidManifest.xml:

<uses-permission android:name="android.permission.BLUETOOTH_CONNECT"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE"/>
  1. Request the BLUETOOTH_CONNECT permission at runtime:

On Android 12, these permissions need to be requested at runtime. Here’s how I adjusted the bluetoothOn() method:

private void bluetoothOn() {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.BLUETOOTH_CONNECT) 
            != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, 
            new String[]{Manifest.permission.BLUETOOTH_CONNECT}, REQUEST_ENABLE_BT);
        return;
    }

    if (!mBTAdapter.isEnabled()) {
        Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
        bluetoothLauncher.launch(enableBtIntent);
    } else {
        Toast.makeText(getApplicationContext(),"Bluetooth is already on", Toast.LENGTH_SHORT).show();
    }
}
  1. Handle the permission request result:

The permission request results are handled as follows:

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == REQUEST_ENABLE_BT) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            bluetoothOn();
        } else {
            Toast.makeText(this, "Permission denied to access Bluetooth", Toast.LENGTH_SHORT).show();
        }
    }
}
  1. Replace startActivityForResult with ActivityResultLauncher:

Since startActivityForResult is deprecated in Android 12, I replaced it with ActivityResultLauncher:

ActivityResultLauncher<Intent> bluetoothLauncher = registerForActivityResult(
        new ActivityResultContracts.StartActivityForResult(),
        result -> {
            if (result.getResultCode() == RESULT_OK) {
                mBluetoothStatus.setText("Enabled");
            } else {
                mBluetoothStatus.setText("Disabled");
            }
        }
);

This solution worked for me on Android 12 and above.


Solution

  • BLUETOOTH_CONNECT is a run time permission. You have to explicitly request user the permission to connect. For testing purpose, I would suggest granting the permission manually via Android Settings > Apps > {Your App} > Permissions.

    Best practice is to always check for permission before calling these Android APIs to avoid such crashes.