BLE 的 connectGatt 中 autoConnect 的哪个正确标志?
Which correct flag of autoConnect in connectGatt of BLE?
我的目标是在低功耗蓝牙设备和 phone 之间建立自动连接。我按照示例代码进行操作,找到了行
// 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(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport)
Connect to GATT Server hosted by this device.
而且我还尝试了两个标志:true
和 false
,但只有 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;
}
"Direct connect" 与 "auto connect" 相反,因此如果将 autoConnect 参数设置为 false,则会得到 "direct connect"。请注意,执行 "mBluetoothGatt.connect()" 也会使用自动连接。
注意 https://code.google.com/p/android/issues/detail?id=69834 这是一个影响旧版本 Android 的错误,它可能会使您的自动连接成为直接连接。这可以通过反射解决。
直接连接和自动连接之间存在一些未记录的差异:
直接连接是一种超时为 30 秒的连接尝试。当直接连接正在进行时,它将暂停所有当前的自动连接。如果已经有一个直接连接挂起,最后一个直接连接将不会立即执行,而是排队等候并在前一个完成时开始。
使用自动连接,您可以同时拥有多个挂起的连接,它们永远不会超时(直到明确中止或直到蓝牙关闭)。
如果连接是通过自动连接建立的,Android 将在断开连接时自动尝试重新连接到远程设备,直到您手动调用 disconnect() 或 close()。一旦通过直接连接建立的连接断开,将不会尝试重新连接到远程设备。
直接连接具有不同的扫描间隔,并且扫描 window 的任务比自动连接更高,这意味着它将花费更多的无线电时间来侦听远程设备的可连接广告,即连接将被建立更快。
ANDROID10
的新变化
自Android10起,直接连接队列被移除,不会再暂时暂停自动连接。这是因为直接连接现在像自动连接一样使用白名单。在进行直接连接时,扫描 window/interval 得到改进。
autoConnect参数决定是否主动连接
远程设备,或者更确切地说是被动扫描并完成连接
当远程设备在 range/available 时。一般来说,第一次
与设备的连接应该是直接的(autoConnect 设置为 false)并且
与已知设备的后续连接应使用
autoConnect 参数设置为 true。
我的目标是在低功耗蓝牙设备和 phone 之间建立自动连接。我按照示例代码进行操作,找到了行
// 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(Context context, boolean autoConnect, BluetoothGattCallback callback, int transport) Connect to GATT Server hosted by this device.
而且我还尝试了两个标志:true
和 false
,但只有 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;
}
"Direct connect" 与 "auto connect" 相反,因此如果将 autoConnect 参数设置为 false,则会得到 "direct connect"。请注意,执行 "mBluetoothGatt.connect()" 也会使用自动连接。
注意 https://code.google.com/p/android/issues/detail?id=69834 这是一个影响旧版本 Android 的错误,它可能会使您的自动连接成为直接连接。这可以通过反射解决。
直接连接和自动连接之间存在一些未记录的差异:
直接连接是一种超时为 30 秒的连接尝试。当直接连接正在进行时,它将暂停所有当前的自动连接。如果已经有一个直接连接挂起,最后一个直接连接将不会立即执行,而是排队等候并在前一个完成时开始。
使用自动连接,您可以同时拥有多个挂起的连接,它们永远不会超时(直到明确中止或直到蓝牙关闭)。
如果连接是通过自动连接建立的,Android 将在断开连接时自动尝试重新连接到远程设备,直到您手动调用 disconnect() 或 close()。一旦通过直接连接建立的连接断开,将不会尝试重新连接到远程设备。
直接连接具有不同的扫描间隔,并且扫描 window 的任务比自动连接更高,这意味着它将花费更多的无线电时间来侦听远程设备的可连接广告,即连接将被建立更快。
ANDROID10
的新变化自Android10起,直接连接队列被移除,不会再暂时暂停自动连接。这是因为直接连接现在像自动连接一样使用白名单。在进行直接连接时,扫描 window/interval 得到改进。
autoConnect参数决定是否主动连接 远程设备,或者更确切地说是被动扫描并完成连接 当远程设备在 range/available 时。一般来说,第一次 与设备的连接应该是直接的(autoConnect 设置为 false)并且 与已知设备的后续连接应使用 autoConnect 参数设置为 true。