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”输出
热门推荐
Nintendo Switch错误代码2124-8007的解决方法
从抗日战争到内战结束,国民党军一共接收了多少美国援助?
住房网签合同:了解合同条款确保权益
2n赔偿怎么争取:从证据收集到维权流程
视频聊天会泄露出去吗?揭秘潜在风险与防范措施
房子有煞气是什么意思?如何化解家居风水隐患
了悟道学~自证预言
人要学会拒绝 教你怎么理直气壮地说"不"
支付系统中的幂等性:原理与实现
再战萨巴伦卡遗憾失利,郑钦文:已找到调整方向
医生解答:过夜鸡蛋到底能不能吃?
江苏吴中被证监会立案调查后 子公司又因骗税被起诉
书桌高度多少合适?买书桌注意哪些问题?
王者荣耀刘禅技能详解与玩法攻略
贺知章:人生几度春风
快速提升视频剪辑效率的五招技巧
Kindle邮箱后缀问题解决指南:从".cn"到".com"的正确传输方法
上海专家在顶刊发文,为益生菌减肥降糖添新证
150多年前,美国花720万美元买下阿拉斯加,如今发展怎样了?
拆迁补偿相关问题解答:从资料收集到权益保障
公司拖欠提成怎么解决
比熊犬的饲养成本大概是多少?如何合理预算?
王者荣耀中的节奏点,掌握节奏,胜券在握
林冲研究报告
澳洲技术移民491签证的申请要求有哪些?如何转永居?
越南榴莲:突击检查!多家果商存在违规行为
40岁肌肤保养秘籍:重塑肌肤年轻活力
冰箱排水孔的水排到哪里去了?是否需要定期清理?
秦始皇六次声势浩大的巡游,背后隐藏着什么?
秦始皇巡游的目的只是为了找长生不老药吗?