Android BLE扫描无法发现Saveo C200 LF设备问题咨询
我正在开发的Android应用中实现了BLE设备发现与连接功能,该功能可正常发现部分BLE设备并完成配对、通信,但Saveo C200 LF设备始终无法出现在扫描结果中。我使用Google Play上的BLE Scanner应用扫描时,该设备可立即被发现并正常连接、获取服务。我的扫描逻辑未设置任何过滤规则,但无论如何操作都无法发现该设备,以下是相关代码实现:
private BLEScanCallback bleScanCallback = new BLEScanCallback(); public void bleStartBluetoothDiscovery() { BluetoothAdapter adapter = getBluetoothAdapter(); if (adapter != null) { // for bluetooth le bleScanner = adapter.getBluetoothLeScanner(); if (bleScanner != null) { // run the discovery for 12 seconds and then automatically stop bleScheduleEndDiscovery(12); List<ScanFilter> filters = null; ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .build() ; bleScanner.startScan(filters, scanSettings, this.bleScanCallback); } } } private static class BLEScanCallback extends ScanCallback { @Override public void onScanResult(int callbackType, ScanResult result) { BluetoothDevice device = result.getDevice(); // the address of the Saveo C200 device, got it from the // 3rd party BLE Scanner app that is able to see it when scanning. String crfidscanneraddress = "DD:0D:30:5E:1A:AC"; String address = device.getAddress(); String deviceName = device.getName(); deviceName = (deviceName == null) ? "Unknown" : deviceName; if (address.equals(crfidscanneraddress)) { Log.d(TAG, "onScanResult: FOUND IT!!! ---> " + deviceName + " [" + address + "] "); // I'm not just relying on this log, I'm checking through // all of the results and my device is not found even // with a different address. } Log.d(TAG, "onScanResult: " + deviceName + " [" + address + "] "); } // ... etc }
补全蓝牙权限配置:Android 12+需声明并动态请求
BLUETOOTH_SCAN权限,若无需位置权限,需在权限中添加android:usesPermissionFlags="neverForLocation";Android 11及以下需ACCESS_COARSE_LOCATION或ACCESS_FINE_LOCATION权限,权限缺失会导致部分BLE设备无法被扫描到。优化扫描设置参数:当前扫描设置仅指定了低延迟模式,补充以下配置可提升设备捕获率:
ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setCallbackType(ScanSettings.CALLBACK_TYPE_ALL_MATCHES) .setMatchMode(ScanSettings.MATCH_MODE_AGGRESSIVE) .setNumOfMatches(ScanSettings.MATCH_NUM_MAX_ADVERTISEMENT) .build();这些参数会让扫描逻辑更积极,更容易捕获广播频率低、信号弱的BLE设备。
处理批量扫描结果:仅重写
onScanResult会漏掉系统批量返回的扫描结果,需补充实现onBatchScanResults方法:@Override public void onBatchScanResults(List<ScanResult> results) { for (ScanResult result : results) { BluetoothDevice device = result.getDevice(); String address = device.getAddress(); String deviceName = device.getName() == null ? "Unknown" : device.getName(); Log.d(TAG, "onBatchScanResults: " + deviceName + " [" + address + "] "); if (address.equals("DD:0D:30:5E:1A:AC")) { Log.d(TAG, "onBatchScanResults: FOUND IT!!! ---> " + deviceName + " [" + address + "] "); } } }支持扩展广播类型:Saveo C200 LF可能使用扩展广播,默认扫描设置不支持该类型,Android 8.0+可添加以下配置开启支持:
ScanSettings scanSettings = new ScanSettings.Builder() .setScanMode(ScanSettings.SCAN_MODE_LOW_LATENCY) .setLegacy(false) // 启用非传统扫描,适配扩展广播 .build();规避后台扫描限制:Android对后台BLE扫描有严格限制,测试时确保应用处于前台;若需后台扫描,Android 10+需额外申请
ACCESS_BACKGROUND_LOCATION权限。
内容的提问来源于stack exchange,提问作者Kenny Wyland

