ESP32S3 arduino通过蓝牙发送
时间: 2025-04-26 08:30:35 浏览: 55
### 使用 ESP32-S3 在 Arduino 环境中通过蓝牙发送数据
为了使 ESP32-S3 实现蓝牙数据传输,需先配置好Arduino开发环境并安装必要的库文件[^2]。一旦完成这些前置条件,就可以利用特定的代码片段来实现蓝牙数据的发送。
下面是一个简单的例子,展示了如何设置ESP32-S3作为蓝牙串行端口协议(SPP)服务器,并向其他蓝牙客户端发送字符串消息:
```cpp
#include "BluetoothSerial.h"
// 创建 BluetoothSerial 对象实例
BluetoothSerial SerialBT;
void setup() {
// 初始化串行通信用于调试目的
Serial.begin(115200);
// 启动蓝牙模块
SerialBT.begin("ESP32_SPP_SERVER"); // 设备名称
Serial.println("The device started, now you can pair it with bluetooth!");
}
void loop() {
// 检查是否有来自蓝牙连接的数据输入
if (Serial.available()) {
SerialBT.write(Serial.read()); // 将接收到的内容转发给蓝牙设备
}
// 定期发送测试信息到已配对的蓝牙设备
static unsigned long lastSendTime = 0;
const unsigned long sendInterval = 5000; // 设置每五秒发送一次
if ((millis() - lastSendTime) >= sendInterval){
String messageToSend = "Hello from ESP32!";
Serial.print("Sending: ");
Serial.println(messageToSend);
SerialBT.println(messageToSend); // 发送消息
lastSendTime = millis();
}
}
```
这段程序首先包含了`BluetoothSerial.h`头文件,这是操作ESP32蓝牙特性的必需组件之一。接着定义了一个名为 `SerialBT` 的对象用来处理所有的蓝牙通讯事务。在初始化阶段(`setup()`函数),启动了硬件串行接口以便于监视器上的日志输出,并开启了蓝牙服务指定其可见的名字为 `"ESP32_SPP_SERVER"` 。循环体内的逻辑则负责监听任何可能到达的命令以及定时向外广播一条问候语句[^1]。
阅读全文
相关推荐


















