esp32s3 怎么把蓝牙模块添加到当前项目
时间: 2025-06-24 15:39:48 浏览: 16
### 如何在ESP32-S3项目中添加蓝牙模块
#### 蓝牙模块概述
ESP32-S3 是一款集成了 Bluetooth 5 (LE) 和 Wi-Fi 的 MCU 芯片,因此无需额外的硬件即可使用其内置的蓝牙功能。该芯片支持 BLE(低功耗蓝牙)和经典蓝牙协议[^3]。
---
#### 配置方法
##### 1. **启用蓝牙功能**
为了在 ESP32-S3 上使用蓝牙功能,需先初始化蓝牙驱动并设置相应的参数。这通常通过 `esp_bt_controller_init` 函数完成。以下是必要的步骤:
- 初始化蓝牙控制器。
- 设置蓝牙设备名称。
- 启动 GAP(Generic Access Profile)层以允许设备扫描和服务发现。
```c
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
void init_bluetooth() {
esp_err_t ret;
// 初始化蓝牙控制器
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret != ESP_OK) {
printf("Error initializing bluetooth controller\n");
return;
}
// 启动蓝牙控制器
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret != ESP_OK) {
printf("Error enabling bluetooth controller\n");
return;
}
// 注册GAP事件回调函数
ret = esp_ble_gap_register_callback(gap_event_handler); // gap_event_handler 自定义实现
if (ret != ESP_OK) {
printf("Error registering GAP callback\n");
return;
}
// 设置蓝牙设备名称
const char *device_name = "ESP32_S3_Bluetooth";
ret = esp_ble_gap_set_device_name(device_name);
if (ret != ESP_OK) {
printf("Error setting device name\n");
return;
}
}
```
---
##### 2. **实现 GAP 层回调**
GAP 层负责管理蓝牙设备之间的基本连接过程。以下是一个简单的 GAP 回调函数示例:
```c
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
printf("Advertising data set successfully.\n");
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
printf("Scan response data set successfully.\n");
break;
default:
break;
}
}
```
---
##### 3. **配置广播数据**
为了让其他设备检测到 ESP32-S3,需要设置广告数据和扫描响应数据。以下是如何配置这些数据的代码片段:
```c
uint8_t adv_data[] = {
0x02, 0x01, 0x06, // Flags: General discoverable mode and BR/EDR not supported.
0x03, 0x03, 0xAA, 0xBB, // Service UUIDs: Example service UUID is AA-BB
};
uint8_t scan_rsp_data[] = {
0x0A, 0x09, 'E', 'S', 'P', '-', '3', '2', '-', 'S', '3' // Complete local name
};
// 设置广告数据
esp_err_t ret = esp_ble_gap_set_adv_data(adv_data, sizeof(adv_data));
if (ret != ESP_OK) {
printf("Failed to set advertising data\n");
}
// 设置扫描响应数据
ret = esp_ble_gap_set_scan_rsp_data(scan_rsp_data, sizeof(scan_rsp_data));
if (ret != ESP_OK) {
printf("Failed to set scan response data\n");
}
```
---
##### 4. **启动广播**
最后一步是启动广播以便其他设备可以发现 ESP32-S3 设备。
```c
esp_ble_gap_start_advertising(&(esp_ble_adv_params_t){
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
});
```
---
#### 示例代码总结
完整的代码结构如下所示:
```c
#include "esp_log.h"
#include "esp_bt.h"
#include "esp_gap_ble_api.h"
#define TAG "BLUETOOTH_DEMO"
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param);
void app_main(void) {
esp_err_t ret;
// 初始化蓝牙控制器
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
ret = esp_bt_controller_init(&bt_cfg);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Bluetooth controller initialization failed.");
return;
}
// 启动蓝牙控制器
ret = esp_bt_controller_enable(ESP_BT_MODE_BLE);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Bluetooth controller enable failed.");
return;
}
// 注册GAP事件回调函数
ret = esp_ble_gap_register_callback(gap_event_handler);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "GAP register failed.");
return;
}
// 设置蓝牙设备名称
const char *device_name = "ESP32_S3_Bluetooth";
ret = esp_ble_gap_set_device_name(device_name);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Set device name failed.");
return;
}
uint8_t adv_data[] = {
0x02, 0x01, 0x06, // Flags: General discoverable mode and BR/EDR not supported.
0x03, 0x03, 0xAA, 0xBB, // Service UUIDs: Example service UUID is AA-BB
};
uint8_t scan_rsp_data[] = {
0x0A, 0x09, 'E', 'S', 'P', '-', '3', '2', '-', 'S', '3'
};
// 设置广告数据
ret = esp_ble_gap_set_adv_data(adv_data, sizeof(adv_data));
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Set advertising data failed.");
return;
}
// 设置扫描响应数据
ret = esp_ble_gap_set_scan_rsp_data(scan_rsp_data, sizeof(scan_rsp_data));
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Set scan response data failed.");
return;
}
// 启动广播
ret = esp_ble_gap_start_advertising(&(esp_ble_adv_params_t){
.adv_int_min = 0x20,
.adv_int_max = 0x40,
.adv_type = ADV_TYPE_IND,
.own_addr_type = BLE_ADDR_TYPE_PUBLIC,
.channel_map = ADV_CHNL_ALL,
.filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
});
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Start advertising failed.");
return;
}
}
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
switch (event) {
case ESP_GAP_BLE_ADV_DATA_SET_COMPLETE_EVT:
ESP_LOGI(TAG, "Advertising data set successfully.");
break;
case ESP_GAP_BLE_SCAN_RSP_DATA_SET_COMPLETE_EVT:
ESP_LOGI(TAG, "Scan response data set successfully.");
break;
default:
break;
}
}
```
---
#### 注意事项
- 确保开发环境中已安装最新的 ESP-IDF 版本,并正确配置了编译选项。
- 如果需要更复杂的蓝牙服务(如 GATT Server 或 Client 功能),则需要进一步研究 `esp_gatt_server.c` 和 `esp_gatt_client.c` API 文档[^3]。
---
阅读全文
相关推荐


















