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

问题描述 投票:6回答:5

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

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

android bluetooth-lowenergy gatt
5个回答
2
投票

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

我所做的事情因为时间太慢而无法等待它,因为它总是在变化,检查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()

0
投票

不知道这是否会有所帮助,但你可以利用(如果你有的话)在gatt中传输的周期性数据。因此,如果您测量的周期为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 ms的时间段内收到数据。请注意,您必须拥有> 2 *数据周期的观看时间。


0
投票

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

一旦android收到断开连接指令,gatt立即整理,而不是花费15秒来实现外设丢失。

似乎大多数外围设备都不会打扰而只是消失。

显然,只有在能够修改外围设备时才能使用此方法。


0
投票

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


-1
投票

Android中有一个回调:

BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
    @Override
    public void onConnectionStateChange( BluetoothGatt gatt,int status,int newState){ 
        if(newState == BluetoothProfile.STATE_DISCONNECTED){
            //your code here
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.