在Android Marshmallow中请求多个蓝牙权限 [英] Requesting multiple Bluetooth permissions in Android Marshmallow

查看:98
本文介绍了在Android Marshmallow中请求多个蓝牙权限的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发具有连接性的应用程序,该应用程序可通过SDK 23连接到蓝牙设备.我在请求蓝牙多个权限时遇到问题.这是我到目前为止所做的:

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

尽管我能够获得用于请求启用蓝牙的对话框,但我没有获得第二个许可,即无法连接到设备.在logcat中,我得到:

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

由于我无法连接到设备,因此我只能被卡在这里.并且此代码在Lollipop之前的Android版本上都可以正常运行,只是在棉花糖版本上引起问题.

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.

推荐答案

BLUETOOTHBLUETOOTH_ADMIN危险权限表中的权限在运行时.

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.

但是,如中所述Android 6.0的更改:访问硬件标识符:

要通过蓝牙和Wi-Fi扫描访问附近的外部设备的硬件标识符,您的应用现在必须具有 ACCESS_COARSE_LOCATION 权限:

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

如果使用这些方法中的任何一种,则需要在运行时至少请求ACCESS_COARSE_LOCATION(因为 是危险的权限).

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

这篇关于在Android Marshmallow中请求多个蓝牙权限的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆