写在前面
经典蓝牙我们一般说的是BT,低功耗蓝牙我们一般说成BLE。
ESP32具有蓝牙功能,本例程将示范使用两块FireBeetle 2 ESP32-E进行蓝牙通信,通过其中一块ESP32向另一块ESP32发送数据。
测试代码
主机代码
#include "BluetoothSerial.h"
//创建蓝牙对象
BluetoothSerial SerialBT;
//从机MAC地址
uint8_t address[6] = {0xB0, 0xB2, 0x1C, 0xAB, 0x19, 0xD2};
//蓝牙状态
bool connected;
void setup()
{
//启用串口
Serial.begin(115200);
//开启蓝牙
SerialBT.begin("ESP32_master", true);
//打印提示
Serial.println("The device started in master mode, make sure remote BT device is on!");
//与从机配对
connected = SerialBT.connect(address);
//打印提示
if(connected)
{
//打印成功
Serial.println("Connected Succesfully!");
}
else
{
//打印失败
Serial.println("Failed to connect. Make sure remote device is available and in range, then restart app.");
}
}
void loop()
{
//断线自动重连
if(!SerialBT.hasClient())
{
//打印掉线
Serial.println("The slave is