更改蓝牙低能量gatt超时或刷新读取流以更快地检测断开事件

bte*_*n96 6 android bluetooth-lowenergy gatt

我正在寻找一种方法来刷新应用程序从Ble设备接收的特征,或者至少从数据中知道连接已经丢失,除非它在断开连接后大约15秒.如果有办法改变gatt连接超时,那将会更好.

要以不同的形式重复,我想要一个解决方案(或一个可以解释的链接)来检测BLE设备的断开速度比当前的超时值更快,通过查看我得到的值是否是新的通过刷新特性,或改变gatt侧的断开超时,所以我可以看到它在一秒内断开连接以触发其他代码.

bte*_*n96 2

这里的其他答案可能比这个更好,但这是我解决问题的方法。在使用这个之前一定要尝试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;
    }
}
Run Code Online (Sandbox Code Playgroud)

在循环中的某个地方,调用

mBluetoothGatt.readRemoteRssi()
Run Code Online (Sandbox Code Playgroud)