DEV Community

HarmonyOS
HarmonyOS

Posted on

How to solve the problem of low efficiency of low-power Bluetooth connection when there are a large number of Bluetooth devices?

Read the original article:How to solve the problem of low efficiency of low-power Bluetooth connection when there are a large number of Bluetooth devices?

Context

When a large number of Bluetooth devices are present (as in a garage scenario), Bluetooth Low Energy (BLE) link efficiency is low.

Description

BLE is a protocol supported since Bluetooth 4.0. Compared to traditional Bluetooth, it offers significantly lower power consumption and faster transmission speeds, but with smaller data transfer volumes. It is commonly used in various smart electronic products that require high battery life and only require small data transfers, such as smart wearables, smart appliances, and sensors, and has a wide range of applications.

The BLE link establishment principle is as follows:

  • Before client A (such as a mobile phone) connects to Bluetooth device B, device B (Advertise) needs to broadcast first, sending a broadcast every certain period. When device A receives the broadcast signal from peripheral device B, it sends a scan request packet (SCAS_REQ). If device B can be scanned and receives the scan request packet, it returns a SCAN_RSP packet to the sender of the scan request packet, and then proceeds to the next Connect connection and data transmission (as shown below):

  • When there are a large number of Bluetooth devices, the advertising channel will be flooded with a large number of SCAN_REQ messages, and the air interface resources will be occupied. The scanning end uses ScanFilter filtering parameters. The link layer of device A (scanning end) only processes the advertising data of devices in the filtering parameter whitelist, reducing the sending of SCAN_REQ messages, reducing the air interface pressure, and at the same time reducing the interactive communication between the internal controller of device A and the host, reducing chip power consumption, and improving link efficiency.

Solution

When scanning within the BLE scanning range, you can use the ScanFilter parameter to filter out target devices, such as the device's random MAC address, device name, or serviceUUID, to quickly find the target device. If you've already connected, you can save the random MAC address or deviceName and use it to filter scans next time.
Note: The random MAC address will change if a paired device is unpaired and then scanned again, or if the Bluetooth service is powered off. If paired with the device address, the address will not change. See ble.getConnectedBLEDevices.
Example: Code to initiate a connection using the serviceUuid.

import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
import { ble } from '@kit.ConnectivityKit';

 function onReceiveEvent(data: Array<ble.ScanResult>) {
     console.info('BLE scan device find result = '+ JSON.stringify(data));
 }
 try {
     ble.on("BLEDeviceFind", onReceiveEvent);
     let scanFilter: ble.ScanFilter = {
             deviceId:"XX:XX:XX:XX:XX:XX",
             name:"test",
             serviceUuid:"00001888-0000-1000-8000-00805f9b34fb"
         };
     let scanOptions: ble.ScanOptions = {
     interval: 500,
     dutyMode: ble.ScanDuty.SCAN_MODE_LOW_POWER,
     matchMode: ble.MatchMode.MATCH_MODE_AGGRESSIVE
     }
     ble.startBLEScan([scanFilter],scanOptions);
 } catch (err) {
     console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
 }
Enter fullscreen mode Exit fullscreen mode

Key Takeaways

During the Bluetooth Low Energy connection process, the strength of the peer BLE device's broadcasts and the distance between the local and peer BLE devices can cause Bluetooth connection failures. Therefore, it's important to stay within the connection range.
After the initial Bluetooth connection, you can save information such as the MAC address or device name as a ScanFilter parameter to improve connection efficiency.
[Frequently Asked Questions]
Q: Does scanFilter refer to the parts of the results that are filtered out (not included) or does it mean filtered out (only included)?
A: It does.

Q: How do I cancel a filter condition during a Bluetooth scan?
A: Pass null as the filter condition, for example:

ble.startBLEScan(null, scan0ptions)
Enter fullscreen mode Exit fullscreen mode

Q: What is the difference between writeCharacteristicValue and writeDescriptorValue in Bluetooth Low Energy?
A: writeDescriptorValue is used for writing descriptors, while writeCharacteristicValue is used for writing characteristic values. If you need to write a message to a device, use writeCharacteristicValue.

Written by Hatice Akyel

Top comments (0)