android:安卓12申请蓝牙权限

 我的sdk版本如下,测试用的redme note 11T pro安卓版本13

compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
    applicationId "xxxx
    minSdkVersion 19
    targetSdkVersion 30
    versionCode 1
    versionName "1.0"
    multiDexEnabled true
    testInstrumentationRunner 'androidx.test.runner.AndroidJUnitRunner'
}
dependencies {
    classpath 'com.android.tools.build:gradle:4.0.1'

    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}
#Mon Jan 09 09:50:14 CST 2023
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME

找了几个都是爆红的,可能不适配

找到一个可以用的

清单文件

 

    
    
    
    
    

    
    
    

MainActivity

//获取蓝牙适配器
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();
        }
    }

 

你可能感兴趣的:(android)