Android Bluetooth GattCallback onConnectionStateChange中的循环状态

Android Bluetooth GattCallback onConnectionStateChange中的循环状态,android,bluetooth,bluetooth-lowenergy,gatt,bluetooth-gatt,Android,Bluetooth,Bluetooth Lowenergy,Gatt,Bluetooth Gatt,所以我一直无法找出是什么导致了这个问题,或者如何解决它。我正在开发一款应用程序,它可以全天与BLE外设保持连接,并从设备上的传感器收集数据。有时,设备会断开连接,当我查看logcat时,我只看到重新连接失败,Bluetooth GattCallback onConnectionStateChange回调获得133状态,这将只是循环。关闭应用程序、切换手机蓝牙开关、清除蓝牙缓存都是我们试图让设备重新连接的事情。我在主线程上连接到设备,如下所示: if (Build.VERSION.SDK_

所以我一直无法找出是什么导致了这个问题,或者如何解决它。我正在开发一款应用程序,它可以全天与BLE外设保持连接,并从设备上的传感器收集数据。有时,设备会断开连接,当我查看logcat时,我只看到重新连接失败,Bluetooth GattCallback 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上,当我重新启动手机,启动接收器启动服务以连接到外围设备时,会出现类似的行为。来自外围设备的日志没有提供任何细节,当返回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错误,但是我应该如何以编程方式处理这个问题呢?我已经看到谷歌收到的一些关于类似问题的bug报告,但这个问题发生在我的应用程序支持的所有版本的android上,即5.0及以上版本。 对于这个问题我真的不知所措。有时,让另一个应用程序只扫描蓝牙设备会让我的应用程序重新连接。让三星手机仅仅通过关闭和打开蓝牙就陷入了这个错误,这真是不幸和恼人。 我一直在尝试我能想到的一切来修复或解决这个问题,但它仍然存在。希望有人能给我一些见解。谢谢你抽出时间

-编辑-

我已经更改了根手机上的Bluetooth配置文件,以打印出所有Bluetooth日志消息,这是从手机处于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
当设备发生故障时,Chu状态高于1,Chu状态=1=GATT_Chu关闭;当设备连接时,Chu状态低于1,Chu状态=0=GATT_Chu关闭。然后下一行显示Chu状态变为2,即GATT_Chu CONN。那么这是否意味着BluetoothGatt没有关闭,它正在关闭过程中?只有当它关闭时才能连接到设备?BluetoothGatt.close()在断开连接时被调用两次。我会尽量让它只关闭一次,看看这是否会改变什么。也许我没有等足够长的时间再给connect打电话


另一方面,当我在调用
mBluetoothAdapter.getRemoteDevice(address)
之前添加
mBluetoothAdapter.startDiscovery()
并在调用设备上的connectGatt之前取消查找时,S6和S7现在在重新启动后重新连接设备。S6的连接速度相当快,但S7需要几次尝试才能最终重新连接。不太清楚为什么会这样。

Android的BLE API不是很棒吗?您应该启用hci snoop log(),并在Wireshark中查看它,以了解实际情况。Wireshark的级别有点太低。我看过那些日志,但它们没有告诉我我想弄明白什么。我更改了手机上的蓝牙配置,以打印出所有蓝牙系统日志,我觉得这是我需要分析的级别,以找出事情为什么会这样做。同样的问题,也是257个错误代码,您只需添加“startDiscovery”就可以解决在getRemoteDevice之前?@AntonShkurenko我正在做一些事情来尝试解决这个问题,但有时我仍然会遇到这个问题。有一件事很有帮助,那就是在尝试重新连接之前执行startDiscovery/cancelDiscovery。我还请求高连接优先级,但当它连接时,请求平衡的连接优先级。保留高优先级会耗尽手机电池。用其他应用程序或手机扫描蓝牙设备似乎可以让应用程序重新连接,这对我来说是最好的,所以我一直想用startDiscovery做得更好。让我知道你在做什么。祝你好运。我也有同样的问题,一次又一次的状态133。你明白了吗?