【问题标题】:Android Bluetooth LE: Not discovering services after connectionAndroid蓝牙LE:连接后未发现服务
【发布时间】:2014-06-03 21:46:34
【问题描述】:

我正在尝试使用 Android 的低功耗蓝牙与 BLE 设备进行通信。第一次连接时,一切正常(连接到 GATT 服务器正常,发现所有服务和特性等)但是,如果我断开连接并尝试重新连接,它将连接到 GATT 服务器,但不会能够发现服务。我必须杀死应用程序并重新启动它,有时即使这样也不起作用。

这是我用来断开设备连接的代码:

public void close(View view) {
    if (mBluetoothGatt == null) {
        return;
    }
    mBluetoothGatt.close();
    mBluetoothGatt = null;
}

断开连接时我还需要做什么吗?似乎有一些资源仍处于连接状态,当我尝试重新连接时会阻止发现服务。

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy android-bluetooth


    【解决方案1】:

    我似乎找到了解决方案:您需要同时调用 BluetoothGatt.disconnect()BluetoothGatt.close()

    【讨论】:

    • 从技术上讲,您需要调用 BluetoothGatt.disconnect() 然后 [因为不能保证调用 onConnectionStateChange] 启动一个计时器,然后等待 onConnectionStateChange state=STATE_DISCONNECTED 或超时,然后调用 BluetoothGatt.close()
    • @swooby 是否有您可以提供的消息来源提到需要在调用 close 之前等待 STATE_DISCONNECTED?谢谢!
    • 有趣的是,即使我在关闭前调用了断开连接,我也没有在日志中观察到该跟踪。再说一次,我现在正在看牛轧糖。感谢您的链接!
    • 断线后绝对可以直接调用close。唯一可能发生的坏事是在日志中打印警告。您应该避免的唯一事情是在 connectGatt 之后立即调用 disconnect 或 close,因为这些调用可能会由于内部竞争条件而被忽略。
    猜你喜欢
    • 2012-08-18
    • 2022-01-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-09
    • 1970-01-01
    相关资源
    最近更新 更多