我在三星S8,Android 7.0上发现了一个问题(更新:这个问题也发生在Android 7.0上的三星S7和Nexus 5x上),经过几次测试后得出结论,该应用程序扫描得太频繁:
08-14 12:44:20.693 25329-25329/com.my.app D/BluetoothAdapter: startLeScan(): null
08-14 12:44:20.695 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.696 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.698 25329-25329/com.my.app D/BluetoothLeScanner: Start Scan
08-14 12:44:20.699 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.700 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.700 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.701 25329-25329/com.my.app D/BluetoothAdapter: STATE_ON
08-14 12:44:20.703 4079-4093/? D/BtGatt.GattService: registerClient() - UUID=dbaafee1-caf1-4482-9025-b712f000eeab
08-14 12:44:20.807 4079-4204/? D/BtGatt.GattService: onClientRegistered() - UUID=dbaafee1-caf1-4482-9025-b712f000eeab, clientIf=5, status=0
08-14 12:44:20.808 25329-25342/com.my.app D/BluetoothLeScanner: onClientRegistered() - status=0 clientIf=5 mClientIf=0
08-14 12:44:20.809 4079-7185/? D/BtGatt.GattService: start scan with filters
08-14 12:44:20.811 4079-7185/? D/BtGatt.GattService: getScanSettings
08-14 12:44:20.812 4079-7185/? D/BtGatt.GattService: Is it foreground application = true
08-14 12:44:20.812 4079-7185/? D/BtGatt.GattService: not a background application
08-14 12:44:20.817 4079-7185/? E/BtGatt.GattService: App 'com.my.app' is scanning too frequently
问题明显出现在那6个STATE_ON呼叫结果上,这是未记录的BLE行为变更的一部分,首次在DP4版本说明中提到:
我们从DP4开始改变了BLE扫描行为。我们将防止应用程序在30秒内启动和停止扫描超过5次。对于长时间运行的扫描,我们将把它们转换为机会式扫描。
我不理解的是,即使我设置了: ScanSettings.setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC)
,仍然存在在30秒内进行6次以上的扫描。
代码如下:
List<ScanFilter> filters = new ArrayList<>();
ScanSettings scanSettings = new ScanSettings.Builder()
.setScanMode(ScanSettings.SCAN_MODE_OPPORTUNISTIC)
.build();
bluetoothAdapter.getBluetoothLeScanner().startScan(filters, scanSettings, recoderScanCallback);
//events from the log happen before this log is printed
Log.i("test", " started!");
return recoderScanCallback.scanResultObservable().map((ScanResult record) -> {
//never gets here
Log.i("test", " result!");
});
RecorderScanCallback
继承自ScanCallback
。
由于我们的代码即将冻结并且我们使用lib的1.1.0版本,因此我们无法使用RxAndroidBle#rxBleClient.scanBleSettings
(ScanSettings)。
为什么ScanSettings.setScanMode
不会改变搜索结果?
startScan
,又如何依赖其他应用程序开始扫描呢?再次强调,在文档中:一种特殊的蓝牙LE扫描模式。使用此扫描模式的应用程序将被动地监听其他扫描结果,而不会启动BLE扫描本身。 - Margarita Litkevych