13 / 20
通过蓝牙对话
让虚拟手机配对并向开发板发送 ON 或 OFF。
运行此实验 →实验目标
配对手机后发送两种指令,观察 LED 与接收日志。
工作原理
让虚拟手机配对并向开发板发送 ON 或 OFF。 不需要。经典蓝牙 SPP 是直连链路,由原始 ESP32 支持,并非所有新型号都有。 此处为配套教学模型,硬件示例基于经典 ESP32 与 Arduino-ESP32 3.x;网络、声波、负载和机械运动均为简化演示。
动手试一试
- 打开此实验
- 配对手机后发送两种指令,观察 LED 与接收日志。
接线
- Bluetooth SPP — 经典 ESP32 与兼容客户端
- GPIO 2 — LED 输出
示例代码
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
// Bluetooth Classic SPP: original ESP32, compatible client required.
void setup() {
pinMode(2, OUTPUT);
SerialBT.begin("ESP32-Learning");
}
void loop() {
if (SerialBT.available()) {
String command = SerialBT.readStringUntil('\n'); command.trim();
if (command == "ON") digitalWrite(2, HIGH);
if (command == "OFF") digitalWrite(2, LOW);
SerialBT.println(command);
}
}检查理解
蓝牙串口需要路由器吗?
查看答案
不需要。经典蓝牙 SPP 是直连链路,由原始 ESP32 支持,并非所有新型号都有。