使用绑定设备时gatt.writeDescriptor错误状态回调

tal*_*ari 5 android bluetooth-lowenergy gatt

我正在开发一个需要自动连接到外围设备的应用程序.

我有一个粘性服务,执行以下操作:

  1. 在绑定设备中查找所需的设备
  2. 如果没有对设备进行微调(第一次),扫描并通过使用device.createBond()绑定到它,通过收听ACTION_BOND_STATE_CHANGED广播等待绑定完成
  3. 使用连接到它 device.connectGatt(ctx,true,callback)
  4. 等待onConnectionStateChange连接状态的回调
  5. 通过使用启动服务发现 gatt.discoverServices()
  6. 等待onServicesDiscoverd回调
  7. 通过使用编写描述符来启用特征通知 gatt.writeDescriptor
  8. 等待具有成功状态的onDescriptorWrite回调 BluetoothGatt.GATT_SUCCESS (0)
  9. 做收到的通知

这一切都是第一次正常.当设备断开连接(例如,超出范围或关闭)时,粘性服务回调gatt.disconnect()gatt.close()重新启动并再次完成所有这些操作,这次它使用绑定设备进行连接.

一切正常,直到第7步,这意味着我得到onDescriptorWrite状态133 的回调,有时接着是状态0和状态22的连接状态更改回调.

我在网上找不到任何状态133或22的信息.

知道为什么会这样吗?

我现在正在努力解决这个糟糕的onDescriptorWrite回调问题,通过删除键(反射)再次使用新扫描的设备完成所有操作.

所以基本上我使用绑定只是为了等待设备连接,然后重新启动整个事情.

这意味着绑定设备的gatt连接对于编写我需要的描述符是没用的.

感觉我错过了什么,很想知道什么.

编辑:一些相关的logcat输出

08-18 16:06:31.363  12765-12835/? W/bt-att? gatt_rsp_timeout disconnecting...  
08-18 16:06:31.363  12765-12835/? W/bt-btif? bta_gattc_conn_cback() - cif=3     connected=0 conn_id=3 reason=0x0016  
08-18 16:06:31.363  12765-12835/? W/bt-btif? bta_gattc_conn_cback() - cif=4     connected=0 conn_id=4 reason=0x0016  
08-18 16:06:31.363  12765-12835/? W/bt-btif? bta_gattc_conn_cback() - cif=5     connected=0 conn_id=5 reason=0x0016  
08-18 16:06:31.366  12765-12807/? D/BtGatt.GattService? onDisconnected() -     clientIf=5, connId=5, address=C1:D1:22:BA:F5:13  
here im getting onDescriptorWrite with status 133  
D/BluetoothGatt? onClientConnectionState() - status=22 clientIf=5     device=C1:D1:22:BA:F5:13  
Run Code Online (Sandbox Code Playgroud)

通过这个看起来:https:
//android.googlesource.com/platform/external/bluetooth/bluedroid/+/idea133/bta/include/bta_gatt_api.h#169

16表示BTA_GATT_CONN_TERMINATE_LOCAL_HOST
22表示BTA_GATT_CONN_LMP_TIMEOUT

在nordics github上问这个问题:https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406

tal*_*ari 1

在北欧github上问这个: https://github.com/NordicSemiconductor/Android-nRF-Toolbox/issues/9#issuecomment-132191406

按照他们的建议,在调用 connectGatt 后添加了 2 秒的延迟,现在它可以工作了。