Contributed by Dachuizi, organized by Ai-Thinker
[WB2 BLE Learning-1] BLE Data Broadcasting
Abstract
After studying the BLE tutorial shared by Bilibili content creator "我是鹏老师", combined with various other technical shares and the help of deepseek, I tried to verify the features on the WB2-32S-KIT development board to deepen my understanding. 我是鹏老师's BLE course is very suitable for getting started with BLE development. It does not dwell too much on the boring underlying protocol stack. It uses a learn-while-experimenting approach, which makes it easier for beginners to get started; the address is at the end of the article;
Preparation
WB2-32S-KIT development board, with BLE and WiFi functions;
A BLE debugging assistant installed on the computer or phone. I installed the nRF Connect MAC version;
A USB-to-serial tool, used to connect to the other serial port on the development board;
Experiment Steps
- First connect the WB2 to the computer via USB, then open the terminal and check the currently connected serial ports:
Among them, /dev/tty.wchusbserial14240 is used for log output and flashing;
The other one is for the USB-to-serial tool, which will be needed later;
- Use VSCode to open the downloaded WB2 SDK. In the /Ai-Thinker-WB2/make_scripts_riscv directory, the serial port used for flashing in project.mk is /dev/tty.wchusbserial14240:

The reason for this modification was already explained in the unboxing LED-blinking article, so I won't repeat it here.
- In the terminal, cd to the /Ai-Thinker-WB2/applications/bluetooth/ble_slave directory and run make -j to compile:

- After compilation succeeds, run make flash

When prompted, press the reset button:

Seeing "all success" means flashing succeeded;
- Use the BLE debugging tool to start scanning; you can see a BLE device named
Ai-thinker:

After connecting, you can see the services and characteristics provided by the BLE device;

Experiment Analysis
Program Analysis
Main Program

From the main program, we can see that it first performs system initialization and then prints log messages. Next, it creates a FreeRTOS task and a proc_main_entry task; personally, I think this name was picked arbitrarily;
Serial Task
The serial task is very simple: it initializes a serial port and continuously listens for received data, then sends it out over BLE. Note that the API used for sending here is: notify;

BLE Entry Task
After this task finishes executing, it is deleted; it probably only performs BLE startup:

Let's look further into the implementation of the apps_ble_start() function:

The general flow is:
BLE stack startup, that is, some low-level hardware startup functions;
BLE slave mode initialization;
Two callback functions are registered;
I won't look at the startup flow of the underlying BLE protocol stack for now; I'll study it later when I have the chance;
Here let's focus on the implementation of the ble_slave_init() function:

The first two functions register the connection-success callback and the disconnection callback respectively;
The ble_server_init() function is used to create and register services and characteristics;
The ble_salve_adv() function sends advertising and waits for a connection;
Program Verification
Verifying the Advertising Data

In the figure above, salve_adv is the advertising data,
The first line of advertising data is:
#define BT_DATA_FLAGS 0x01 /* AD flags */
The value is:
#define BT_LE_AD_GENERAL 0x02 /* General Discoverable */
#define BT_LE_AD_NO_BREDR 0x04 /* BR/EDR not supported */
From the screenshot above, we can also see that the device is connectable;
The second line is the device name:
#define BT_DATA_NAME_COMPLETE 0x09 /* Complete name */
The name is:
#define ble_slave_name "Ai-thinker"
Verifying the Service and Characteristic UUIDs
The uuid in the code is defined like this:

From the screenshot above, we can also see that the uuids are consistent;
Verifying the Connection and Disconnection Callbacks:

Serial output on disconnection:

Serial output on reconnection:

Verifying Serial Message Sending
Serial sends data:

The BLE assistant receives:

That's all for this session. I'll share more next time.
Finally, here is the free BLE tutorial link from 我是鹏老师:

