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

changing bluetooth low energy gatt timeout or flushing read stream to detect disconnect event quicker

我正在寻找一种 android 方法来刷新应用程序从 Ble 设备接收到的特征,或者至少从数据中知道连接已经丢失,但实际上是在 15 左右断开连接后几秒钟。如果有办法改变 gatt 连接超时,那会好得多。

为了以不同的形式重复,我想要一个解决方案(或可以解释的 link)来检测 BLE 设备断开连接的速度比当前的超时值更快,方法是通过刷新特征或更改 gatt 端的断开连接超时来查看我获得的值是否新鲜,因此我可以在一秒钟内看到它断开连接以触发其他代码。

Android有回调:

BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange( BluetoothGatt gatt,int status,int newState){ 
        if(newState == BluetoothProfile.STATE_DISCONNECTED){
            //your code here
        }
    }
}

不知道这是否有帮助,但您可以利用(如果有的话)在关贸总协定中传输的周期性数据。因此,例如,如果您测量的是 1 秒的周期性,您可以执行以下操作:

// runnable to detect the lack of activity:
private final Runnable watchDog = new Runnable() {
    @Override
    public void run() {
        measurement_timeout--;
        if(measurement_timeout==0) {
            Log.d("BLE_CONTROLLER", "PROBE WITH NO ACTIVITY");

        }
    }
};

// this should be in the reception of the periodic data:
measurement_timeout++;
mHandler.postDelayed(watchDog, 3000);

所以 "measurement_timeout" 将作为实际超时工作,当它达到 0 时意味着您在 3000 毫秒的时间内没有收到数据。 请注意,您的观看时间必须 > 2*数据周期。

我设法实现快速 gatt 断开连接的唯一方法是确保外围设备在断电或切断连接之前发送 BLE 断开连接指令。

一旦android 收到断开连接指令,gatt 会立即整理,而不是需要 15 秒才能意识到外围设备丢失。

似乎大多数外围设备都不会打扰并且消失了。

显然,这种方法只有在您能够修改外围设备时才可行。

正确的方法是使用外围端的连接参数更新请求将超时更改为较低的值。

这里的其他答案可能比这个更好,但这是我解决问题的方法。在使用这个之前一定要尝试 回答。

由于时间太慢而无法等待,我所做的就是检查 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()