使用Arduino开发ESP32(十六):ESP32 BLE Server

ESP32 BLE for Arduino

The Arduino IDE provides an excellent library package manager where versions of libraries can be downloaded and installed. This Github project provides the repository for the ESP32 BLE support for Arduino.git

Arduino IDE提供了一个优秀的库包管理器,能够在其中下载和安装不一样版本的库。这个Github项目为Arduino提供了ESP32 BLE支持的存储库。github

仍在更新的项目资源下载:web

https://github.com/nkolban/esp32-snippetsjson

问题反馈:服务器

https://github.com/nkolban/esp32-snippets/issues网络

使用文档可在这里找到:ide

https://github.com/nkolban/esp32-snippets/tree/master/Documentationsvg


基础知识:

什么是BLE?
低功耗蓝牙(Low Energy; LE),又视为Bluetooth Smart或蓝牙核心规格4.0版本。其特色具有节能、便于采用,是蓝牙技术专为物联网(Internet of Things; IOT)开发的技术版本。函数

因此它最主要的特色是低功耗,普及率高。如今所说的蓝牙设备,大部分都是在说4.0设备,ble也特指4.0设备。 在4.0以前重要的版本有2.1版本-基本速率/加强数据率(BR/EDR)和3.0 高速蓝牙版本,这些统称为经典蓝牙,oop

4.0还有4.1101和4.2的小版本,其中4.2版本对传输速率作了进一步他提高,提升了2.5倍,

蓝牙开发必须知道的概念:

central和peripheral

蓝牙应用开发中,存在两种角色,分别是central和peripheral(pə’rɪfərəl) ,中文就是中心和外设。好比手机去链接智能设备,那手机就是central,智能设备就是peripheral。大多时候都是central去链接peripheral的场景,因此咱们就来讲他的流程

广播和链接

peripheral会发出广播(advertisement:ædvɚ’taɪzmənt),central扫描到广播后,能够对设备进行链接,发出connect请求,peripheral接收到请求后,赞成链接后,central和peripheral就创建了链接。

蓝牙的数据交互
write,read,notify,indecate, response or not … 读写你们都是容易理解的,indecate和notify对应的是长链接,创建indecate后,peripheral能够随时往central发送数据。

indecate和notify的区别就在于,indecate是必定会收到数据,notify有可能会丢失数据(不会有central收到数据的回应),write也分为response和noresponse,若是是response,那么write成功回收到peripheral的确认消息,可是会下降写入的速率。

对于一个charateristic,他的读写订阅的权限是peripheral决定的,熟悉能够被同时设置,通常会根据外设的功能来决定。

蓝牙应用的通常流程
1.创建中心角色
2.扫描外设(discover)
3.链接外设 (connect)
4.扫描外设中的服务和特征(discover)
(1)4.1 获取外设的services
(2)4.2 获取外设的Characteristics,获取Characteristics的值,获取Characteristics的Descriptor值
5.与外设作数据交互(explore and interact)
6.订阅Characteristic的通知
7.断开链接(disconnect)

引述自关于蓝牙开发,你必须知道的知识


Central(中心设备);
Peripheral(外围设备);
advertising(广告);
Services(服务);
Characteristic(特征)
CoreBluetooth介绍

在CoreBluetooth中有两个主要的部分,Central和Peripheral,CBPeripheralManager 做为周边设备。CBCentralManager做为中心设备。全部可用的iOS设备能够做为周边(Peripheral)也能够做为中央(Central),但不能够同时既是周边也是中央。

周边设备(Peripheral)设备是广播设备的数据,中央设备(Central)是管理而且使用这些数据的设备。
也就是说周边(Peripheral)向周围发送广播,告诉周围的中央设备(Central)它(周边(Peripheral)这里有数据,而且说明了能提供的服务和特征值(链接以后才能获取),
其实蓝牙传值至关于网络接口,硬件的service的UUID加上characteristic的UUID,
打一个比喻:service的UUID至关于主地址,characteristic的UUID至关于短连接,短连接必须是主地址的分支,拼在一块儿的是接口,你和硬件设定的蓝牙传输格式相似于json,双方可识别的数据,由于蓝牙只能支持16进制,并且每次传输只能20个字节,因此要把信息流转成双方可识别的16进制

引述自[iOS蓝牙]蓝牙链接并经过特征CBCharacteristic读写数据

相关函数定义:

static void        init(std::string deviceName);

初始化BLE环境.

static BLEServer*  createServer();

建立一个BLE Server

createService(SERVICE_UUID)

建立一个Service

BLECharacteristic* createCharacteristic(const char* uuid, uint32_t properties);
BLECharacteristic* createCharacteristic(BLEUUID uuid, uint32_t properties);

建立一个Characteristic
**

void setValue(任意类型数据);
void setValue(uint8_t* data, size_t size);

为Characteristic赋一个值

补充:BLE特征(characteristic)是一个管理值的标识值容器。它由BLE服务器公开,能够由BLE客户端读写。(真绕口,英语原文如此)

实例:

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>

// 访问下述网站生成本身的UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID "b3f48ce7-6322-49e4-ab01-e672744db6fb"
#define CHARACTERISTIC_UUID "ef6a6b6a-5a46-474c-8b2a-93f05a2179f3"

void setup() {
  Serial.begin(115200);
  Serial.println("BLE开启!");

  BLEDevice::init("ESP32-Test");    //初始化
  BLEServer *pServer = BLEDevice::createServer();   //建立server
  BLEService *pService = pServer->createService(SERVICE_UUID);  //建立Service
  BLECharacteristic *pCharacteristic = pService->createCharacteristic( CHARACTERISTIC_UUID,BLECharacteristic::PROPERTY_READ |BLECharacteristic::PROPERTY_WRITE);

  pCharacteristic->setValue("Hello World From ESP32");
  pService->start();   //服务开启
  BLEAdvertising *pAdvertising = BLEDevice::getAdvertising();
  // BLEAdvertising *pAdvertising = pServer->getAdvertising(); //兼容写法 this still is working for backward compatibility
  pAdvertising->addServiceUUID(SERVICE_UUID);   //设置服务UUID
  pAdvertising->setScanResponse(true);   //开启扫描响应(?)
  pAdvertising->setMinPreferred(0x06);  // 解决ipone手机链接问题,functions that help with iPhone connections issue
  pAdvertising->setMinPreferred(0x12);
  BLEDevice::startAdvertising();   //开始广播
  Serial.println("Characteristic(特征)已定义,你能够在手机上查看");
}

void loop() {
  delay(2000);
}

结果:
在这里插入图片描述

暂未找到查看characteristic的路子,待以后补充