我正在开发一个严重依赖蓝牙连接的 Android 应用程序。我实现了一个 BroadcastReceiver 来侦听蓝牙状态的变化,特别是监视 BluetoothAdapter.ACTION_STATE_CHANGED 意图来检测蓝牙适配器何时关闭或已关闭。
这是我的代码片段:
private final BroadcastReceiver bluetoothStateReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
final int state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_TURNING_OFF:
// Handle Bluetooth turning off
handleBluetoothTurningOff();
break;
case BluetoothAdapter.STATE_OFF:
// Handle Bluetooth being off
handleBluetoothOff();
break;
}
}
}
};
我已使用 ADB 捕获日志,以确保设备没有发出所需的 Intent:
08-12 14:28:10.369 5868 5868 D BluetoothGatt: connect() - device: 00091F_A, auto: false
08-12 14:28:10.370 5868 5868 D BluetoothGatt: registerApp()
08-12 14:28:10.371 5868 5868 D BluetoothGatt: registerApp() - UUID=fc7486f2-6fe9-4101-a439-d6f0498067d1
08-12 14:28:10.383 5868 5901 D BluetoothGatt: onClientRegistered() - status=0 clientIf=5
08-12 14:28:15.421 5868 5901 D BluetoothGatt: onClientConnectionState() - status=133 clientIf=5 device=00091F_A
08-12 14:28:15.444 5868 5901 D BluetoothGatt: close()
08-12 14:28:15.451 5868 5901 D BluetoothGatt: unregisterApp() - mClientIf=5
08-12 14:28:21.162 13313 13438 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.163 1624 1639 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.163 2970 2990 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.163 5868 16206 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.164 1270 1431 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.164 3258 6357 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.165 1957 3863 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.165 1605 5864 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.166 13001 13024 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.166 13026 16208 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.185 13313 13438 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 1605 5864 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 1270 1431 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 5868 16206 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 2970 2990 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 3258 6357 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 13001 13024 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.186 1624 1639 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.187 13026 16208 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.187 1957 4130 D BluetoothAdapter: onBluetoothServiceDown
08-12 14:28:21.188 1957 4556 I BluetoothAdapter: BluetoothAdapter() : com.google.android.gms
08-12 14:28:21.212 1957 4790 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.215 1957 4790 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.215 1957 4790 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.217 1270 1270 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.228 1270 1270 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.244 1270 1270 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.245 1270 1270 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.246 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.246 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.253 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.253 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.253 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.253 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.253 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.253 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.253 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.253 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.253 5868 5868 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.253 5868 5868 D BluetoothLeScanner: stop scan is not allowed as BT is off
08-12 14:28:21.267 1957 4790 I BluetoothAdapter: STATE_OFF=10
08-12 14:28:21.408 1957 1957 I BluetoothAdapter: BluetoothAdapter() : com.google.android.gms
08-12 14:28:22.006 16228 16228 I BluetoothAdapter: BluetoothAdapter() : com.android.bluetooth
08-12 14:28:22.204 13313 13832 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@687270d
08-12 14:28:22.204 1605 5864 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@c42db05
08-12 14:28:22.204 2970 8098 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@9f044fb
08-12 14:28:22.205 5868 15214 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@c313804
08-12 14:28:22.205 1270 1431 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@75b5a99
08-12 14:28:22.205 1624 1639 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@8eff7df
08-12 14:28:22.206 1957 3863 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@5e00d03
08-12 14:28:22.206 3258 4734 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@5004e05
08-12 14:28:22.208 13001 13024 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@e28ae0a
08-12 14:28:22.208 13026 13306 D BluetoothAdapter: onBluetoothServiceUp: android.bluetooth.IBluetooth$Stub$Proxy@3de93fe
08-12 14:28:22.209 16228 16240 D BluetoothAdapter: onBluetoothServiceUp: com.android.bluetooth.btservice.AdapterService$AdapterServiceBinder@dd932f7
08-12 14:28:22.246 1270 1270 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.274 1957 4790 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.731 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.731 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.731 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.731 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.735 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.735 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.735 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.735 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.739 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.740 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.740 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.740 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.743 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.743 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.743 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.743 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.746 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.746 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.746 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.746 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.747 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.747 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.747 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.747 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.748 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.748 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.748 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.748 1624 1773 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.749 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.749 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.749 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.749 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.755 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.755 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.756 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.756 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.765 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.765 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.765 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.765 13026 13026 I BluetoothAdapter: STATE_OFF=14
08-12 14:28:22.774 13026 13026 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.775 13026 13026 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.775 13026 13026 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.775 13026 13026 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.782 1270 1270 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.782 1270 1270 I BluetoothAdapter: appoCount is = 1
08-12 14:28:22.783 1270 1270 I BluetoothAdapter: STATE_BLE_ON
08-12 14:28:22.783 1270 1270 I BluetoothAdapter: appoCount is = 1 G/S = 1/0
08-12 14:28:22.787 13026 13026 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.788 13026 13026 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.789 13026 13026 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.789 13026 13026 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.797 1270 1270 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.818 1270 1270 I BluetoothAdapter: STATE_TURNING_ON
08-12 14:28:22.886 16228 16228 I BluetoothAdapter: BluetoothAdapter() : com.android.bluetooth
08-12 14:28:23.041 16228 16228 I BluetoothAdapter: BluetoothAdapter() : com.android.bluetooth
08-12 14:28:23.058 16228 16228 D BluetoothAdapter: listenUsingL2capOn: set assigned channel to 4097
08-12 14:28:23.296 1957 4790 I BluetoothAdapter: STATE_TURNING_ON
在大多数情况下,此设置可以按预期工作。但是,我注意到有时蓝牙适配器根本不发出 STATE_TURNING_OFF 意图。这会导致我的应用程序没有收到蓝牙正在关闭的通知,因此无法做出适当的反应(例如,通过清理资源或通知用户)。
这种行为似乎不一致,而且我无法识别不发出意图的模式或特定条件。
我的问题:
我考虑过添加后备机制,例如定期轮询BluetoothAdapter状态,但我正在寻求更好地理解根本原因以及是否有更强大的解决方案。
任何见解或建议将不胜感激!
我认为发生这种情况是因为这种状态的快速/临时性质,该状态可能非常短暂或完全被跳过,具体取决于硬件或 Android 蓝牙堆栈实现。我建议在不同的硬件上测试您的代码,看看行为是否匹配,但无论如何,我的建议是避免使用这种方法,因为您似乎正在正确地做事,但它仍然没有按预期工作。相反,请考虑使用 STATE_OFF 意图而不是 STATE_TURNING_OFF,因为它更可靠,或者使用检查 BluetoothAdapter 状态的后备机制。
下面是一些其他人过去如何实现此功能的链接,以防您还没有看到它们:-