Android 11及以下三星设备BLE扫描无法检测指定TEMP设备
Android 11及以下三星设备无法扫描特定BLE设备"TEMP"的解决方案
问题概述
开发的Android应用存在BLE扫描异常:在Android 11及以下的三星设备(以三星A50,蓝牙5.0为例)上,无法检测到名为"TEMP"的BLE设备,但该设备在Android 12及以上设备、其他品牌设备上扫描正常。应用逻辑为:先通过BLE广播数据,再启动扫描监听目标设备。
相关代码片段
负责BLE扫描的BleModule模块中ReceiveDataRedDot方法:
@ReactMethod void ReceiveDataRedDot() { BluetoothAdapter bluetoothAdapter = BluetoothAdapter.getDefaultAdapter(); if (bluetoothAdapter != null) { BluetoothLeScanner bluetoothLeScanner = bluetoothAdapter.getBluetoothLeScanner(); if (bluetoothLeScanner != null) { if (bluetoothAdapter.isEnabled()) { if (scanStarted == false) { Log.d("BLEEEEE", "Receiver"); // Create a ScanFilter for your target device ScanFilter filter = new ScanFilter.Builder().setDeviceName("TEMP").build(); // Create a ScanSettings object ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build(); try { // Start filtered scanning bluetoothLeScanner.startScan( Collections.singletonList(filter), settings, leScanCallback); } catch (Exception e) { Log.d("BLEEEE", e + ""); } } scanStarted = true; } else { IS_RX_ENABLE_UNTREATED = true; } } } }
已排查内容
- 权限:已在
AndroidManifest.xml中声明BLUETOOTH、BLUETOOTH_ADMIN、ACCESS_FINE_LOCATION权限 - 设备设置:检查过三星专属的省电模式、开发者选项等可能影响BLE扫描的设置
- 日志:未发现扫描过程中的错误或警告
解决方案
1. 避免仅依赖系统级设备名称过滤
三星Android 11及以下系统对ScanFilter的设备名称过滤兼容性较差,建议改为无过滤扫描,在回调中自行判断设备名称:
// 修改扫描启动代码,去掉过滤规则 bluetoothLeScanner.startScan(null, settings, leScanCallback); // 在ScanCallback中手动筛选目标设备 private ScanCallback leScanCallback = new ScanCallback() { @Override public void onScanResult(int callbackType, ScanResult result) { super.onScanResult(callbackType, result); BluetoothDevice device = result.getDevice(); // 注意处理device.getName()为null的情况 if (device != null && "TEMP".equals(device.getName())) { // 处理目标设备逻辑 } } };
2. 调整扫描参数适配三星设备
修改ScanSettings,禁用硬件批处理并设置报告延迟为0,避免系统过滤失效:
ScanSettings settings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setReportDelay(0) .setUseHardwareBatchingIfSupported(false) // 禁用硬件批处理,提升扫描兼容性 .build();
3. 确保权限与位置服务正常
- Android 10及以下设备需动态申请
ACCESS_FINE_LOCATION权限,且必须开启系统位置服务(三星旧机型强制要求位置服务开启才能扫描BLE设备) - 确认权限申请流程完整,用户已授予相关权限
4. 检查目标设备广播包规范
确认"TEMP"设备的BLE广播包中设备名称无隐藏字符、编码问题,且长度符合BLE规范(广播名称最大为16字节),避免三星旧系统无法正确解析名称。
内容的提问来源于stack exchange,提问作者Md Afzaal Khan
相关产品推荐
相关产品推荐

