Welcome to MicroROS V2 Robot Car repository
17. Bluetooth Communication
Bluetooth Communication
I. Experiment Objective
Learn the BLE (Bluetooth Low Energy) communication function of the MicroROS-V2 control board. Through this experiment, you will master how to initialize a BLE GATT server, create an SPP service with characteristic values, and implement data transmission and reception with a phone or other BLE devices.
II. Experiment Principle
1. Keywords
| Keyword | Description |
|---|---|
| BLE (Bluetooth Low Energy) | Bluetooth Low Energy technology, designed for low-power devices. It supports short-range data communication and is widely used in IoT devices. |
| GATT (Generic Attribute Profile) | The Generic Attribute Protocol, which defines the architecture of data communication between BLE devices, based on a client-server model. |
| SPP (Serial Port Profile) | Serial Port Profile, a service mode that emulates traditional Bluetooth serial communication over BLE. |
| Characteristic | The data unit in a GATT service, containing a value, properties, and descriptors. It is the actual carrier of data reads and writes. |
| UUID | Universally Unique Identifier, used to uniquely identify BLE services and characteristic values (16-bit/128-bit). |
| Advertising | A BLE device periodically sends advertising packets to announce its presence so that peripheral devices can discover and connect to it. |
2. Technical Architecture
This example implements a BLE GATT server based on the ESP32 Bluedroid protocol stack. After startup, the program initializes NVS Flash, the Bluetooth controller, and the Bluedroid protocol stack, registers a GATT application, and creates an SPP service (UUID 0xFFE0) with 4 characteristic values for data transmission and reception. The device advertises under the name "YahBoom_BL" and waits for a phone or other BLE client to connect.
Main component files:
main/main.c— Program entry, initializes BLEcomponents/ble_server/ble_server.c— BLE GATT service implementationcomponents/ble_server/ble_server.h— Characteristic index and constant definitions
III. Experiment Steps
1. Hardware Preparation
| Hardware | Requirement |
|---|---|
| MicroROS-V2 control board | Required |
| USB data cable | Required |
| A phone that supports BLE (with nRF Connect or another Bluetooth debugging tool installed) | Required |
2. Prerequisites
Set up the ESP32-IDF development environment
Connect the MicroROS-V2 control board to the computer via USB
First, the Bluetooth feature is disabled by default in ESP-IDF, so you need to enable Bluetooth in the configuration file.
cd ~/esp/base_samples/bluetoochidf.py menuconfig
Because only the Bluetooth BLE 5.0 feature is enabled by default, to make the compilation pass, you need to manually enable the Bluetooth BLE 4.2 feature.

3. Compile and Flash
x
cd ~/esp/base_samples/bluetoochsource ~/esp/esp-idf/export.shidf.py build flash monitor
4. Stopping Method
In the terminal, press Ctrl+] to exit the serial monitor.
IV. Experiment Result
After successful compilation and flashing, the Monitor will output:
xxxxxxxxxxhello yahboomI (xxx) MAIN: Nice to meet you!I (xxx) BLE_SERVER: BLE_Init init bluetooth
Using a BLE debugging tool such as nRF Connect or LightBlue on your phone, you can scan for the BLE device named YahBoom_BL. After connecting, you can see the service with UUID 0xFFE0 and its 4 characteristic values underneath. You can write data to the 0xFFE1 characteristic, and the control board will print the received data in the log.
V. Program Analysis
Source code paths:
Firmware source:
~/esp/base_samples/bluetooch/main/main.cBLE service:
~/esp/base_samples/bluetooch/components/ble_server/ble_server.h,ble_server.c
1. Main Program Logic
main/main.c is the program entry. After printing a welcome message, it calls BLE_Init() to initialize the BLE protocol stack:
xxxxxxxxxx
void app_main(void){ printf("hello yahboom\n"); ESP_LOGI(TAG, "Nice to meet you!"); BLE_Init();}2. BLE Service Initialization Flow
The BLE_Init() function in components/ble_server/ble_server.c completes the full initialization of the BLE protocol stack with the following steps:
Initialize NVS Flash — Call
nvs_flash_init()to initialize non-volatile storage for saving Bluetooth configuration information. If the NVS partition is abnormal, erase it first and then initialize.Release classic Bluetooth memory — Call
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT)to release the memory occupied by classic Bluetooth, keeping only the BLE function.Initialize the Bluetooth controller — Start the BLE controller through
esp_bt_controller_init()andesp_bt_controller_enable(ESP_BT_MODE_BLE).Initialize Bluedroid — Call
esp_bluedroid_init()andesp_bluedroid_enable()to start the Bluedroid protocol stack.Register callback functions — Register the GATT Server callback and the GAP callback.
Register the application — Call
esp_ble_gatts_app_register(ESP_SPP_APP_ID)to register the SPP application, whereESP_SPP_APP_IDis defined as0x56.
3. BLE Device Name and Advertising Data
The device advertising name is YahBoom_BL, and the advertising data packet format is as follows:
xxxxxxxxxxstatic const uint8_t spp_adv_data[] = { 0x02, 0x01, 0x06, /* Flags */ 0x03, 0x03, 0xF0, 0xAB, /* 16-bit Service Class UUIDs */ 0x0B, 0x09, 'Y', 'a', 'h', 'B', 'o', 'o', 'm', '_', 'B', 'L' /* Complete Local Name */};Advertising parameter settings:
xxxxxxxxxxstatic esp_ble_adv_params_t spp_adv_params = { .adv_int_min = 0x20, // Minimum advertising interval .adv_int_max = 0x40, // Maximum advertising interval .adv_type = ADV_TYPE_IND, // Connectable advertising type .own_addr_type = BLE_ADDR_TYPE_PUBLIC, .channel_map = ADV_CHNL_ALL, .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,};4. SPP Service and Characteristic Values
This example implements a BLE service similar to SPP (Serial Port Profile). The service UUID is 0xFFE0 and it contains 4 characteristic values:
| Characteristic | UUID | Properties | Description |
|---|---|---|---|
| SPP_DATA_RECEIVE | 0xFFE1 | Read/Write | Data reception channel, used to receive data sent from the phone |
| SPP_DATA_NOTIFY | 0xFFE2 | Read/Notify | Data notification channel, used to send data notifications to the phone |
| SPP_COMMAND_RECEIVE | 0xFFE3 | Read/Write | Command reception channel, used to receive commands sent from the phone |
| SPP_COMMAND_NOTIFY | 0xFFE4 | Read/Notify | Command status notification channel, used to notify the phone of status changes |
The data length limits of each characteristic (defined in ble_server.h):
SPP_DATA_MAX_LEN= 512 bytes (maximum length of the data channel)SPP_CMD_MAX_LEN= 20 bytes (maximum length of the command channel)SPP_STATUS_MAX_LEN= 20 bytes (maximum length of the status channel)
5. Data Reception and Processing
The gatts_profile_event_handler handles the ESP_GATTS_WRITE_EVT event:
When the write handle is
SPP_IDX_SPP_DATA_RECV_VAL, callBLE_Parse_Rx_Data()to process the received dataWhen the write handle is
SPP_IDX_SPP_COMMAND_VAL, save the command data to the bufferWhen the write handle is
SPP_IDX_SPP_DATA_NTF_CFG, enable or disable the notification function according to the CCCD value written by the client
6. Key Components
ble_server.h defines the characteristic index enum and key constants:
Parameter definition file:
~/esp/base_samples/bluetooch/components/ble_server/ble_server.h
| Definition | Value | Description |
|---|---|---|
SPP_IDX_SVC | 0 | Service declaration index |
SPP_IDX_SPP_DATA_RECV_VAL | 2 | Data reception characteristic index |
SPP_IDX_SPP_DATA_NTY_VAL | 4 | Data notification characteristic index |
SPP_IDX_SPP_COMMAND_VAL | 7 | Command characteristic index |
SPP_IDX_SPP_STATUS_VAL | 9 | Status characteristic index |
SPP_IDX_NB | 11 | Total number of attribute table entries |
7. Connection and Disconnection Events
Connection event (ESP_GATTS_CONNECT_EVT): Save the connection ID and the GATT interface handle, and set
is_connected = trueDisconnection event (ESP_GATTS_DISCONNECT_EVT): Reset the connection state and restart advertising to wait for a new connection
MTU negotiation (ESP_GATTS_MTU_EVT): Save the negotiated MTU size to
spp_mtu_size, with an initial value of 23 bytes
VI. Common Problems
| Problem | Cause | Solution |
|---|---|---|
| Cannot scan the YahBoom_BL device | Advertising did not start normally or the controller is faulty | Check the Monitor log and confirm that BLE initialization succeeded |
| Cannot connect to the BLE device | Bluetooth compatibility issue with the phone or computer | Try using a different BLE debugging tool |
| NVS initialization fails | The NVS partition data is corrupted | Run idf.py erase_flash to erase and re-flash |