1. 首先,确保 ESP32 板子上已经安装了蓝牙模块,如 HC-05 或 HC-06。

  2. 在 Arduino IDE 中打开一个新的项目,并导入 ESP32 的蓝牙库。

  3. 在代码中初始化蓝牙模块,并设置蓝牙名称和 PIN 码。

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin('ESP32_BT'); // 设置蓝牙名称
  Serial.println('The device started, now you can pair it with your phone.');
  SerialBT.setPin('1234'); // 设置 PIN 码
}
  1. 在代码中通过 SerialBT.available() 函数检查蓝牙是否有数据输入。
void loop() {
  if (SerialBT.available()) {
    Serial.write(SerialBT.read());
  }
  if (Serial.available()) {
    SerialBT.write(Serial.read());
  }
}
  1. 烧录代码到 ESP32 板子上,并将蓝牙模块连接到板子的 UART 接口。

  2. 将手机的蓝牙打开,并搜索可用设备,找到 'ESP32_BT' 并配对连接。

  3. 在手机上打开一个串口终端应用程序,例如 Serial Bluetooth Terminal,设置好通讯参数,连接到 'ESP32_BT',即可实现蓝牙通讯。

注意:在配对连接时,需要输入与设备设置的 PIN 码相同的密码。

ESP32 蓝牙模块连接手机教程 - 配对与通讯

原文地址: https://www.cveoy.top/t/topic/m7Tr 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录