BluetoothGattCallback onConnectionStateChange

时间:2017-03-29 02:52:08

标签: android bluetooth bluetooth-lowenergy gatt bluetooth-gatt

因此,我无法弄清楚导致此问题的原因或解决方法。我开发的应用程序整天都与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);
    }

此代码位于自定义Device对象(基本上是外围设备地址的包装器)中,如果我仍然有一个实例再次连接,我会重用该对象。我目前正在尝试使用BluetoothAdapter.getRemoteDevice获取新的BluetoothDevice实例来创建新的自定义Device对象并调用上面的代码。 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上的类似行为。来自外围设备的日志没有提供任何见解,并且当返回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错误是非常不幸和恼人的。 我一直在尝试我能想到的一切来修复或解决这个问题,但它仍然存在。希望有人可以给我一些见解。谢谢你的时间。

- 编辑 -

我已经更改了我的root手机上的蓝牙配置文件以打印出所有蓝牙日志消息,这是从手机以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()会被调用两次。我打算尝试让它只调用一次,看看是否会改变任何东西。也许在再次呼叫连接之前我不会等待足够长的时间?

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

0 个答案:

没有答案