Overview

[中文]

This document is a development note for ESP32 series BLE applications, mainly introducing the basic concepts of ESP32 series BLE application development, the development environment, and some generally applicable precautions in the BLE development process based on NimBLE, and organizing the necessary documents for development.

Introduction to ESP32 Series Bluetooth Features

ESP32 has multiple chips that support Bluetooth, and the current support for Bluetooth can be divided into the following types:

Bluetooth Features of ESP32 Series Chips

Chip Model

Bluetooth Certification Version

Function Support

ESP32

4.2

The controller is certified by Bluetooth 4.2

ESP32-C2

5.3

The controller is certified by Bluetooth 5.3, supporting all features of BLE 5.0 and below

ESP32-S3, ESP32-C3

5.3

The controller is certified by Bluetooth 5.3, supporting all features of BLE 5.0 and below

ESP32-C6, ESP32-H2, ESP32-C61, ESP32-C5

6.0

Certified by Bluetooth 6.0, supporting all features of BLE 5.0 and below, and some features above BLE 5.0

For specific information about chip functions, you can choose the corresponding chip and refer to ESP32 Series Bluetooth Function Support Status

Explanation of Bluetooth Protocol Stack

ESP-IDF supports two types of Bluetooth host protocol stacks:

  1. Bluedroid: Supports both classic Bluetooth and low power Bluetooth

  2. NimBLE: Only supports low power Bluetooth

The functions of the two protocol stacks are consistent, but each has its own advantages and disadvantages, as follows:

  1. Bluedroid: The protocol stack architecture is clear, but it occupies a larger memory and firmware size

  2. NimBLE: The protocol stack implementation is simpler, and the memory and firmware size occupation is smaller. Developers need to have a deeper understanding of the Bluetooth protocol stack

Both protocol stacks provide a wealth of examples and documents for developers to refer to:

This document is mainly based on the NimBLE protocol stack, introducing the BLE development process and basic concepts. But the basic concepts and programming ideas are also applicable to the Bluedroid protocol stack.

Basic Concepts of BLE Programming

Event-driven programming model:

In ESP32’s NimBLE, the callback mechanism is the basis for the entire BLE application to respond to events. It allows the registration of some functions, and when specific events (such as connection establishment, disconnection, receiving notifications, reading and writing GATT characteristics, etc.) occur, the NimBLE stack automatically calls these functions for processing.

Basic Roles of BLE

There are mainly the following roles in BLE:

  1. Central Device - The device that initiates the connection - Responsible for managing connections and data exchange

  2. Peripheral Device - The device waiting to be connected - Provides data and services

  3. Broadcaster - Only sends broadcast data - Does not establish a connection - Suitable for applications such as beacons

  4. Observer - Only receives broadcast data - Does not establish a connection - Suitable for data collection scenarios

In practical applications, a device can support multiple roles at the same time. For example, a smartwatch can act as a slave device connecting to a mobile phone, while also acting as a master device connecting to a heart rate band.

GAP (Generic Access Profile)

Responsible for BLE device discovery, connection management, broadcasting, etc.

GATT (Generic Attribute Profile)

Defines the data communication format of BLE.

For more introductions to basic BLE concepts, you can refer to Introduction to Low Power Bluetooth

BLE programming considerations:

  1. Ensure that only lightweight logic is processed in the callback. Time-consuming logic, complex logic is handled in other tasks.

  2. Avoid calling blocking functions in callbacks, such as vTaskDelay, etc.

Protocol stack deinitialization

NimBLE protocol stack deinitialization:

  1. Disconnect all connections, turn off broadcasting and scanning

  2. Call nimble_port_stop(); to stop the NimBLE protocol stack

  3. Call nimble_port_freertos_deinit(); to deinitialize FreeRTOS related resources

  4. Call nimble_port_deinit(); to deinitialize the NimBLE port

  5. Call esp_bt_controller_disable(); to disable the Bluetooth controller

  6. Call esp_bt_controller_deinit(); to deinitialize the Bluetooth controller

Bluedroid protocol stack deinitialization:

  1. Disconnect all connections, turn off broadcasting and scanning

  2. Call esp_bluedroid_disable(); to disable the Bluedroid protocol stack

  3. Call esp_bluedroid_deinit(); to deinitialize the Bluedroid protocol stack

  4. Call esp_bt_controller_disable(); to disable the Bluetooth controller

  5. Call esp_bt_controller_deinit(); to deinitialize the Bluetooth controller