Android BLE Resubscribing to Characteristics

核能气质少年 提交于 2020-01-25 00:48:07

问题


I have an Android app which auto-subscribes to specific BLE characteristics on onServicesDiscovered() after BluetoothGatt gives me a GATT_SUCCESS. This was working pretty well.

Later, I noticed that reconnecting to a previous connection using an indirect connection via BluetoothGatt.connect() was taking too long, so I changed it to be a direct connection: device.connectGatt(this, false, mGattCallback). This worked great in improving my reconnection speed.

The problem: Now whenever I do a reconnect I resubscribe to all my characteristics. This is resulting in me recieving a given notification/indication one additional time for each time I had to do a reconnection.

So if I reconnected two times and I ask my BLE server to send me the temperature it would send me:

25C

25C

25C

I've tried a couple things with no luck including setting my BluethoothGatt to null before reconnecting or disabling notification/indications for a characterstic after disconnecting, but I've had no luck. Thanks in advance.


回答1:


I think you forget to call close() on your first BluetoothGatt object.



来源:https://stackoverflow.com/questions/49889420/android-ble-resubscribing-to-characteristics

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!