Read the original article:Reconnecting to a Previously Connected BLE Device Without Scanning
Reconnecting to a Previously Connected BLE Device Without Scanning
Requirement Description
Enable quick reconnection to a previously connected BLE device without performing a new Bluetooth scan in HarmonyOS.
Background Knowledge
- In HarmonyOS, if two devices (A and B) are not paired, their virtual Bluetooth MAC addresses may change after reboot or reconnection.
- Once paired, the virtual MAC address becomes fixed and does not change even if the device restarts.
- Starting from API Level 16, HarmonyOS provides the
addPersistentDeviceIdAPI, allowing developers to persist a virtual MAC address without pairing. -
Important notes:
- Using persistent device ID requires the restricted permission
ohos.permission.PERSISTENT_BLUETOOTH_PEERS_MACand approval through ACL. - Developers must ensure that the persisted virtual address corresponds to a stable real device address. If the remote device’s real address changes, the stored virtual address becomes invalid.
- Using persistent device ID requires the restricted permission
Implementation Steps
Solution 1: Use Pairing
- After the first BLE connection, initiate pairing with the remote device.
- Once paired, the virtual MAC address is fixed.
- In subsequent sessions, call
getPairedDevicesto fetch the device ID. - Use
ble.createGattClientDevice()andconnect()directly without scanning.
Solution 2: Use Persistent Device ID (API ≥16)
- After the first BLE connection, call
addPersistentDeviceIdto persist the virtual MAC address. - In subsequent sessions, call
getPersistentDeviceIds. - Validate the stored device ID and connect directly.
Code Snippet / Configuration
Sample Code (Solution 1):
let devices = connection.getPairedDevices()
for (let device of devices) {
let name = connection.getRemoteDeviceName(device)
if (name === this.deviceName) {
this.gattClient = ble.createGattClientDevice(device)
this.onBLEConnectionStateChange()
this.gattClient.connect()
return
}
}
// If not found, fall back to scanning and pair afterwards
Sample Code (Solution 2):
let devices = access.getPersistentDeviceIds()
for (let device of devices) {
let name = connection.getRemoteDeviceName(device)
if (access.isValidRandomDeviceId(device) && name === this.deviceName) {
this.gattClient = ble.createGattClientDevice(device)
this.onBLEConnectionStateChange()
this.gattClient.connect()
return
}
}
// If not found, perform scanning and add persistent device ID
Test Results
- Solution 1 verified: once paired, reconnection succeeds without scanning.
- Solution 2 verified: after persisting a device ID, reconnection is possible without scanning even if the device restarts.
Limitations or Considerations
- Solution 1 (Pairing) is recommended because it requires only standard permissions.
- Solution 2 requires restricted permission
ohos.permission.PERSISTENT_BLUETOOTH_PEERS_MACand ACL approval. - Persistent device ID becomes invalid if the remote device’s real MAC address changes.
Top comments (0)