如何在每次打开同一蓝牙低功耗设备时重新连接我的应用程序?
how can I reconnect my application at every opening to the same bluetooth low energy device?
我正在开发 Android 应用程序。在开始时,这个应用程序会向用户显示一个蓝牙设备列表,当他选择其中一个时,会打开另一个 activity.
我的目标是:有一天当用户重新打开应用程序时,应用程序必须记住蓝牙设备并且必须尝试重新连接它。
目前我的目标是这样实现的:
- 我优先记住所选设备的 mac 地址
- 当用户重新打开应用程序时,应用程序会进行隐藏扫描并尝试重新连接到具有相同 MAC 地址的设备。
- 为了检索 mac 地址,我使用函数 getAddress() (当我尝试连接到 android 外围设备时,这个函数给了我
有些问题,因为设备每次都会更改他的 MAC 地址
开始 :/)
同样的东西有没有最好的获取方式?
题外话:ScanResult中的onScanResult函数有一段时间return给我空设备,正常吗?
我使用了 startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) 并且在棒棒糖之后我使用了带有 Scanfilter 的新版本的 startLeScan。会不会是问题?
出于隐私原因,mac 地址不断变化 - 请参阅以下来自 ble 规范的片段:
5.4.5 Privacy Feature
Bluetooth LE supports a feature that reduces the ability to track a LE device over a period of time by changing the
Bluetooth device address on a frequent basis. The privacy feature is
not used in the GAP discovery mode and procedures but it is used, when
supported, during connection mode and connection procedures. In order
for a device using the privacy feature to reconnect to known devices,
the device address, referred to as the private address, must be
resolvable by the other device.
The private address is generated using
the device’s resolving identity key (IRK) exchanged during the bonding
procedure.
所以理想的方法是使用 IRK 来生成 device.However 的实际地址,我没有找到任何 API 来做到这一点。
目前,我在 mny 应用程序中使用的解决方法是重新连接到设备,
- 遍历所有可用设备。
- 获取序列号-如果序列号与已存储的号码匹配
然后保持连接,否则断开连接。
对所有设备重复步骤 2。
此解决方法仅在设备通过某些服务公开序列号时才有效。
您也可以尝试在调用 connectGatt (Context context, boolean autoConnect, BluetoothGattCallback callback)
时将 autoconnect 标志设置为 true
来自文档:
public BluetoothGatt connectGatt (Context context, boolean autoConnect,
BluetoothGattCallback callback) Added in API level 18
Connect to GATT Server hosted by this device. Caller acts as GATT
client. The callback is used to deliver results to Caller, such as
connection status as well as any further GATT client operations. The
method returns a BluetoothGatt instance. You can use BluetoothGatt to
conduct GATT client operations. Parameters
autoConnect Whether to
directly connect to the remote device (false) or to automatically
connect as soon as the remote device becomes available (true).
callback GATT callback handler that will receive asynchronous
callbacks. Throws IllegalArgumentException if callback is null
我正在开发 Android 应用程序。在开始时,这个应用程序会向用户显示一个蓝牙设备列表,当他选择其中一个时,会打开另一个 activity.
我的目标是:有一天当用户重新打开应用程序时,应用程序必须记住蓝牙设备并且必须尝试重新连接它。
目前我的目标是这样实现的:
- 我优先记住所选设备的 mac 地址
- 当用户重新打开应用程序时,应用程序会进行隐藏扫描并尝试重新连接到具有相同 MAC 地址的设备。
- 为了检索 mac 地址,我使用函数 getAddress() (当我尝试连接到 android 外围设备时,这个函数给了我 有些问题,因为设备每次都会更改他的 MAC 地址 开始 :/)
同样的东西有没有最好的获取方式?
题外话:ScanResult中的onScanResult函数有一段时间return给我空设备,正常吗? 我使用了 startLeScan(UUID[] serviceUuids, BluetoothAdapter.LeScanCallback callback) 并且在棒棒糖之后我使用了带有 Scanfilter 的新版本的 startLeScan。会不会是问题?
出于隐私原因,mac 地址不断变化 - 请参阅以下来自 ble 规范的片段:
5.4.5 Privacy Feature
Bluetooth LE supports a feature that reduces the ability to track a LE device over a period of time by changing the Bluetooth device address on a frequent basis. The privacy feature is not used in the GAP discovery mode and procedures but it is used, when supported, during connection mode and connection procedures. In order for a device using the privacy feature to reconnect to known devices, the device address, referred to as the private address, must be resolvable by the other device.
The private address is generated using the device’s resolving identity key (IRK) exchanged during the bonding procedure.
所以理想的方法是使用 IRK 来生成 device.However 的实际地址,我没有找到任何 API 来做到这一点。
目前,我在 mny 应用程序中使用的解决方法是重新连接到设备,
- 遍历所有可用设备。
- 获取序列号-如果序列号与已存储的号码匹配 然后保持连接,否则断开连接。
对所有设备重复步骤 2。
此解决方法仅在设备通过某些服务公开序列号时才有效。
您也可以尝试在调用 connectGatt (Context context, boolean autoConnect, BluetoothGattCallback callback)
时将 autoconnect 标志设置为 true来自文档:
public BluetoothGatt connectGatt (Context context, boolean autoConnect, BluetoothGattCallback callback) Added in API level 18
Connect to GATT Server hosted by this device. Caller acts as GATT client. The callback is used to deliver results to Caller, such as connection status as well as any further GATT client operations. The method returns a BluetoothGatt instance. You can use BluetoothGatt to conduct GATT client operations. Parameters
autoConnect Whether to directly connect to the remote device (false) or to automatically connect as soon as the remote device becomes available (true).
callback GATT callback handler that will receive asynchronous callbacks. Throws IllegalArgumentException if callback is null