BLE Example

[中文]

In the BLE (Bluetooth Low Energy) protocol stack, communication between devices is accomplished by different layers working together. Among them, BLE GAP (Generic Access Profile) is responsible for the general access configuration of BLE devices, such as device broadcasting, scanning, connection and connection parameter management, realizing the functions of device discovery and communication establishment. The GATT (Generic Attribute Profile) layer defines how devices organize, access, and transmit attribute data. It manages device data through a hierarchical structure of “Service—Characteristic—Descriptor”, enabling clients to discover and operate server-side functions and states in a unified manner. For further explanation, please refer to GATT layer.

In the GATT architecture, the GATT Server is the device that stores attribute data and is responsible for exposing services and characteristics to the client; the GATT Client obtains data or controls the device by reading, writing, or subscribing to characteristics. Simply put, the Server provides data interfaces, and the Client interacts with these interfaces.

This function is often used in scenarios such as data collection, status reporting, and control instruction reception of BLE peripherals. For example, in smart bracelets, Bluetooth speakers, or sensor devices, the GATT Server provides core data interfaces, and the GATT Client (such as mobile applications) communicates with the device through these interfaces.

This document summarizes two BLE examples provided by the official ESP-IDF: GATT Server Service Table and GATT Client. The examples use the Bluedroid protocol stack. For reference to the NimBLE implementation, please refer to the official NimBLE example description.

For ease of understanding and use, the content of the document is divided into two parts:

  1. General Steps Document: Summarizes the common processes of various BLE modes, including initialization, driver configuration, GAP settings, GATT service and characteristic registration, broadcasting and scanning, connection management, and event handling, etc., providing a unified reference for examples.

  2. Example Document: Explains the implementation of each example by function, the common parts avoid repetition by referencing the general steps document, and only retain the logic and parameter settings specific to the example.

This layered structure facilitates understanding the overall process in the general steps document first, and then learning the implementation details of specific functions in the example document, thus more efficiently understanding the working principles and design methods of the BLE module. For more content on BLE basics and working principles, please refer to Low Power Bluetooth.