在 Android API 级别 31 上,Google 引入了对受保护的 API 的更严格限制。BleManager 使用了几个受保护的 API,因此在这个级别上无法正常工作。最好的解决方法是升级 BleManager 版本到最新版本,该版本已包含对这些变更的解决方案。如果无法升级 BleManager,则需要手动更改代码,替换受保护的 API 以获得更高的兼容性。
代码示例:
// 获取 BluetoothAdapter 对象
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
// 开启扫描
bluetoothAdapter.getBluetoothLeScanner().startScan(scanCallback);
// 获取 BluetoothAdapter 对象
BluetoothManager bluetoothManager = (BluetoothManager) getSystemService(Context.BLUETOOTH_SERVICE);
BluetoothAdapter bluetoothAdapter = bluetoothManager.getAdapter();
// 使用 BluetoothLeScannerCompat 开始扫描
BluetoothLeScannerCompat scanner = BluetoothLeScannerCompat.getScanner();
scanner.startScan(scanCallback);
上一篇:BLE连接无法配对
下一篇:BLEMesh多个配置器和子网