Bluetooth Low Energy,简称BLE,是蓝牙的一种节能变体。BLE 的主要应用是少量数据的短距离传输(低带宽)。与始终开启的蓝牙不同,BLE 会一直保持睡眠模式,除非发起连接。这使得它消耗非常低的功率。BLE 的功耗比蓝牙低大约 100 倍(取决于使用情况)。
此外,BLE 不仅支持点对点通信,还支持广播模式和网状网络。由于其特性,BLE 适用于需要在纽扣电池上定期交换少量数据的应用程序。例如,BLE 在医疗保健、健身、跟踪、信标、安全和家庭自动化行业中有很大的用途。
对于低功耗蓝牙,有两种类型的设备:服务器和客户端。ESP32 既可以作为客户端也可以作为服务器。
服务器广播它的存在,因此可以被其他设备发现,并包含客户端可以读取的数据。客户端扫描附近的设备,当它找到它正在寻找的服务器时,它会建立连接并监听传入的数据。这称为点对点通信。
BLE 还支持广播模式和网状网络:
ESP32 可以充当 BLE 服务器或 BLE 客户端。Arduino IDE 的 ESP32 BLE 库中有几个 ESP32 的 BLE 示例 。当您在 Arduino IDE 上安装 ESP32 时,该库默认安装。
在您的 Arduino IDE 中,您可以转到 文件>示例> ESP32 BLE Arduino并浏览 BLE 库附带的示例。
注意:要查看 ESP32 示例,您必须在Tools > Board上选择 ESP32 board 。
为了简要介绍在 Arduino IDE 上使用 BLE 的 ESP32,我们将创建一个 ESP32 BLE 服务器,然后创建一个 ESP32 BLE 扫描器来查找该服务器。我们将使用和解释 BLE 库附带的示例。
要完成此示例,您需要两块 ESP32 开发板。我们将使用 ESP32 DOIT DEVKIT V1 开发板。
要创建 ESP32 BLE 服务器,请打开您的 Arduino IDE 并转到 文件>示例> ESP32 BLE Arduino并选择BLE_server示例。应加载以下代码:
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleServer.cpp
Ported to Arduino ESP32 by Evandro Copercini
updates by chegewara
*/
#include
#include
#include
// 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"
void setup() {
Serial.begin(115200);
Serial.println("Starting BLE work!");
BLEDevice::init("Long name works now");
BLEServer *pServer = BLEDevice::createServer();
BLEService *pService = pServer->createService(SERVICE_UUID);
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
pCharacteristic->setValue("Hello World says Neil");
pService->start();
// BLEAdvertising *pAdvertising = pServer->getAdvertising(); // this still is working for backward compatibility
BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
pAdvertising->addServiceUUID(SERVICE_UUID);
pAdvertising->setScanResponse(true);
pAdvertising->setMinPreferred(0x06); // functions that help with iPhone connections issue
pAdvertising->setMinPreferred(0x12);
BLEDevice::startAdvertising();
Serial.println("Characteristic defined! Now you can read it in your phone!");
}
void loop() {
// put your main code here, to run repeatedly:
delay(2000);
}
查看原始代码
要创建 BLE 服务器,代码应遵循以下步骤:
首先导入 BLE 功能所需的库。
#include
#include
#include
然后,需要为 Service 和 Characteristic 定义一个 UUID。
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
可以保留默认 UUID,也可以转到 uuidgenerator.net 创建随机 UUID。
在里面设置(), 它以 115200 的波特率开始串行通信。
Serial.begin(115200);
然后,创建一个名为“ MyESP32 ”的 BLE 设备。
// Create the BLE Device
BLEDevice::init("MyESP32");
在下一行中,将 BLE 设备设置为服务器。
BLEServer *pServer = BLEDevice::createServer();
之后,使用之前定义的 UUID 为 BLE 服务器创建服务。
BLEService *pService = pServer->createService(SERVICE_UUID);
然后,设置该服务的特征。还使用了之前定义的 UUID,并且需要将特性的属性作为参数传递。在这种情况下,它是:READ 和 WRITE。
BLECharacteristic *pCharacteristic = pService->createCharacteristic(
CHARACTERISTIC_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_WRITE
);
创建特征后,可以使用设定值()方法。
pCharacteristic->setValue("Hello World says Neil");
在这种情况下,将值设置为文本“Hello World says Neil”。
最后,可以启动服务和广告,以便其他 BLE 设备可以扫描并找到此 BLE 设备。
BLEAdvertising *pAdvertising = pServer->getAdvertising();
pAdvertising->start();
这只是一个关于如何创建 BLE 服务器的简单示例。。
创建 ESP32 BLE 扫描器很简单。用另一个ESP32,在 Arduino IDE 中,转到 文件 > 示例> ESP32 BLE Arduino并选择BLE_scan示例。加载以下代码。
/*
Based on Neil Kolban example for IDF: https://github.com/nkolban/esp32-snippets/blob/master/cpp_utils/tests/BLE%20Tests/SampleScan.cpp
Ported to Arduino ESP32 by Evandro Copercini
*/
#include
#include
#include
#include
int scanTime = 5; //In seconds
BLEScan* pBLEScan;
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
void onResult(BLEAdvertisedDevice advertisedDevice) {
Serial.printf("Advertised Device: %s
", 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);
}
此代码将 ESP32 初始化为 BLE 设备并扫描附近的设备。将此代码上传到 ESP32。。
上传代码后,打开两个 ESP32 开发板:
转到运行“BLE_scan”示例的 ESP32 的串行监视器,按下 ESP32)启用按钮以重新启动并在扫描时等待几秒钟。
扫描器发现了设备: MyESP32
页面更新:2024-03-10
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2020-2024 All Rights Reserved. Powered By 71396.com 闽ICP备11008920号-4
闽公网安备35020302034903号