DEV Community

HarmonyOS
HarmonyOS

Posted on

Reconnecting to a Previously Connected BLE Device Without Scanning

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 addPersistentDeviceId API, allowing developers to persist a virtual MAC address without pairing.
  • Important notes:
    1. Using persistent device ID requires the restricted permission ohos.permission.PERSISTENT_BLUETOOTH_PEERS_MAC and approval through ACL.
    2. 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.

Implementation Steps

Solution 1: Use Pairing

  1. After the first BLE connection, initiate pairing with the remote device.
  2. Once paired, the virtual MAC address is fixed.
  3. In subsequent sessions, call getPairedDevices to fetch the device ID.
  4. Use ble.createGattClientDevice() and connect() directly without scanning.

Solution 2: Use Persistent Device ID (API ≥16)

  1. After the first BLE connection, call addPersistentDeviceId to persist the virtual MAC address.
  2. In subsequent sessions, call getPersistentDeviceIds.
  3. 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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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_MAC and ACL approval.
  • Persistent device ID becomes invalid if the remote device’s real MAC address changes.

Related Documents or Links

Written by Bunyamin Eymen Alagoz

Top comments (0)