我有一个Android应用程序,每60秒扫描一次BT设备。我在三个相同的设备中测试相同的应用程序(Moto X Play,2个使用Lollipop,1个使用Marshmallow),但是,在其中一个Lollipop手机中,startDiscovery()方法在几分钟正常运行后停止检测设备。我知道因为这个调试行每分钟打印一次:"Bluetooth is starting a new discovery"
并且都没有收到BluetoothAdapter.ACTION_DISCOVERY_STARTED
或BluetoothAdapter.ACTION_DISCOVERY_FINISHED
个事件
这是负责收听BT事件的BroadcastReceiver的onReceive方法内的代码:
if (intent.getAction().equals(ACTION_AWARE_BLUETOOTH_REQUEST_SCAN)) {
if (bluetoothAdapter == null) {
Log.d(TAG, "Bluetooth adapter is not present");
} else if (!bluetoothAdapter.isEnabled()) {
//Bluetooth is off
Log.d(TAG, "Bluetooth adapter is turned off...");
}else{
if (bluetoothAdapter.isDiscovering()) {
Log.d(TAG, "Bluetooth adapter was discovering, cancelling...");
if(bluetoothAdapter.cancelDiscovery()){
Log.d(TAG, "Bluetooth adapter cancelled a previous discovery");
} else {
Log.d(TAG, "Bluetooth adapter could not cancel a previous discovery");
}
}
if (bluetoothAdapter.startDiscovery()) {
Log.d(TAG, "Bluetooth adapter is starting a new discovery");
} else {
Log.d(TAG, "Bluetooth adapter error on starting a new discovery");
}
}
}
我正在我的服务的onCreate方法中注册这样的BT动作:
IntentFilter filter = new IntentFilter();
filter.addAction(Bluetooth.ACTION_AWARE_BLUETOOTH_REQUEST_SCAN);
filter.addAction(BluetoothDevice.ACTION_FOUND);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_FINISHED);
filter.addAction(BluetoothAdapter.ACTION_DISCOVERY_STARTED);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(bluetoothMonitor, filter);
Intent backgroundService = new Intent(ACTION_AWARE_BLUETOOTH_REQUEST_SCAN);
bluetoothScan = PendingIntent.getBroadcast(this, 0, backgroundService, PendingIntent.FLAG_UPDATE_CURRENT);
非常奇怪,有时我会在logcat中得到这个奇怪的输出:
03-01 16:57:31.768 1103-1300/? V/AlarmManager: sending alarm {16adb330 type 0 *walarm*:ACTION_AWARE_BLUETOOTH_REQUEST_SCAN}
03-01 16:57:31.788 21145-21145/? D/AWARE::Bluetooth: Bluetooth is starting a new discovery
03-01 16:57:31.802 1103-1103/? V/AlarmManager: done {16adb330, *walarm*:ACTION_AWARE_BLUETOOTH_REQUEST_SCAN} [40ms]
03-01 17:00:31.852 1103-1300/? V/AlarmManager: sending alarm {16adb330 type 0 *walarm*:ACTION_AWARE_BLUETOOTH_REQUEST_SCAN}
03-01 17:00:31.879 21145-21145/? D/AWARE::Bluetooth: Bluetooth is starting a new discovery
03-01 17:00:31.884 1103-1103/? V/AlarmManager: done {16adb330, *walarm*:ACTION_AWARE_BLUETOOTH_REQUEST_SCAN} [40ms]
03-01 17:00:46.187 3573-3630/? D/BluetoothGatt: cancelOpen() - device: 80:E4:DA:71:B5:13
03-01 17:00:46.189 3573-3630/? D/BluetoothGatt: cancelOpen() - device: 80:E4:DA:71:BC:69
03-01 17:00:46.191 3573-3630/? D/BluetoothGatt: cancelOpen() - device: 80:E4:DA:71:B4:C8
03-01 17:00:46.193 3573-3630/? D/BluetoothGatt: cancelOpen() - device: 80:E4:DA:71:A4:61
03-01 18:58:34.548 2017-10585/? D/BluetoothAdapter: startLeScan(): null
03-01 18:58:37.823 2017-10585/? D/BluetoothAdapter: stopLeScan()
03-01 18:58:37.826 2017-10585/? D/BluetoothLeScanner: could not find callback wrapper
03-01 18:58:40.375 2017-10585/? D/BluetoothAdapter: startLeScan(): null
03-01 18:58:44.408 2017-10585/? D/BluetoothAdapter: stopLeScan()
03-01 18:58:44.411 2017-10585/? D/BluetoothLeScanner: could not find callback wrapper
03-01 18:58:44.579 2017-10585/? D/BluetoothAdapter: startLeScan(): null
03-01 18:58:46.589 2017-10585/? D/BluetoothAdapter: stopLeScan()
03-01 18:58:46.591 2017-10585/? D/BluetoothLeScanner: could not find callback wrapper