Android BLE Resubscribing to Characteristics

2019-07-28 18:45发布

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条回答
forever°为你锁心
2楼-- · 2019-07-28 19:39

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

查看更多
登录 后发表回答