Bluetooth Serial Library(蓝牙串口库)
A simple Serial compatible library using ESP32 classical bluetooth (SPP)
一个用到了ESP32的蓝牙()库的简单串口兼容库
如何使用它?
-
Download one bluetooth terminal app in your smartphone
-
在你的手机上下载蓝牙终端app
For Android
For IOS -
Flash an example sketch to your ESP32
-
给你的esp32烧写一个例程
-
Scan and pair the device in your smartphone
-
用你的手机扫描并配对蓝牙
-
Open the bluetooth terminal app
-
打开app
函数介绍:
Serial.available()
:返回串口缓冲区中当前剩余的字符个数。
Serial.print()
:发送的是字符,
Serial.write()
:发送的字节.
代码:
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial
//这个例程为串口和传统蓝牙架设了一座桥梁
//同时也演示了SerialBT和普通的Serial具有一样的功能
#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
BluetoothSerial SerialBT;
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32test"); //蓝牙模块名称
Serial.println("蓝牙已启动,你可以配对了!");
}
void loop() {
if (Serial.available()) {
SerialBT.write(Serial.read());
Serial.println("由SerialBT打印");
}
if (SerialBT.available()) {
Serial.write(SerialBT.read());
Serial.println("由Serial打印");
}
delay(20);
}
测试:
打开app,点击左侧菜单
点击devices,找到名称点击配对
发送信息
结果: