本文介绍了Android BLE重新订阅特征的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,它在BluetoothGatt给我GATT_SUCCESS后自动订阅了onServicesDiscovered()上的特定BLE特性.这工作得很好.

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.

后来,我注意到使用通过BluetoothGatt.connect()的间接连接重新连接到先前的连接花费的时间太长,因此我将其更改为直接连接:device.connectGatt(this,false,mGattCallback).这对提高我的重新连接速度非常有用.

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.

因此,如果我重新连接了两次,并要求BLE服务器向我发送温度,它将发送给我:

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

25℃

25℃

25℃

我尝试了几步都没有运气,包括在重新连接之前将BluethoothGatt设置为null,或者在断开连接后禁用特征的通知/指示,但是我没有运气.预先感谢.

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.

推荐答案

我认为您忘了在第一个BluetoothGatt对象上调用close().

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

这篇关于Android BLE重新订阅特征的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-26 12:26