【问题标题】:Which correct flag of autoConnect in connectGatt of BLE?BLE的connectGatt中哪个正确的autoConnect标志?
【发布时间】:2017-03-02 14:06:13
【问题描述】:

我的目标是在蓝牙低功耗设备和手机之间建立自动连接。我按照示例代码找到了这条线

// We want to directly connect to the device, so we are setting the autoConnect parameter to false.
mBluetoothGatt = device.connectGatt(this, false, mGattCallback);

以上代码表示false 用于自动连接。但是,我在here找到了API,上面写着

BluetoothGatt connectGatt(上下文上下文,布尔自动连接,BluetoothGattCallback 回调,int 传输) 连接到此设备托管的 GATT 服务器。

我还尝试了两个标志:truefalse,只有 true 有效。我正在使用版本 >= Android 5.0。代码和 API 之间有不一致的地方吗?哪个标志是正确的?如果我想进行自动连接,需要注意什么吗?

这是我的代码

public boolean connect(final String address) {
    if (mBluetoothAdapter == null || address == null) {
        Log.w(TAG, "BluetoothAdapter not initialized or unspecified address.");
        return false;
    }

    // Previously connected device.  Try to reconnect.
    if (mBluetoothDeviceAddress != null && address.equals(mBluetoothDeviceAddress)
            && mBluetoothGatt != null) {
        Log.d(TAG, "Trying to use an existing mBluetoothGatt for connection.");
        if (mBluetoothGatt.connect()) {
            mConnectionState = STATE_CONNECTING;
            return true;
        } else {
            return false;
        }
    }

    final BluetoothDevice device = mBluetoothAdapter.getRemoteDevice(address);
    if (device == null) {
        Log.w(TAG, "Device not found.  Unable to connect.");
        return false;
    }
    // We want to directly connect to the device, so we are setting the autoConnect
    // parameter to false.
    mBluetoothGatt = device.connectGatt(this, true, mGattCallback);
    Log.d(TAG, "Trying to create a new connection.");
    mBluetoothDeviceAddress = address;
    mConnectionState = STATE_CONNECTING;
    return true;
}

【问题讨论】:

    标签: android bluetooth bluetooth-lowenergy bluetooth-gatt


    【解决方案1】:

    “直接连接”与“自动连接”相反,因此如果将 autoConnect 参数设置为 false,您将获得“直接连接”。请注意,执行“mBluetoothGatt.connect()”也会使用自动连接。

    注意https://code.google.com/p/android/issues/detail?id=69834,这是一个影响旧版 Android 的错误,它可能会使您的自动连接改为直接连接。这可以通过反射来解决。

    直接连接和自动连接之间存在一些未记录的差异:

    直接连接是具有 30 秒超时的连接尝试。当直接连接正在进行时,它将暂停所有当前的自动连接。如果已经有一个直连挂起,最后一个直连不会立即执行,而是排队并在前一个完成时开始。

    使用自动连接,您可以同时拥有多个挂起的连接,并且它们永远不会超时(直到明确中止或蓝牙关闭)。

    如果连接是通过自动连接建立的,Android 将在断开连接时自动尝试重新连接到远程设备,直到您手动调用 disconnect() 或 close()。一旦通过直接连接建立的连接断开,就不会尝试重新连接到远程设备。

    与自动连接相比,直接连接具有不同的扫描间隔和扫描窗口,这意味着它将花费更多的无线电时间来侦听远程设备的可连接广告,即建立连接的速度更快。

    ANDROID 10 的新变化

    自 Android 10 起,直接连接队列已被移除,并且不会再暂时暂停自动连接。这是因为直接连接现在像自动连接一样使用白名单。在进行直接连接时改进了扫描窗口/间隔。

    【讨论】:

    • 这是关于自动连接行为的有用信息。你有它的参考吗? connectGatt docs 没有提供太多细节,到目前为止我还没有找到 Android 源代码的正确位(BluetoothGatt.javaIBluetoothGatt 上调用 clientConnect,它来自 .aidl 文件,但是我还没有找到任何实现它的东西)
    • connect 命令要经过很多层。您可以在android.googlesource.com/platform/system/bt/+/master/stack/gatt/… 中查看 GATT_Connect。这里是aidl接口的另一端:android.googlesource.com/platform/packages/apps/Bluetooth/+/….
    • @Emil,当应用程序在后台时自动连接是否有效?也许在服役?
    • BLE GATT 连接和传输不涉及应用程序如何保持活动或被杀死的候选者。所以如果你想要一个长期存在的 GATT 连接或挂起的连接,你应该在你的应用进程中运行一个前台服务,这样你的进程就不会被杀死。
    【解决方案2】:

    autoConnect参数决定是否主动连接 远程设备,或者更确切地说是被动扫描并完成连接 当远程设备在范围内/可用时。一般来说,第一次 与设备的连接应该是直接的(autoConnect 设置为 false)并且 应调用与已知设备的后续连接 autoConnect 参数设置为 true。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-31
      • 1970-01-01
      • 2014-11-14
      • 2019-01-01
      相关资源
      最近更新 更多