Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Requesting multiple Bluetooth permissions in Android Marshmallow

I'm developing an app with connectivity which connects to a Bluetooth device with SDK 23 as compile with. I'm having problems with requesting multiple permissions for Bluetooth. This is what I have done so far:

@Override
public void onStart() {
    super.onStart();
    if (D)
        Log.e(TAG, "++ ON START ++");


    if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
            Manifest.permission.BLUETOOTH)
            != PackageManager.PERMISSION_GRANTED) {
    } else {
        ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
                new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
                REQUEST_ENABLE_BT);
    }


    if (ContextCompat.checkSelfPermission(MyBlueToothClientActivity.this,
            Manifest.permission.BLUETOOTH)
            != PackageManager.PERMISSION_GRANTED) {
    } else {

        ActivityCompat.requestPermissions(MyBlueToothClientActivity.this,
                new String[]{Manifest.permission.BLUETOOTH, Manifest.permission.BLUETOOTH_ADMIN},
                REQUEST_CONNECT_DEVICE_INSECURE);
    }
}


@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case REQUEST_ENABLE_BT: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

    // permission was granted, yay! 
                Intent enableIntent = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_ENABLE_BT);


            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                if (CommonData.mChatService == null)
                    setupChat();

                Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        case REQUEST_CONNECT_DEVICE_INSECURE: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay!
                Intent enableIntent = new Intent(
                        BluetoothAdapter.ACTION_REQUEST_ENABLE);
                startActivityForResult(enableIntent, REQUEST_CONNECT_DEVICE_INSECURE);


            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                if (CommonData.mChatService == null)
                    setupChat();

                Toast.makeText(MyBlueToothClientActivity.this, "Permission denied for bluetooth", Toast.LENGTH_SHORT).show();
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

Although I'm able to get the dialogue box for requesting enabling the Bluetooth, I don't get the second permission, i.e. to connect to a device. In the logcat, I get:

    01-01 06:41:24.334 25473-25473 E/BluetoothChat: ++ ON START ++
    01-01 06:41:24.344 25473-25473 W/Activity: Can reqeust only one set of permissions at a time

And since I'm not able to connect to the device, I just get stuck here. And this code works fine on Android version up to Lollipop, just causes problem on the Marshmallow version.

like image 967
Mirhawk Avatar asked Apr 22 '16 04:04

Mirhawk


1 Answers

BLUETOOTH and BLUETOOTH_ADMIN are normal permissions and are therefore they are automatically granted. Only permissions in the table of dangerous permissions need to requested at runtime.

However, as mentioned in the Android 6.0 changes: Access to Hardware Identifier:

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions:

  • WifiManager.getScanResults()
  • BluetoothDevice.ACTION_FOUND
  • BluetoothLeScanner.startScan()

If you're using any of those methods, you'll need to request at least ACCESS_COARSE_LOCATION at runtime (as it is a dangerous permission).

like image 72
ianhanniballake Avatar answered Sep 28 '22 07:09

ianhanniballake