我正在寻找一种Android方法来刷新应用程序从Ble设备接收到的特征,或者至少从数据中知道连接实际上已经断开,但断开连接后大约15秒就消失了。如果有办法更改gatt连接超时,那会更好。
以另一种形式重复,我希望有一个解决方案(或可以解释的链接),通过查看我获取的值是否新鲜来检测BLE设备的断开速度,该方法比当前的超时值更快通过刷新特性或更改gatt端的断开连接超时,这样一秒钟之内就可以看到断开连接以触发其他代码。
这里的其他答案可能比这个答案更好,但这是我对问题的解决方案。在使用此功能之前,请务必尝试Emil的回答。
由于时间太慢,我无法等待,所以检查rssi是因为它一直在变化。如果有一段时间(例如3秒钟),则该值保持不变,它将与设备断开连接。这大约是15秒超时,并添加了我们自己的超时。
这将是检查信号强度所需要的。这是几年前写的,所以有些事情可能需要更改。
private final BluetoothGattCallback mGattCallback = new BluetoothGattCallback() {
@Override
public void onReadRemoteRssi(BluetoothGatt gatt, int rssi, int status){
//check for signal strength changes. It will stay the same if we
//are getting no updates
if(mLastRssi == rssi){
disconnectCounter++;
if(disconnectCounter> 140) {
//disconnect logic as if we are done with connection
//maybe start listening for device again to reconnect
disconnectCounter = 0;
}
}
else{
//we are connected. reset counter
disconnectCounter = 0;
}
//store last value as global for comparison
mLastRssi= rssi;
}
}
循环中的某处,致电
mBluetoothGatt.readRemoteRssi()
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句