Android12中蓝牙权限适配

AndroidManifest.xml










代码

//获取蓝牙适配器
public static BluetoothAdapter mBluetoothAdapter = null;
    //蓝牙状态监听广播
    public class BlueToothReceiver extends BroadcastReceiver {
        @Override
        public void onReceive(Context context, Intent intent) {
            switch (intent.getAction()) {
                case BluetoothAdapter.ACTION_STATE_CHANGED:
                    int blueState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, 0);
                    switch (blueState) {
                        case BluetoothAdapter.STATE_TURNING_ON:
                            Log.e("onReceive", "---------蓝牙正在打开中");
                            break;
                        case BluetoothAdapter.STATE_ON:
                            Log.e("onReceive", "---------蓝牙已经打开");
                            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
                            break;
                        case BluetoothAdapter.STATE_TURNING_OFF:
                            Log.e("onReceive", "---------蓝牙正在关闭中");
                            break;
                        case BluetoothAdapter.STATE_OFF:
                            Log.e("onReceive", "---------蓝牙已经关闭");
                            break;
                    }
                    break;
            }
        }
    }
    private IntentFilter makeFilter() {
        IntentFilter filter = new IntentFilter();
        filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
        return filter;
    }
    //Android12蓝牙权限申请
    private boolean bluePermission(){
    //compileSdkVersion项目中编译SDK版本大于30申请以下权限可使用
    //Manifest.permission.BLUETOOTH_SCAN、Manifest.permission.BLUETOOTH_ADVERTISE、Manifest.permission.BLUETOOTH_CONNECT
    //若小于30可以直接使用权限对应的字符串
        if (Build.VERSION.SDK_INT>30){
            if (ContextCompat.checkSelfPermission(this,
                    "android.permission.BLUETOOTH_SCAN")
                    != PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this,
                    "android.permission.BLUETOOTH_ADVERTISE")
                    != PERMISSION_GRANTED
                    || ContextCompat.checkSelfPermission(this,
                    "android.permission.BLUETOOTH_CONNECT")
                    != PERMISSION_GRANTED){
                ActivityCompat.requestPermissions(this,new String[]{
                                "android.permission.BLUETOOTH_SCAN",
                                "android.permission.BLUETOOTH_ADVERTISE",
                                "android.permission.BLUETOOTH_CONNECT"}, 1);
                return false;
            }
        }
        return true;
    }
    private void openBlueTooth(){
        if (bluePermission())
            mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        else
            return;
        BlueToothReceiver btlr = new BlueToothReceiver();
        this.registerReceiver(btlr, makeFilter());
        if (!mBluetoothAdapter.isEnabled()) {// 判断是否打开蓝牙
            showHandlerToast("请先开启蓝牙!");
            //弹出对话框提示用户是后打开
            Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
            //startActivityForResult(intent, SEARCH_CODE);
            // 不做提示,强行打开
            mBluetoothAdapter.enable();
        }
    }

说明:

必须按照这两步申请蓝牙权限,首先在androidManifest.xml中配置蓝牙权限,Android11及以下android.permission.BLUETOOTH、android.permission.BLUETOOTH_ADMIN申请这两个,Android12中蓝牙权限进行新增android.permission.BLUETOOTH_SCAN、android.permission.BLUETOOTH_ADVERTISE、android.permission.BLUETOOTH_CONNECT使用这三个;其次在代码中需要动态申请Android12的蓝牙权限,测试发现Android12中动态申请权限后则蓝牙直接开启,通过对蓝牙状态的监听获取蓝牙适配器:mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();

注意:不管是Android12还是Android12以下不打开蓝牙都是无法获取蓝牙适配器:mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();打开蓝牙则可以获取到蓝牙适配器,但若没有申请蓝牙所需权限,蓝牙功能也是无法使用!!!二者缺一不可

你可能感兴趣的:(蓝牙权限,Bluetooth,Android12,maxSdkVersion,Broadcast)