ESP32C3SuperMini蓝牙功能使用指南
创作时间:
作者:
@小白创作中心
ESP32C3SuperMini蓝牙功能使用指南
引用
1
来源
1.
https://www.nologo.tech/product/esp32/esp32c3/esp32c3supermini/esp32C3SuperMiniBluetooth.html
ESP32C3SuperMini是一款功能强大的开发板,支持蓝牙功能,可以用于各种物联网应用。本文将详细介绍如何使用ESP32C3SuperMini进行蓝牙设备扫描和作为蓝牙服务器的基本操作。
硬件连接
首先,需要将ESP32C3SuperMini通过USB Type-C数据线连接到计算机。
扫描蓝牙
我们将使用ESP32C3SueprMini扫描其周围可用的蓝牙设备。
- 步骤 1.将以下代码复制并粘贴到Arduino IDE中
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEAdvertisedDevice.h>
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());
}
};
void setup() {
Serial.begin(115200);
Serial.println("Scanning...");
BLEDevice::init("");
pBLEScan = BLEDevice::getScan(); //create new scan
pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
pBLEScan->setInterval(100);
pBLEScan->setWindow(99); // less or equal setInterval value
}
void loop() {
// put your main code here, to run repeatedly:
BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
Serial.print("Devices found: ");
Serial.println(foundDevices.getCount());
Serial.println("Scan done!");
pBLEScan->clearResults(); // delete results fromBLEScan buffer to release memory
delay(2000);
}
- 步骤 2.上传代码并打开串行监视器以开始扫描蓝牙设备
作为蓝牙服务器
在此示例中,我们将使用ESP32C3SuperMini作为蓝牙服务器。在这里,我们将使用智能手机搜索ESP32C3SuperMini板并发送字符串以显示在串行监视器上。
- 步骤 1.将以下代码复制并粘贴到Arduino IDE中
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string value = pCharacteristic->getValue();
if (value.length() > 0) {
Serial.println("*********");
Serial.print("New value: ");
for (int i = 0; i < value.length(); i++)
Serial.print(value[i]);
Serial.println();
Serial.println("*********");
}
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("MyESP32");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setCallbacks(new MyCallbacks());
pCharacteristic->setValue("Hello World");
pService->start();
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
步骤 2.上传代码并打开串行监视器
步骤 3.在您的智能手机上下载并安装LightBlue应用程序
LightBlue App(Android)
LightBlue App(IOS)
步骤4.打开手机蓝牙,将手机靠近ESP32C3SuperMini,扫描设备并连接MyESP32设备
步骤 5.打开LightBlue应用程序并单击Bonded选项卡
步骤 6.单击MyESP32旁边的CONNECT
步骤 7.单击最底部显示“可读”、“可写”的部分
步骤 8.在数据格式下拉菜单下,选择UTF-8字符串
步骤 9.在“WRITTEN VALUES”下键入“Hello”,然后单击“WRITE”
您将在Arduino IDE的串行监视器上看到文本字符串“Hello”输出
热门推荐
什么是企业价值评估
哪些国家入境要按指纹
什么是多头授信?
信息学奥赛初赛天天练-06-组合数学抽屉原理习题大全
喜欢喝咖啡的注意!医生紧急提醒
人工智能时代,风水命理何去何从?
C++中的 PIMPL:强大的设计模式
北交所上市和主板上市区别
漫长的探索:人类对宇宙的认知历程
圆周率:从古巴比伦到量子力学,一个永恒的数字之谜
机场贵宾室通常包含哪些服务?
深度解析大宗商品价格波动:多重因素影响下的市场博弈
广东省立中山图书馆推出智能联合新书采选系统,助力大湾区文化资源共享
如何管控软件开发进度与开发质量
缓解骨关节炎疼痛,该如何正确选用药物?
关节红肿痛该不该热敷?
美国革命如何触发了法国大革命?三个关键因素解析
全画幅相机 vs 半画幅相机:为什么专业人士更青睐全画幅?
怎么选择拍视频的镜头 选择拍摄视频的镜头时需要考虑哪些因素
脱硫废水零排放处理技术详解
医疗器械商标注册流程中有哪些关键步骤和注意事项?
一枝黄花的功效与作用及禁忌 中药一枝黄花的副作用
多路阀怎么安装调试?有哪些注意事项?
合同期满后如何处理劳动关系及权益保障
读《散曲名篇》|【正宫】叨叨令
胆红素偏高怎么降
空心菜的5大功效:营养、保存、简单食谱一次看!
惊艳全场!广东小学生上演高难度“抛枪”!
如何选择合适的投资方式?怎样根据自身情况进行投资规划?
如何借助白糖期权举例加深理解?这些例子能提供哪些启示?