Requesting multiple Bluetooth permissions in Android Marshmallow

16,543

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:

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).

Share:
16,543

Related videos on Youtube

Mirhawk
Author by

Mirhawk

Updated on June 04, 2022

Comments

  • Mirhawk
    Mirhawk almost 2 years

    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.

  • Mirhawk
    Mirhawk about 8 years
    Oh. Then do I not need to request for them? If no, how do I handle them for versions lower than Marshmallow? Also, if I needed to use other permissions like READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE , how to I request for them, since I might get the same error there?
  • ianhanniballake
    ianhanniballake about 8 years
    I've updated my answer with what I think might actually be your issue (you need the location permission for Bluetooth scanning if you target 6.0+). In any case, you can request multiple permissions at the same time (as you do), but what you can't do is call requestPermissions() more than once in a row. You should just combine your if statements and call checkSelfPermissions() on every permission you want to request and if any of them aren't granted then request them all (the system won't prompt the user to accept permissions they've already accepted).