BLE 的 connectGatt 中自动连接的哪个正确标志? [英] Which correct flag of autoConnect in connectGatt of BLE?

查看:12
本文介绍了BLE 的 connectGatt 中自动连接的哪个正确标志?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

My goal is to make an auto connection between Bluetooth Low Energy device and phone. I followed the sample code and I found the line

// 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,它说

The above code means that false uses to autoconnection. However, I found the API at here, it said that

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

BluetoothGatt connectGatt(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport) Connect to GATT Server hosted by this device.

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

And I also tried two flags: true and false, and only true works. I am using version >= Android 5.0. Has something inconsistent between code and API? Which flag is correct? Do I need note something if I want to make the auto connection?

这是我的代码

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;
}

推荐答案

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

"Direct connect" is the opposite to "auto connect", so if you set the autoConnect parameter to false you get a "direct connect". Note that doing a "mBluetoothGatt.connect()" will also use auto connect.

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

Beware of https://code.google.com/p/android/issues/detail?id=69834 which is a bug affecting older versions of Android which might make your auto connections to be direct connections instead. This can be worked around with reflection.

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

There are a few non-documented differences between direct and auto connect:

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

Direct connect is a connection attempt with a 30 seconds timeout. It will suspend all current auto connects while the direct connect is in progress. If there already is a direct connect pending, the last direct connect will not be executed immediately but rather be queued up and start when the previous has finished.

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

With auto connect you can have multiple pending connections at the same time and they will never time out (until explicitly aborted or until Bluetooth is turned off).

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

If the connection was established through an auto connect, Android will automatically try to reconnect to the remote device when it gets disconnected until you manually call disconnect() or close(). Once a connection established through direct connect disconnects, no attempt is made to reconnect to the remote device.

与自动连接相比,直接连接具有不同的扫描间隔和扫描窗口,其占空比更高,这意味着它将投入更多的无线电时间来侦听远程设备的可连接广告,即连接建立得更快.

Direct connect has a different scan interval and scan window at a higher duty than auto connect, meaning it will dedicate more radio time to listen for connectable advertisements for the remote device, i.e. the connection will be established faster.

Android 10 的新变化

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

Since Android 10, the direct connect queue is removed and will not temporarily pause auto connects any more. This is because direct connect now uses the white list just like auto connects. The scan window/interval is improved while a direct connect is ongoing.

这篇关于BLE 的 connectGatt 中自动连接的哪个正确标志?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆