The following is a complete process example of BLE technology in typical application scenarios, covering smart home control, health monitoring, asset tracking and IoT data collection. It combines hardware configuration, software processes, communication interactions and code examples to help developers quickly understand the actual application logic: |
1. Smart Home Control (e.g., light switch)
Scenario Description
Control the color and brightness of smart bulbs via BLE.
Process Steps
(1) Device Initialization
•Hardware Configuration:
○Smart bulb (Peripheral): nRF52840 + LED driver circuit.
○Control end (Central): Mobile APP.
•Software Initialization:
C // Bulb initialization (nRF Connect SDK) void main(void) { if (bt_enable(NULL)) { printk(“Bluetooth init failed\n”); return; } init_light_service(); // Register light service start_advertising(); } |
(2) Broadcasting and Scanning
•Bulb Broadcasting:
C static struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_FLAG_BREDR_NOT_SUPPORTED), BT_DATA(BT_DATA_NAME_COMPLETE, “Smart Light”, 11), BT_DATA_UUID16_LIST(BT_UUID_LIGHT_SERVICE), }; void start_advertising(void) { bt_le_adv_start(BT_LE_ADV_NCONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); } |
•Mobile Scanning:
C // Mobile scanning and discovering bulb bt_le_scan_start(BT_LE_SCAN_ACTIVE, scan_cb); |
(3) Connection Establishment
•Mobile Initiates Connection:
C void connect_to_light(const bt_addr_le_t *addr) { bt_conn_le_create(addr, BT_CONN_LE_CREATE_CONN, NULL, conn_cb); } |
(4) Service Discovery
•Discover Light Service:
C // Mobile discovers service bt_gatt_discover(conn, BT_UUID_LIGHT_SERVICE, light_service_cb); |
(5) Data Interaction
•Set Light Color and Brightness:
C // Mobile writes characteristic value struct bt_gatt_write_params write_params = { .data = (uint8_t[]){0xFF, 0x00, 0x00, 0x80}, // Red, brightness 128 .length = 4, }; bt_gatt_write(conn, color_char, &write_params); |
•Bulb Responds:
C // Bulb characteristic write callback static void color_write_cb(struct bt_conn *conn, struct bt_gatt_write_params *params) { uint8_t *data = params->data; set_led_color(data[0], data[1], data[2]); // Set RGB values set_brightness(data[3]); // Set brightness } |
(6) Disconnect
•Mobile Actively Disconnects:
C bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); |
2. Health Monitoring (e.g., heart rate sensor)
Scenario Description
Real-time transmission of heart rate data to mobile APP via BLE.
Process Steps
(1) Device Initialization
•Hardware Configuration:
○Heart rate sensor (Peripheral): nRF52832 + heart rate sensor module.
○Mobile (Central): Listens and displays data.
(2) Service Registration
•Define Heart Rate Service:
C // Heart rate service (GATT configuration) BT_GATT_SERVICE_DEFINE(heart_rate_svc, BT_GATT_PRIMARY_SERVICE(BT_UUID_HEART_RATE), BT_GATT_CHARACTERISTIC(BT_UUID_HEART_RATE_MEASUREMENT, BT_GATT_CHRC_NOTIFY, BT_GATT_PERM_READ, heart_rate_read, NULL, NULL), BT_GATT_CCC(heart_rate_ccc, BT_GATT_PERM_READ | BT_GATT_PERM_WRITE) ); |
(3) Broadcasting and Connection
•Broadcast includes heart rate service UUID:
C BT_DATA_UUID16_LIST(BT_UUID_HEART_RATE), // Broadcast heart rate service UUID |
(4) Data Notification
•Sensor actively pushes data:
C // Heart rate measurement callback (triggered every second) void heart_rate_update(uint16_t value) { struct bt_gatt_notify_params notify_params = { .uuid = BT_UUID_HEART_RATE_MEASUREMENT, .data = &value, .length = 2, }; bt_gatt_notify(NULL, notify_params); // Broadcast to all connected Central } |
•Mobile Receives Notification:
C // Mobile notification callback static void notify_cb(struct bt_conn *conn, struct bt_gatt_subscribe_params *params, const struct bt_gatt_notify_params *notify) { uint16_t heart_rate = *(uint16_t *)notify->data; printf(“Heart Rate: %d bpm\n”, heart_rate); } |
3. Asset Tracking (e.g., Beacon positioning)
Scenario Description
Broadcast location information via BLE Beacon for mobile or gateway to locate devices.
Process Steps
(1) Beacon Broadcasting Configuration
•Non-connectable broadcast:
C static struct bt_data ad[] = { BT_DATA_BYTES(BT_DATA_FLAGS, BT_LE_AD_FLAG_BREDR_NOT_SUPPORTED), BT_DATA(BT_DATA_UUID16, (uint8_t[]){0x00, 0x11, 0x22, 0x33}, 4), // Custom UUID BT_DATA_MANUFACTURER_DATA, // Contains location information }; void start_beacon(void) { bt_le_adv_start(BT_LE_ADV_NCONN_NAME, ad, ARRAY_SIZE(ad), NULL, 0); } |
(2) Broadcast Interval Optimization
•Extend broadcast interval to reduce power consumption:
Makefile CONFIG_BT_BLE_ADV_INTERVAL=1000 # Broadcast once every second (1000×0.625ms=625ms) |
(3) Scanning and Parsing
•Mobile scans Beacon:
C // Scan callback static void beacon_scan_cb(const struct bt_le_scan_resp *resp, int err) { if (err) return; // Parse UUID and location information from advertisement data parse_beacon_data(resp->adv_data); } |
4. IoT Data Collection (e.g., temperature and humidity sensor)
Scenario Description
Periodically collect temperature and humidity data and upload it to the gateway via BLE.
Process Steps
(1) Low Power Mode Configuration
•Peripheral Role:
C nrf_pwr_mgmt_init(); nrf_pwr_mgmt_run(); // Enter deep sleep |
(2) Timed Wake-up to Collect Data
•Use timer to trigger collection:
C void sensor_task(void) { while (1) { collect_sensor_data(); // Collect temperature and humidity update_sensor_characteristic(); // Update GATT characteristic value k_sleep(K_SECONDS(60)); // Collect once every minute } } K_THREAD_DEFINE(sensor_thread_id, 1024, sensor_task, NULL, NULL, NULL, 7, 0, 0); |
(3) Data Transmission
•Gateway actively connects and reads data:
C // Gateway reads characteristic value bt_gatt_read(conn, temp_char, temp_read_cb); |
(4) Enter Sleep After Disconnection
•Disconnect after connection to save power:
C bt_conn_disconnect(conn, BT_HCI_ERR_REMOTE_USER_TERM_CONN); |
5. Key Points for Co-Design
(1) Low Power Strategy
•Peripheral Role:Reduce the number of active wake-ups.
•Broadcast Interval:Adjust according to the scenario (sensor broadcasts once a minute vs. Beacon broadcasts once a second).
(2) Communication Optimization
•Batch Transmission:Package multiple sets of data for transmission.
•Use Notification Mechanism:Reduce power consumption caused by polling.
(3) Security and Compatibility
•Encrypted Transmission:Sensitive data must enable encryption (e.g., smart home control).
•UUID Standardization:Use standard UUIDs defined by Bluetooth SIG (e.g., heart rate service 0x180D).
6. Common Questions and Solutions
Q1: Data transmission is unstable?
•Reason:Connection interval is too long or severe interference.
•Solution:
Makefile CONFIG_BT_CONN_INTERVAL_MIN=75 → 7.5ms connection interval |
Q2: Beacon positioning accuracy is insufficient?
•Reason:Broadcast interval is too long or signal strength is weak.
•Solution:
○Increase broadcast power:
Makefile CONFIG_BT_BLE_TX_POWER=4 → Maximum transmission power |
Q3: High latency in sensor data?
•Reason:Collection frequency is too low or MTU limitation.
•Solution:
C bt_gatt_exchange_mtu(conn); // Increase MTU to reduce transmission times |
Summary
The core processes of typical BLE application scenarios include:
1.Device initialization and role definition → 2. Broadcasting/Scanning and Connection → 3. Service Discovery and Data Interaction → 4. Low Power Maintenance and Disconnection.