蓝牙GATT断开连接时onConnectionStateChange未被调用

6
我正在尝试通过设置计时器并手动调用BluetoothGatt.disconnect()来在我的蓝牙GATT服务上实现自己的超时。但是,如果断开连接是由远程设备触发的,回调不会像通常发生的那样被调用。还有一个来自BluetoothGatt的日志表明已经调用了断开连接函数。
D/BluetoothGatt﹕ cancelOpen() - device: 00:07:80:04:1A:5A

这是我断开连接的代码

private void scheduleDisconnect() {
    isTimerRunning = true;
    disconnectTimer = new Timer();
    disconnectTimer.schedule(new TimerTask() {
        @Override
        public void run() {
            isTimerRunning = false;
            disconnect();
        }
    }, 2000);
}

为什么onConnectionStateChange没有被调用?对于另一个回调和操作它很有效。

我遇到了同样的问题。它非常随机。我调用了disconnect()和close()方法。onConnectionStateChange有时会被调用,但并不总是。 - Paul
请参阅此处:https://dev59.com/r2Ag5IYBdhLWcg3w1t_y - RenniePet
2个回答

2
你的disconnect()方法是否也关闭了连接?只有在使用完设备后才调用BluetoothGatt.close(),否则你的回调函数将会被取消注册。

我简直要亲亲你!这就是我的问题。太感谢了!我在我的断开连接方法里调用了我的关闭方法,所以它在执行调用前就已经"注销"了。 - luckyging3r

0

为了断开我的设备,我使用设备的gatt链接,并使用BluetoothGatt.disconnect()和BluetoothGatt.close()方法。

这个方法可以工作,但是由于我关闭了流,所以没有调用onConnectionStateChange()。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接