5

所以我一直无法弄清楚是什么导致了这个问题或如何解决它。我正在开发一个应用程序,它整天都与 BLE 外围设备保持连接,并从设备上的传感器收集数据。有时,设备会断开连接,当我查看 logcat 时,我只看到重新连接失败并且 BluetoothGattCallback onConnectionStateChange 回调获得 133 状态,这将只是循环。杀死应用程序,关闭和打开手机蓝牙,清除蓝牙缓存都是我们试图让设备重新连接的所有事情。我像这样连接到主线程上的设备:

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mBluetoothGatt = mBluetoothDevice.connectGatt(mContext,
                autoConnect, mBleGattCallback, TRANSPORT_LE);
    } else {
        mBluetoothGatt = mBluetoothDevice.connectGatt(mContext,
                autoConnect, mBleGattCallback);
    }

此代码位于自定义设备对象中(基本上是外围地址的包装器),如果我仍然有一个实例可以再次连接,我会重用该对象。我目前正在尝试使用 BluetoothAdapter.getRemoteDevice 来获取一个新的 BluetoothDevice 实例来创建一个新的自定义设备对象并调用上面的代码。onConnectionStateChange 看起来像这样:

    @Override
    public void onConnectionStateChange(BluetoothGatt gatt, int status, int newState) {
        if (status == BluetoothGatt.GATT_SUCCESS && newState == BluetoothGatt.STATE_CONNECTED) {
             final boolean success = gatt.discoverServices();
             if (!success) {
                disconnect();
                broadcastDisconnected();
             }

        } else {
             try {
                // https://code.google.com/p/android/issues/detail?id=183108
                gatt.close();
                disconnect();
             } catch (Exception e) {
                Logger.d("close ignoring: " + e);
             }
             broadcastDisconnected();
        }
    }

然后 disconnect 方法调用 bluetoothGatt 对象上的隐藏刷新方法,然后调用mBluetoothGatt.disconnect(),然后mBluetoothGatt.close(). 使用此代码,我测试的手机通常表现良好,但有时手机最初会因为超时而断开连接,然后只是 133。有时手机会重新连接到设备,但有时需要很多时间技巧(切换 bt,清除蓝牙共享应用程序的数据/缓存,终止应用程序等)以使设备重新连接,并且在极少数情况下,无论我尝试什么,设备都不会重新连接。我觉得奇怪的另一件事是,在三星 S6 和 S7 上,如果设备连接到我的应用程序,并且我关闭手机的蓝牙,然后再打开,设备无法重新连接,我只看到 133 状态. 当我重新启动手机并且我的启动接收器启动我的服务以连接到外围设备时,Nexus 5 上的类似行为。外围设备的日志没有 t 提供了任何见解,当返回 133 状态时,它甚至根本没有与设备通信。这是尝试重新连接并失败时的 logcat 日志片段:

03-28 18:59:27.381 8530-8530/myapp.android.dev D/CustomDevice.connect: thread: main, message: connect
03-28 18:59:27.383 8530-8530/myapp.android.dev D/BluetoothGatt: connect() - device: D8:6D:C8:C2:05:CE, auto: false
03-28 18:59:27.384 8530-8530/myapp.android.dev D/BluetoothGatt: registerApp()
03-28 18:59:27.384 8530-8530/myapp.android.dev D/BluetoothGatt: registerApp() - UUID=bd4590d1-a2d9-496c-b3c3-f1393c4a1c9b
03-28 18:59:27.388 8530-8542/myapp.android.dev D/BluetoothGatt: onClientRegistered() - status=0 clientIf=5
03-28 18:59:27.833 8530-8645/myapp.android.dev D/BluetoothGatt: onClientConnectionState() - status=133 clientIf=5 device=D8:6D:C8:C2:05:CE
03-28 18:59:27.835 8530-8645/myapp.android.dev I/CustomDevice$BleGattCallback.onConnectionStateChange: thread: Binder_3 message: GATT status :133
03-28 18:59:27.835 8530-8645/myapp.android.dev D/BluetoothGatt: close()
03-28 18:59:27.836 8530-8645/myapp.android.dev D/BluetoothGatt: unregisterApp() - mClientIf=5
03-28 18:59:27.838 8530-8645/myapp.android.dev D/CustomDevice.disconnect:  thread: Binder_3 message: disconnect
03-28 18:59:27.840 8530-8645/myapp.android.dev D/BluetoothGatt: refresh() - device: D8:6D:C8:C2:05:CE
03-28 18:59:27.840 8530-8645/myapp.android.dev D/BluetoothGatt: cancelOpen() - device: D8:6D:C8:C2:05:CE
03-28 18:59:27.841 8530-8645/myapp.android.dev D/CustomDevice.close:  thread: Binder_3 message: close
03-28 18:59:27.841 8530-8645/myapp.android.dev D/BluetoothGatt: close()
03-28 18:59:27.842 8530-8645/myapp.android.dev D/BluetoothGatt: unregisterApp() - mClientIf=0

这些日志只会循环和循环。所以寻找为什么会发生这种情况。133 表示 GATT 错误,但我应该如何以编程方式处理这个问题?我已经看到一些关于类似问题的错误报告给谷歌,但这个问题发生在我的应用程序支持的所有 android 版本上,即 5.0 及更高版本。我真的不知道该怎么处理这个问题。有时让另一个应用程序扫描蓝牙设备会让我的应用程序重新连接。仅仅通过关闭和打开蓝牙,三星手机就会陷入这个 133 错误,这真的很不幸也很烦人。我一直在尝试我能想到的一切来解决或解决这个问题,但它仍然存在。希望有人能给我一些见解。谢谢你的时间。

-编辑-

我已经更改了植根手机上的蓝牙配置文件以打印出所有蓝牙日志消息,这是从手机以 133 状态循环时开始的。但是,手机在循环出现此错误一分钟左右后最终连接到设备。但是,如果有人可以帮助理解这些,这里是蓝牙日志。我一直在放慢速度,试图查看日志来自的代码,看看我是否能弄清楚发生了什么,但绝对可以用另一双眼睛。

03-29 22:11:42.112 2802-2989/com.android.bluetooth I/bt_btm: btm_ble_set_connectability mode=0x0 combined_mode=0x1
03-29 22:11:42.120 2802-3001/com.android.bluetooth I/bt_btif: btif_dm_cancel_discovery
03-29 22:11:42.120 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x201
03-29 22:11:42.120 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_search_sm_execute state:0, event:0x201
03-29 22:11:42.121 2802-2989/com.android.bluetooth I/bt_btm: btif_dm_search_services_evt:
03-29 22:11:42.121 2802-2930/com.android.bluetooth I/bt_btif: btif_dm_search_services_evt:  event = 6
03-29 22:11:42.122 2802-2930/com.android.bluetooth E/bt_btif_dm: ### ASSERT : system/bt/main/../btif/src/btif_dm.c line 1748 unhandled search services event (6) ###
03-29 22:11:42.138 2802-2829/com.android.bluetooth D/BtGatt.GattService: registerClient() - UUID=5330faee-9ba5-4656-aee4-fab95861ebd4
03-29 22:11:42.138 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f17
03-29 22:11:42.139 2802-2989/com.android.bluetooth D/bt_btif: bta_gattc_register state 2
03-29 22:11:42.139 2802-2989/com.android.bluetooth I/bt_att: GATT_Register
03-29 22:11:42.140 2802-2989/com.android.bluetooth D/bt_att: UUID=[0x5330faee9ba54656aee4fab95861ebd4]
03-29 22:11:42.140 2802-2989/com.android.bluetooth I/bt_att: allocated gatt_if=5
03-29 22:11:42.141 2802-2989/com.android.bluetooth I/bt_btif: HAL bt_gatt_callback
03-29 22:11:42.141 2802-2989/com.android.bluetooth I/bt_att: GATT_StartIf gatt_if=5
03-29 22:11:42.141 2802-2930/com.android.bluetooth I/bt_btif: gatt_find_the_connected_bda start_idx=0
03-29 22:11:42.142 2802-2989/com.android.bluetooth D/bt_att: gatt_find_the_connected_bda start_idx=0
03-29 22:11:42.142 2802-2930/com.android.bluetooth D/BtGatt.GattService: onClientRegistered() - UUID=5330faee-9ba5-4656-aee4-fab95861ebd4, clientIf=5
03-29 22:11:42.143 2802-2989/com.android.bluetooth D/bt_att: gatt_find_the_connected_bda found=0 found_idx=20
03-29 22:11:42.144 2802-12858/com.android.bluetooth D/A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d471cca
03-29 22:11:42.145 2802-12858/com.android.bluetooth I/A2dpService: audio isMusicActive is false
03-29 22:11:42.146 2802-12858/com.android.bluetooth D/BtGatt.GattService: clientConnect() - address=D8:6D:C8:C2:05:CE, isDirect=true
03-29 22:11:42.147 2802-2930/com.android.bluetooth D/bt_btif_config: btif_get_address_type: Device [d8:6d:c8:c2:05:ce] address type 1
03-29 22:11:42.148 2802-2930/com.android.bluetooth D/bt_btif_config: btif_get_device_type: Device [d8:6d:c8:c2:05:ce] type 2
03-29 22:11:42.148 2802-2930/com.android.bluetooth D/bt_btif: BTA got event 0x112
03-29 22:11:42.148 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x112
03-29 22:11:42.149 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_sm_execute event:0x12
03-29 22:11:42.149 2802-2989/com.android.bluetooth D/bt_btm: BTM_SecAddBleDevice dev_type=0x2
03-29 22:11:42.149 2802-2989/com.android.bluetooth D/bt_btm: Device already exist
03-29 22:11:42.150 2802-2989/com.android.bluetooth I/bt_btm: BTM_InqDbRead: bd addr [d86dc8c205ce]
03-29 22:11:42.150 2802-2989/com.android.bluetooth D/bt_btm: InqDb  device_type =0x2  addr_type=0x1
03-29 22:11:42.151 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f00
03-29 22:11:42.152 2802-2989/com.android.bluetooth I/bt_att: GATT_Connect gatt_if=5
03-29 22:11:42.152 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=1
03-29 22:11:42.153 2802-2989/com.android.bluetooth E/bt_btif: Connection open failure
03-29 22:11:42.153 2802-2930/com.android.bluetooth I/bt_btif: HAL bt_gatt_callbacks->client->open_cb
03-29 22:11:42.154 2802-2930/com.android.bluetooth D/BtGatt.GattService: onConnected() - clientIf=5, connId=0, address=D8:6D:C8:C2:05:CE
03-29 22:11:42.159 2802-3080/com.android.bluetooth D/BtGatt.GattService: unregisterClient() - clientIf=5
03-29 22:11:42.160 2802-2930/com.android.bluetooth D/bt_btif: btif_obtain_multi_adv_data_cb, Count:16
03-29 22:11:42.160 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f18
03-29 22:11:42.161 2802-2989/com.android.bluetooth I/bt_att: GATT_Deregister gatt_if=5
03-29 22:11:42.162 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=1
03-29 22:11:42.162 2802-2989/com.android.bluetooth D/bt_att: gatt_update_app_use_link_flag  is_add=0 chk_link=0
03-29 22:11:42.163 2802-2989/com.android.bluetooth D/bt_att: gatt_update_app_hold_link_status found=0[1-found] idx=32 gatt_if=5 is_add=0
03-29 22:11:42.163 2802-2989/com.android.bluetooth D/bt_att: gatt_num_apps_hold_link   num=0
03-29 22:11:42.164 2802-2989/com.android.bluetooth D/bt_att: gatt_disconnect 
03-29 22:11:42.165 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=1
03-29 22:11:42.166 2802-2989/com.android.bluetooth D/bt_att: gatt_disconnect already in closing state
03-29 22:11:42.167 2802-2989/com.android.bluetooth I/bt_att: GATT_Listen gatt_if=5
03-29 22:11:42.167 2802-2989/com.android.bluetooth I/bt_btm: BTM_BleUpdateAdvFilterPolicy
03-29 22:11:42.168 2802-2989/com.android.bluetooth I/bt_btm: BTM_ReadConnectability

以下是设备成功连接时的日志,我认为可能很重要的差异在行前标有星号。

03-29 22:12:07.278 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x104
03-29 22:12:07.279 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_sm_execute event:0x4
03-29 22:12:07.279 2802-2989/com.android.bluetooth D/bt_btm: btm_get_acl_disc_reason_code
03-29 22:12:07.280 2802-2989/com.android.bluetooth W/bt_l2cap: btif_dm_upstreams_cback  ev: BTA_DM_LINK_DOWN_
03-29 22:12:07.280 2802-2930/com.android.bluetooth I/bt_btif: BTA got event 0x104
03-29 22:12:07.281 2802-2989/com.android.bluetooth I/bt_btif: BTA_DM_LINK_DOWN_EV
03-29 22:12:07.281 2802-2930/com.android.bluetooth D/bt_btif: bta_dm_sm_execute event:0x4
03-29 22:12:07.281 2802-2989/com.android.bluetooth I/bt_btif: num_active_le_links is 0 
03-29 22:12:07.281 2802-2930/com.android.bluetooth D/bt_btif: BTA got event 0x1f17
03-29 22:12:07.282 2802-2989/com.android.bluetooth I/bt_btif: btif_av_move_idle: A
03-29 22:12:07.283 2802-2989/com.android.bluetooth D/bt_btif: bta_gattc_register state 2
03-29 22:12:07.282 2802-2930/com.android.bluetooth D/bt_btif: GATT_Register
03-29 22:12:07.284 2802-2989/com.android.bluetooth I/bt_att: BTA_DM_LINK_D
03-29 22:12:07.285 2802-2930/com.android.bluetooth D/bt_btif: UUID=[0xa701255809b941c99db97751dcd19f58]
03-29 22:12:07.285 2802-2989/com.android.bluetooth D/bt_att: HAL bt_hal_cbacks->acl_state_changed_cb
03-29 22:12:07.286 2802-2930/com.android.bluetooth I/bt_btif: allocated gatt_if=6
03-29 22:12:07.286 2802-2989/com.android.bluetooth I/bt_att: allocated gatt_if=6
03-29 22:12:07.286 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f16
03-29 22:12:07.287 2802-2989/com.android.bluetooth I/bt_att: GATT_StartIf gatt_if=6
03-29 22:12:07.287 2802-2989/com.android.bluetooth D/bt_att: gatt_find_the_connected_bda start_idx=0
03-29 22:12:07.288 2802-2989/com.android.bluetooth D/bt_att: gatt_find_the_connected_bda found=0 found_idx=20
03-29 22:12:07.298 2802-2930/com.android.bluetooth E/BluetoothRemoteDevices: state12newState1
03-29 22:12:07.298 2802-2930/com.android.bluetooth D/BluetoothRemoteDevices: aclStateChangeCallback: sending ACL disconnected intent
03-29 22:12:07.299 2802-2930/com.android.bluetooth D/BluetoothRemoteDevices: aclStateChangeCallback: State:DisConnected to Device:D8:6D:C8:C2:05:CE
03-29 22:12:07.301 2802-2930/com.android.bluetooth I/bt_btif: btif_dm_upstreams_cback  ev: BTA_DM_BUSY_LEVEL_EVT
03-29 22:12:07.301 2802-2930/com.android.bluetooth I/bt_btif: HAL bt_gatt_callbacks->client->register_client_cb
03-29 22:12:07.301 2802-2930/com.android.bluetooth D/BtGatt.GattService: onClientRegistered() - UUID=a7012558-09b9-41c9-9db9-7751dcd19f58, clientIf=6
03-29 22:12:07.304 2802-2802/com.android.bluetooth D/BluetoothMapService: onReceive
03-29 22:12:07.304 2802-2802/com.android.bluetooth D/BluetoothMapService: onReceive: android.bluetooth.device.action.ACL_DISCONNECTED
03-29 22:12:07.303 2802-2830/com.android.bluetooth D/A2dpService: getA2DPService(): returning com.android.bluetooth.a2dp.A2dpService@d471cca
03-29 22:12:07.306 2802-2830/com.android.bluetooth I/A2dpService: audio isMusicActive is false
03-29 22:12:07.309 2802-2830/com.android.bluetooth D/BtGatt.GattService: clientConnect() - address=D8:6D:C8:C2:05:CE, isDirect=true
03-29 22:12:07.310 2802-2930/com.android.bluetooth D/bt_btif_config: btif_get_address_type: Device [d8:6d:c8:c2:05:ce] address type 1
03-29 22:12:07.310 2802-2930/com.android.bluetooth D/bt_btif_config: btif_get_device_type: Device [d8:6d:c8:c2:05:ce] type 2
03-29 22:12:07.311 2802-2802/com.android.bluetooth V/BluetoothPbapService: action: android.bluetooth.device.action.ACL_DISCONNECTED
03-29 22:12:07.311 2802-2930/com.android.bluetooth D/bt_btif: BTA_GATTC_Open Transport  = 2, dev type = 2
03-29 22:12:07.312 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x112
03-29 22:12:07.312 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_sm_execute event:0x12
03-29 22:12:07.313 2802-2989/com.android.bluetooth D/bt_btm: BTM_SecAddBleDevice dev_type=0x2
03-29 22:12:07.313 2802-2989/com.android.bluetooth D/bt_btm: Device already exist
03-29 22:12:07.313 2802-2989/com.android.bluetooth I/bt_btm: BTM_InqDbRead: bd addr [d86dc8c205ce]
03-29 22:12:07.314 2802-2989/com.android.bluetooth D/bt_btm: InqDb  device_type =0x2  addr_type=0x1
03-29 22:12:07.314 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f00
**03-29 22:12:07.315 2802-2989/com.android.bluetooth I/bt_att: GATT_Connect gatt_if=6
**03-29 22:12:07.315 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=0
**03-29 22:12:07.316 2802-2989/com.android.bluetooth D/bt_att: gatt_set_ch_state: old=0 new=2
03-29 22:12:07.316 2802-2989/com.android.bluetooth I/bt_l2cap: L2CA_ConnectFixedChnl() CID: 0x0004  BDA: d86dc8c205ce
03-29 22:12:07.316 2802-2989/com.android.bluetooth I/bt_l2cap: l2c_ble_link_adjust_allocation  num_hipri: 0  num_lowpri: 1  low_quota: 16  round_robin_quota: 0  qq: 16
03-29 22:12:07.317 2802-2989/com.android.bluetooth I/bt_l2cap: l2c_ble_link_adjust_allocation LCB 0   Priority: 0  XmitQuota: 16
03-29 22:12:07.317 2802-2989/com.android.bluetooth I/bt_l2cap:         SentNotAcked: 0  RRUnacked: 0
03-29 22:12:07.317 2802-2989/com.android.bluetooth D/bt_l2cap: l2cu_allocate_ccb: cid 0x0000
03-29 22:12:07.318 2802-2989/com.android.bluetooth D/bt_l2cap: l2c_link_adjust_chnl_allocation
03-29 22:12:07.318 2802-2989/com.android.bluetooth D/bt_l2cap: POOL ID:3, GKI_poolcount = 400, reserved_buff = 0, weighted_chnls = 2, quota_per_weighted_chnls = 201
03-29 22:12:07.319 2802-2989/com.android.bluetooth I/bt_l2cap: CID:0x0047 Priority:2 TxDataRate:1 Quota:201
03-29 22:12:07.319 2802-2989/com.android.bluetooth I/bt_btm: BTM_InqDbRead: bd addr [d86dc8c205ce]
03-29 22:12:07.320 2802-2989/com.android.bluetooth D/bt_btm: btm_find_dev_type - device_type = 2 addr_type = 1
03-29 22:12:07.320 2802-2989/com.android.bluetooth I/bt_btm: btm_find_or_alloc_dev
03-29 22:12:07.323 2802-2989/com.android.bluetooth D/bt_att: gatt_update_app_use_link_flag  is_add=1 chk_link=0
03-29 22:12:07.324 2802-2989/com.android.bluetooth D/bt_att: gatt_update_app_hold_link_status found=1[1-found] idx=0 gatt_if=6 is_add=1
03-29 22:12:07.324 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=2
03-29 22:12:07.325 2802-2989/com.android.bluetooth I/bt_att: GATT_GetConnIdIfConnected status=0
03-29 22:12:07.346 2802-3001/com.android.bluetooth I/bt_btif: btif_dm_get_remote_services: remote_addr=d8:6d:c8:c2:05:ce
03-29 22:12:07.346 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x202
03-29 22:12:07.346 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_search_sm_execute state:0, event:0x202
03-29 22:12:07.347 2802-2989/com.android.bluetooth I/bt_btif: bta_dm_discover services_to_search=0x7FFFFFFF, sdp_search=1
03-29 22:12:07.348 2802-2989/com.android.bluetooth I/bt_btm: BTM_InqDbRead: bd addr [d86dc8c205ce]
03-29 22:12:07.348 2802-2989/com.android.bluetooth I/bt_btm: BTM_InqDbRead: bd addr [d86dc8c205ce]
03-29 22:12:07.348 2802-2989/com.android.bluetooth D/bt_btm: btm_find_dev_type - device_type = 2 addr_type = 1
03-29 22:12:07.349 2802-2989/com.android.bluetooth D/bt_btif: bta_dm_discover_device BDA:0xD86DC8C205CE
03-29 22:12:07.349 2802-2989/com.android.bluetooth D/bt_btif: bta_dm_discover_device name_discover_done = 0 p_btm_inq_info 0xf3def708 state = 3, transport=2
03-29 22:12:07.349 2802-2989/com.android.bluetooth D/bt_btif: bta_dm_discover_device appl_knows_rem_name 1
03-29 22:12:07.350 2802-2989/com.android.bluetooth I/bt_btm: BTM_IsAclConnectionUp: RemBdAddr: d86dc8c205ce
03-29 22:12:07.350 2802-2989/com.android.bluetooth D/bt_btif: bta_dm_discover_device p_btm_inq_info 0xf3def708 results.device_type 0x2 services_to_search 0x7fffffff
03-29 22:12:07.351 2802-2989/com.android.bluetooth I/bt_btif: BTA got event 0x1f00
03-29 22:12:07.351 2802-2989/com.android.bluetooth I/bt_att: GATT_Connect gatt_if=3
03-29 22:12:07.351 2802-2989/com.android.bluetooth D/bt_att: gatt_get_ch_state: ch_state=2

上面的ch_state在失败时为1,ch_state = 1 = GATT_CH_CLOSING,低于设备连接时,ch_state = 0 = GATT_CH_CLOSE。然后下一行显示 ch_state 变为 2,即 = GATT_CH_CONN。那么这是否意味着 BluetoothGatt 没有关闭,它正在关闭过程中?只有关闭时才能连接到设备?当断开连接发生时,BluetoothGatt.close() 会被调用两次。我将尝试让它只调用 close 一次,看看是否会改变任何东西。也许我在再次调用 connect 之前没有等待足够长的时间?

mBluetoothAdapter.startDiscovery()在单独的说明中,当我在调用之前添加时,S6 和 S7 现在在重新启动后与设备重新连接,mBluetoothAdapter.getRemoteDevice(address)然后在设备上调用 connectGatt 之前取消发现。S6 连接非常快,但 S7 需要几次尝试才能最终重新连接。不完全确定为什么会这样。

4

0 回答 0