Skip to content

First, What Is It

  • BLE (Bluetooth Low Energy): a low-power wireless protocol family since Bluetooth 4.0, used by phones, wearables, and sensors; Ai-WB2 (BL602) has built-in BLE 5.0.
  • Advertising & connecting: a device first "announces" itself (advertising) so others can find it; after a connection, both sides exchange data according to GATT rules.
  • GATT (Generic Attribute Profile): the rules for "how data is organized and accessed" after connection — Service → Characteristic → Value, like "drawer inside drawer inside file".
  • Why it matters: GATT hierarchy, MTU, and notify vs indicate are classic interview topics, and any wristband/remote/sensor project needs them.

Breaking Down the Principle

1. Role Stack: From Radio to Application

The SDK BLE example startup order matches this stack exactly: btble_controller_init (controller) → hci_driver_init (HCI) → bt_enable (starts the host stack GAP/GATT).

2. Advertising and Connecting (GAP)

  • Advertising: before a connection, the peripheral periodically sends advertisement packets (device name, manufacturer data, service UUIDs, etc.); the central scans and "sees" it.
  • Connection: the central initiates the connection; after the link is established advertising stops, and both sides communicate on the connection parameters (interval, latency, timeout).
  • Roles: the peripheral advertises/waits; the central scans/connects; a phone app is usually the central.

3. GATT Data Organization: Service → Characteristic → Value

LevelMeaningExample
ServiceA collection of related characteristics with a UUIDBattery service 0x180F
CharacteristicOne data point: value + propertiesBattery level 0x2A19
DescriptorExtra description, e.g., CCCD controls notify on/offClient Characteristic Configuration Descriptor

The SDK peripheral example registers a test service with characteristics covering read/write/write-without-response/notify/indicate — perfect for understanding this structure.

4. ATT: Handles, Attributes, and MTU

  • Every attribute (service, characteristic, value) has a handle; GATT operations address attributes by handle.
  • MTU (Maximum Transmission Unit): how many bytes one L2CAP packet can carry (default 23 bytes, negotiable higher). Data larger than the MTU must be split across packets.
  • Notify vs Indicate:
    • Notify: the peripheral pushes data without confirmation — fast, may drop.
    • Indicate: the central must acknowledge (ACK) — reliable but slower.
    • Enabling either is done through the central writing the CCCD (Client Characteristic Configuration Descriptor) — that is what "subscribe" really means.

5. Security Mechanisms

  • Pairing/bonding: exchanging keys and encrypting the link.
  • Common cryptography: AES-CCM encrypts data; security level affects usability.
  • Connection parameter update: both sides negotiate the connection interval — longer interval saves power but adds latency.

How the SDK Implements It

The peripheral example's test-service code shows the "define characteristics → build service → register" GATT flow:

c
/* ble_tp_svc.h: test service UUID 07af27a5-9c22-11ea-9afe-02fcdc4e7412 */
ble_tp_init();                       /* register read/write/notify/indicate characteristics */
bt_le_adv_start(&param, adv_data, n, adv_rsp, m);   /* connectable advertising */

On the central side, bt_le_scan_start scans, bt_gatt_discover discovers services/characteristics after connecting, then read/write/subscribe by handle — mirroring what a phone app does.

Common Exam & Interview Questions

How are GATT, ATT, and GAP related?

GAP handles discovery and connection (advertising/scanning/roles); after connection, GATT defines how data is organized (services/characteristics); GATT sits on the ATT attribute table, which runs over L2CAP channels.

Difference among service, characteristic, and descriptor?

A service is a functional collection; a characteristic is a concrete data point (value + properties); a descriptor describes the characteristic (e.g., CCCD controls notify/indicate). Clients use them in the order "discover service → discover characteristic → read/write/subscribe".

Difference between notify and indicate?

Notify requires no acknowledgment — fast but lossy; indicate requires acknowledgment — reliable but slower. Choose based on how important the data is.

What is MTU and why does it matter?

MTU is the maximum bytes per L2CAP packet (default 23). Negotiating a larger MTU (e.g., 247) reduces packetization overhead and speeds up bulk transfers.

Can advertising and connection happen at the same time?

Usually advertising stops once a connection is established; it can resume after disconnect (the SDK peripheral example restarts advertising in the disconnect callback).

Have questions?

For any other questions, visit the unified Q&A and discussion board: Ai-Thinker Discussions

Released under the MIT License. Build Time 2026-09-11 14:52:23