Understanding BLE Stack: A Beginner’s Guide

Understanding BLE Stack: A Beginner’s Guide

Hello everyone! Today, I want to talk to you about the Bluetooth Low Energy (BLE) stack. As an embedded developer, I know that many beginners can be intimidated by the complex architecture of BLE. Don’t worry, I will use the simplest language to help you clarify the ins and outs of the BLE stack. Imagine that the BLE stack is like a five-story apartment building, with each floor having its unique function, working together to accomplish the big task of Bluetooth communication.

1. Physical Layer: The Infrastructure of Communication

The physical layer is like the foundation and plumbing system of the apartment. At this layer, BLE uses the 2.4GHz frequency band for wireless communication, converting digital signals into radio waves through modulation.

c copy

// A simple physical layer configuration example
ble_phy_config_t phy_config = {
    .tx_power = TX_POWER_0_DBM,    // Transmission power set to 0dBm
    .rf_channel = 37,              // Use broadcast channel 37
    .phy_mode = BLE_PHY_1M         // Use 1Mbps data transmission rate
};

Tip: BLE uses 40 RF channels, of which 3 are broadcast channels (37, 38, 39), and the rest are data channels.

2. Link Layer: The Traffic Police of Communication

The link layer is like the security system of the apartment, responsible for managing connections between devices. It handles the sending and receiving of packets, ensuring the reliability of communication.

c copy

// Link layer connection parameter configuration example
ble_gap_conn_params_t conn_params = {
    .min_conn_interval = 16,    // Minimum connection interval 20ms (16 * 1.25ms)
    .max_conn_interval = 32,    // Maximum connection interval 40ms (32 * 1.25ms)
    .slave_latency = 0,         // Slave latency
    .conn_sup_timeout = 400     // Supervision timeout 4s (400 * 10ms)
};

Note: The settings of connection parameters directly affect the device’s power consumption and response speed, and need to be weighed according to the actual application scenario.

3. Host Layer: The Brain of Communication

The host layer includes important protocols like L2CAP, ATT, GATT, and GAP, just like the management office of the apartment.

c copy

// GATT service definition example
BLE_SERVICE_DEF(my_service,
    BLE_UUID_TYPE_VENDOR_BEGIN,        // Custom UUID
    BLE_CHARACTERISTIC_DEF(
        my_char,
        BLE_GATT_HVX_NOTIFICATION,     // Supports notification feature
        BLE_GATTS_VLOC_STACK,          // Characteristic value storage location
        20                             // Maximum length 20 bytes
    )
);

4. Application Layer: The Actual Business Logic

This is the layer we interact with the most, just like the residents living in the apartment.

python run copy

# A simple BLE application example
def on_ble_data_received(data):
    if data.characteristic_uuid == TEMPERATURE_CHAR_UUID:
        temperature = struct.unpack('f', data.value)[0]
        print(f"Received temperature data: {temperature}C")

Practice Assignment

  1. Try to configure a basic BLE advertising packet that includes the device name.
  2. Implement a simple GATT service that includes a readable and writable characteristic.

Summary of Learning Points:

  • The BLE stack is divided into physical layer, link layer, host layer, and application layer.
  • The physical layer is responsible for actual wireless communication.
  • The link layer manages device connections and packet transmission.
  • The host layer provides abstraction for services and characteristics.
  • The application layer implements specific business logic.

Remember, the best way to learn the BLE stack is through hands-on practice. Start with simple broadcasts and gradually dive into more complex services and characteristic operations. Don’t be intimidated by complex concepts; each function is designed to solve a specific problem.

Happy learning, and next time we will delve into the implementation of BLE security mechanisms!

(End of article)

Leave a Comment