
ESP32 最容易被忽视的宝藏功能就是自带蓝牙。相比需要外接模块的 Arduino,ESP32 芯片内部集成了完整的蓝牙双模射频,Classic 蓝牙和 BLE 都能用。本文聚焦 Classic 蓝牙(SPP 串口透传),从协议栈结构讲到串口互通,最后做一个手机蓝牙控制继电器的实战项目。
Classic 与 BLE 怎么选
ESP32 的蓝牙分为两种模式:经典蓝牙(Bluetooth Classic)面向持续、大吞吐的数据流,典型场景是音频传输和串口透传;低功耗蓝牙(BLE)牺牲吞吐换取极低功耗,适合电池供电的传感器节点间歇上报数据。本文的串口透传、手机控灯这类"双向聊天"需求,用 Classic 的 SPP 协议最直接——写法上几乎就是把硬件串口换成蓝牙串口对象。
协议栈结构:Host 与 Controller 分层
ESP32 的蓝牙实现分两层:蓝牙控制器(Bluetooth Controller)负责射频与底层链路,主机栈(Host)负责协议逻辑。Arduino 环境下 Host 侧默认跑 Bluedroid 栈(也可换 Apache NimBLE,BLE 场景更省资源),两层之间通过 VHCI 接口交互;控制器对外还可以经 UART/SDIO 直连外部主机做 HCI 透传,不过日常开发用不到这一层。理解这结构的意义在于:我们写的 BluetoothSerial 代码属于 Host 层,底层射频、跳频、重传都由芯片固件搞定。
alt="ESP32蓝牙协议栈分层结构图" />注意一点:SPP(串行端口协议)只有经典蓝牙才支持,ESP32-S2 这类没有蓝牙的芯片自然用不了;ESP32-C3/S3 只支持 BLE,也没有 SPP。选板子时别搞混。
第一个例程:蓝牙串口透传
官方库自带 BluetoothSerial,无需安装第三方依赖。下面这个例程把电脑串口监视器和手机蓝牙之间双向打通:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
}
delay(20);
}上传后打开串口监视器(115200 波特率),看到"The device started, now you can pair it with bluetooth!"即初始化成功:
alt="串口监视器显示蓝牙初始化成功" />两个 #if !defined 预编译检查值得留意:它们在编译期确认固件已开启蓝牙和 SPP,缺配置会直接 #error 报错,省去运行期排查。
手机配对与收发测试
Android 手机端推荐装 Serial Bluetooth Terminal 应用。步骤:
- 打开手机蓝牙,搜索新设备,找到名为
ESP32test的设备并配对。 - 打开 App,在菜单里选择 Devices,从已配对列表中选中 ESP32test。
- 连接成功后即可像聊天一样收发消息。
在 App 里发一条消息,它会经蓝牙送到 ESP32,再由透传代码转发到 USB 串口显示;反向从串口监视器发的内容也会出现在手机上。双向链路验证通过,说明环境完全就绪。
实战项目:手机蓝牙控制继电器
透传只是热身,来做个有用的:手机发 on/off 无线控制继电器,稍加扩展就是蓝牙智能插座的基础。接线很简单——继电器模块的 IN 接 GPIO5,VCC 接 5V,GND 共地(下图以二路继电器为例,单路接法相同):
alt="ESP32与继电器模块接线图" />代码在透传基础上增加了消息拼接和指令判断:
#include "BluetoothSerial.h"
#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif
#if !defined(CONFIG_BT_SPP_ENABLED)
#error Serial Bluetooth not available or not enabled. It is only available for the ESP32 chip.
#endif
// GPIO where relay is connected to
const int relayPin = 5;
// Handle received messages
String message = "";
// Create BluetoothSerial object
BluetoothSerial SerialBT;
void setup() {
// Begin serial communication with Arduino IDE (Serial Monitor)
Serial.begin(115200);
// Initialize relayPin as an output
pinMode(relayPin, OUTPUT);
digitalWrite(relayPin, HIGH);
// Initialize the Bluetooth stack
SerialBT.begin("ESP32test"); //Bluetooth device name
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
if (SerialBT.available()){
char incomingChar = SerialBT.read();
if (incomingChar != '\n'){
message += String(incomingChar);
}
else{
message = "";
}
Serial.write(incomingChar);
}
// Check received message and control output accordingly
if (message == "on"){
digitalWrite(relayPin, LOW);
}
else if (message == "off"){
digitalWrite(relayPin, HIGH);
}
delay(20);
}逐字符读入并拼成字符串,遇到换行符清空,这样 message 始终是最新一条完整指令。多数继电器模块低电平触发,所以 on 对应 digitalWrite(relayPin, LOW)。在手机 App 发送 on/off,继电器随即吸合、断开,串口侧也能看到收到的指令回显:
alt="手机发送on/off控制继电器的串口回显" />基于这个框架可以继续扩展:把继电器换成四路同时控制、加一条 status 指令回传当前开关状态,或者接 DHT11 传感器把温湿度实时推回手机——一个不带 App 开发的"私有物联网"就这样搭起来了。











BluetoothSerial 这个用法太省心了,不用第三方库直接透传
低电平触发继电器那个提醒很关键,我第一次接反了直接没反应
S2 没蓝牙这个坑写得对,买板子前真得看清规格表
按这个加个 status 指令回传状态,扩展开销很小,思路不错