问小白 wenxiaobai
资讯
历史
科技
环境与自然
成长
游戏
财经
文学与艺术
美食
健康
家居
文化
情感
汽车
三农
军事
旅行
运动
教育
生活
星座命理

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”输出

© 2023 北京元石科技有限公司 ◎ 京公网安备 11010802042949号