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”输出
热门推荐
考研初试将近,这份清单你准备好了吗?
盐酸帕罗西汀片不同人群使用的疗程一样吗
智能手表腕带:隐藏的“永久化学物质”危机
《永劫无间手游》公测FAQ:性能优化篇
西方刀叉拿法与使用方法介绍
如何正确测量:自己的血压?
如何吃好每顿饭,阿尔茨海默症患者的饮食与营养照护
新手制作小程序如何挑选靠谱模板
大蒜真的能降血压吗?医生的专业解答来了
英国红茶可以加什么功效
2025上海春季赏花全攻略:十大花卉打卡点与赏花活动推荐
茵陈蒿:从化学成分到药用价值的全面解析
日本人忌讳什么颜色?
台球击球准确性提升指南:从技巧到心态的全方位训练
教师资格证报考条件详解与实例分析
教师资格证一年考几次 考试科目有哪些
百万富翁不是梦!梦想成真又该如何规划?
腮腺炎是由什么引起的?有哪些症状
腮腺炎是什么?症状、原因、治疗与预防全解析
定冠词the的用法口诀及详解
父母遗嘱房产如何过户?遗嘱冲突怎么办?
子女分房产纠纷怎么处理
日本人最喜欢的颜色是什么?
紫菜虾皮汤:两种微小食材的营养碰撞
买虾皮,记得分清“白色”和“浅黄色”,二者区别很大,建议弄懂
砧板是什么意思 砧板正确使用方式是什么
豆米火锅:美食与情感交融的聚会选择
欧米伽-3补充剂:为儿童带来更好的心理健康和行为表现
对于特定的求职意向,简历和面试中应突出哪些要点
如何让简历邮件主题在众多邮件中脱颖而出