Android 多次 connection/disconnection 尝试使用 BLE 外围设备时设备崩溃

Android device crash on multiple connection/disconnection attempts with BLE peripheral

当我尝试多次连接和断开 BLE 外围设备(中间有几秒钟的延迟)时,我的应用程序崩溃了。仔细检查是因为设备的蓝牙服务崩溃了。这是来自 logcat:

的相关日志行
2022-03-21 12:54:08.591 2161-2210/? E/bt_hci: Ctlr H/w error event - code:0x21
2022-03-21 12:54:08.591 2161-2210/? I/bt_stack: [INFO:  .cc(323)] clear_l2cap_whitelist: Clearing whitelist from l2cap channel. conn_handle=512 cid=4:4
2022-03-21 12:54:08.591 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=3 connected=0 conn_id=0x0003 reason=0x0003
2022-03-21 12:54:08.591 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=4 connected=0 conn_id=0x0004 reason=0x0003
2022-03-21 12:54:08.591 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=5 connected=0 conn_id=0x0005 reason=0x0003
2022-03-21 12:54:08.591 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=6 connected=0 conn_id=0x0006 reason=0x0003
2022-03-21 12:54:08.592 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=7 connected=0 conn_id=0x0007 reason=0x0003
2022-03-21 12:54:08.592 2161-2210/? W/bt_stack: [WARNING:bta_gattc_act.cc(1043)] bta_gattc_conn_cback: cif=8 connected=0 conn_id=0x0008 reason=0x0003
2022-03-21 12:54:08.593 2161-2743/? I/bt_stack: [INFO:message_loop_thread.cc(175)] Run: message loop starting for thread bt_module_lifecycle_thread
2022-03-21 12:54:08.593 2161-2743/? I/bt_core_module: module_start_up Starting module "controller_module"
2022-03-21 12:54:08.593 2161-2190/? E/bt_btif: Received H/W Error. 
2022-03-21 12:54:08.596 2161-2190/? I/bt_stack: [INFO:btif_config.cc(647)] hash_file: Disabled for multi-user
2022-03-21 12:54:08.597 2161-2190/? I/bt_stack: [INFO:btif_config.cc(689)] write_checksum_file: Disabled for multi-user, since config changed removing checksums.
2022-03-21 12:54:08.598 2161-2743/? I/bt_log_fw_log_switch: Start: skip to enable legacy FW log feature
2022-03-21 12:54:08.599 2161-2210/? I/bt_stack: [INFO:btu_hcif.cc(1160)] read_encryption_key_size_complete_after_encryption_change: disconnecting, status: 0x02
2022-03-21 12:54:08.615 2161-2743/? I/bt_core_module: module_start_up Started module "controller_module"
2022-03-21 12:54:08.615 2161-2743/? I/bt_stack: [INFO:message_loop_thread.cc(196)] Run: message loop finished for thread bt_module_lifecycle_thread
2022-03-21 12:54:08.615 2161-2209/? W/bt_btm: btm_decode_ext_features_page: feature page 1 ignored
2022-03-21 12:54:08.616 2161-2209/? W/bt_btm: btm_decode_ext_features_page: feature page 2 ignored
2022-03-21 12:54:08.616 2161-2209/? I/bt_stack: [INFO:message_loop_thread.cc(87)] ShutDown: thread bt_module_lifecycle_thread(-1) is already stopped
2022-03-21 12:54:08.730 395-6284/? E/mtk.hal.bt@1.0-impl: BluetoothDeathRecipient::serviceDied - Bluetooth service died

这种情况只发生在我测试过的两台设备上(都是 Android 10)。例如,它不会出现在三星 Android 11 设备上。

通过几个 connection/disconnection 周期后重新启动蓝牙服务,我无法再次遇到此问题。但是,此解决方法不利于客户的使用,因为他们会失去与当前使用的其他 BT 设备的连接。

我的问题是如何着手了解此问题的根本原因?这完全是我无能为力的设备硬件问题吗?或者我的方法有什么可以改变的吗?

如果蓝牙堆栈在 phone 中崩溃,那么它很可能是蓝牙堆栈中的错误,您无能为力,只能向 phone 制造商发送错误报告.

不过,您可以尝试查看是否可以找出在何种情况下会发生这种情况,并执行一些解决方法来避免这种情况。 hci snoop 日志与 logcat 结合使用可能会有所帮助。

无论如何,如果您正确地编写了您的应用程序,它不应该仅仅因为蓝牙堆栈崩溃而自行崩溃。当蓝牙堆栈稍后重新启动时,您应该能够恢复。请参阅 How to detect Bluetooth state change using a broadcast receiver? 如何获得蓝牙状态更改的通知。