Android 12 BLE扫描权限

详细内容可参考官方文档

Android 12 中可以不用位置权限进行蓝牙扫描了,蓝牙扫描改用新的权限:

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30"/>
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" tools:targetApi="s" />

但是!!!

扫描 Beacon 类的广播还是需要精准位置权限,就不要声明 neverForLocation 了!

以下是Android BLE扫描、连接、切断、添加蓝牙权限的代码示例: #### 添加蓝牙权限AndroidManifest.xml文件中添加以下蓝牙权限: ```xml <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.bluetooth"> <uses-feature android:name="android.hardware.bluetooth_le" android:required="true"/> <uses-permission android:name="android.permission.BLUETOOTH"/> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <application ... </application> </manifest> ``` #### 扫描BLE设备 ```java private BluetoothAdapter bluetoothAdapter; private BluetoothLeScanner bluetoothLeScanner; private ScanCallback scanCallback; // 初始化蓝牙适配器和扫描回调 bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); scanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); // 处理扫描结果 } @Override public void onScanFailed(int errorCode) { super.onScanFailed(errorCode); // 处理扫描失败 } }; // 开始扫描BLE设备 ScanFilter scanFilter = new ScanFilter.Builder() .setDeviceName("MyDevice") .build(); List<ScanFilter> scanFilters = new ArrayList<>(); scanFilters.add(scanFilter); ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_POWER) .build(); bluetoothLeScanner.startScan(scanFilters, scanSettings, scanCallback); ``` #### 连接BLE设备 ```java private BluetoothGatt bluetoothGatt; // 连接BLE设备 BluetoothDevice device = bluetoothAdapter.getRemoteDevice(deviceAddress); bluetoothGatt = device.connectGatt(this, false, gattCallback); // 处理连接回调 private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_CONNECTED) { // 连接成功 bluetoothGatt.discoverServices(); } else { // 连接失败 bluetoothGatt.close(); } } @Override public void onServicesDiscovered(BluetoothGatt gatt, int status) { super.onServicesDiscovered(gatt, status); // 处理服务发现结果 } @Override public void onCharacteristicRead(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicRead(gatt, characteristic, status); // 处理读取特征值结果 } @Override public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) { super.onCharacteristicWrite(gatt, characteristic, status); // 处理写入特征值结果 } @Override public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic) { super.onCharacteristicChanged(gatt, characteristic); // 处理特征值变化通知 } }; ``` #### 切断BLE设备 ```java // 断开BLE设备连接 bluetoothGatt.disconnect(); // 处理断开连接回调 private final BluetoothGattCallback gattCallback = new BluetoothGattCallback() { @Override public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) { super.onConnectionStateChange(gatt, status, newState); if (newState == BluetoothProfile.STATE_DISCONNECTED) { // 断开连接成功 bluetoothGatt.close(); } } }; ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值