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”输出
热门推荐
香港银行同业人民币拆借利率上行 业内:与海外资本对人民币的需求升温有关
足球战术板 | 442经典阵型,为什么成了摆大巴的铁桶阵?
韩愈《早春呈水部张十八员外》:写尽盛春,人生最美的风景怎么能错过欣赏
胸痛和背痛是怎么回事
江苏送变电公司:深化装备应用 推进电缆施工
电脑数据备份的方式有哪几种?六个具体的操作方法详解
搞笑至极!CBA现场球迷加油口号脑洞大开,辽宁和广东队欲哭无泪
养猪场污水处理智能解决方案:从收集到再利用的全流程管理
数学:面积的计算及应用
富含叶黄素的食物有哪些
水产养殖药物:种类、选择与使用指南
抑郁症患者的情绪波动:深入理解与应对之道
拔牙种牙前未签知情同意书!牙科诊所因此被罚1.2万!
教育部:坚决落实辅导员编制
低碳理念在园林景观设计中的运用
出生人口下降背景下 学前教育供需失衡加剧
如何恢复体力和精力?医生给出专业建议
沪郊风物推荐官:南瓜届的“白富美”——惠和“白贝贝”惊艳上市
参保缴费信息怎么查?这些渠道很方便
沿着黄河游兰州
2025年消费者市场分析:消费趋势与行为洞察
两集飙升9.3,它一出场,吊打所有反转神剧
2025年中国铸造产业现状及发展趋势研究报告
为什么要给孩子接种天花疫苗?
机床租赁合同样本:关于租赁双方权利义务及责任分担的条款
昆明特色美食排行榜前十名,哪道佳肴能征服你的味蕾?
昆明特色美食排行榜前十名,哪道佳肴能征服你的味蕾?
和田玉子冈牌价格详解:含义与市场行情分析
决定离职时,应该如何向领导和同事告知比较得体
如何使用AI提问提示词(Prompt):让你的提问回答更有效