Arduino ESP32 蓝牙(BLE)发送beacon帧

简介

蓝牙(BLE)发送beacon帧 就是说在没有配对连接的情况下进行广播数据帧,不是蓝牙数据传输。因为蓝牙数据传输需要配对连接蓝牙才可以,且配对连接设备数量有限。但需要大量设备都能收到数据帧时,就只能是通过发送广播包,使其不需要配对连接,就能收到数据帧,一般用作广播报文,日志等。

广播包起始是长度,AD_type,一帧数据。

注意广播包长度这个字节是不算在总长度之内的。

比如广播{0x02,0x01,0x06} - 长度为2 ,AD_type 为0x01,0x06为数据。2长度就是0x01和0x06,而不包括0x02。
AD_Type 类型包括以下几种
在这里插入图片描述

目前试了两种方法都可行,代码如下:

第一种方法
/*
   EddystoneTLM beacon by BeeGee based on https://github.com/pcbreflux/espressif/blob/master/esp32/arduino/sketchbook/ESP32_Eddystone_TLM_deepsleep/ESP32_Eddystone_TLM_deepsleep.ino
   EddystoneTLM frame specification https://github.com/google/eddystone/blob/master/eddystone-tlm/tlm-plain.md
*/

/*
   Create a BLE server that will send periodic Eddystone URL frames.
   The design of creating the BLE server is:
   1. Create a BLE Server
   2. Create advertising data
   3. Start advertising.
   4. wait
   5. Stop advertising.
   6. deep sleep
   
*/
#include "sys/time.h"

#include <Arduino.h>

#include "BLEDevice.h"
#include "BLEUtils.h"
#include "BLEBeacon.h"
#include "BLEAdvertising.h"
#include "BLEEddystoneURL.h"

#include "esp_sleep.h"

#define GPIO_DEEP_SLEEP_DURATION 10     // sleep x seconds and then wake up
RTC_DATA_ATTR static time_t last;    // remember last boot in RTC Memory
RTC_DATA_ATTR static uint32_t bootcount; // remember number of boots in RTC Memory

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/
BLEAdvertising *pAdvertising;
struct timeval nowTimeStruct;

time_t lastTenth;

//#define BEACON_UUID "8ec76ea3-6668-48da-9866-75be8bc86f4d" // UUID 1 128-Bit (may use linux tool uuidgen or random numbers via https://www.uuidgenerator.net/)

void setBeacon1()  //第一帧数据包
{
  static uint8_t Count1;
  uint8_t i = 0;
  char beacon_data1[40];
  uint16_t beconUUID = 0xFFFA;
  char Test_Buff1[27] = {0x00,0x00,0x01,0x12,0x31,0x39,0x35,0x32,0x46,0x30,0x39,0x30,0x43,0x4e,0x32,0x33,0x31,0x32,0x30,0x36,0x30,0x31,0x33,0x34,0x00,0x00,0x00};
  
  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();//创建广告数据对象
  BLEAdvertisementData oScanResponseData = BLEAdvertisementData(); //创建扫描数据对象
  
 oScanResponseData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04   //广播{0x02,0x01,0x06} 设置蓝牙为显性
  oScanResponseData.setCompleteServices(BLEUUID(beconUUID));//广播//beconUUID
  beacon_data1[0] = 0x0D;
  beacon_data1[1] = Count1;
  Count1++;
  if(Count1>252)
  Count1 = 0;
  for(i=2;i<27;i++)
  {
    beacon_data1[i] = Test_Buff1[i];
  }
 

  oScanResponseData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data1, 27));//把第一帧扫描数据加入扫描数据
  oAdvertisementData.setName("TLMBeacon");//把蓝牙名字加入广告数据
  pAdvertising->setAdvertisementData(oAdvertisementData);//把广告数据放入广告帧
  pAdvertising->setScanResponseData(oScanResponseData);//把扫描数据放入扫描帧
}

void setBeacon2() //第二帧数据同上
{
  static uint8_t Count2;
  uint8_t i = 0;
  char beacon_data2[40];
  uint16_t beconUUID = 0xFFFA;
  char Test_Buff2[27] = {0x00,0x00,0x41,0x68,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x08,0x00,0x31,0x00,0x03,0x00,0x00,0x28,0x00,0x00,0x01,0x00,0x00};
  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  
  //oScanResponseData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
  //oScanResponseData.setCompleteServices(BLEUUID(beconUUID));
  beacon_data2[0] = 0x0D;
  beacon_data2[1] = Count2;
  beacon_data2[2] = 0x41;
  beacon_data2[3] = 0x68;
  Count2++;
  if(Count2>252)
  Count2 = 0;
  for(i=4;i<27;i++)
  {
    beacon_data2[i] = Test_Buff2[i];
  }
 

  oScanResponseData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data2, 27));
  oAdvertisementData.setName("TLMBeacon");
  pAdvertising->setAdvertisementData(oAdvertisementData);
  pAdvertising->setScanResponseData(oScanResponseData);
}

void setBeacon3()第三帧数据同上
{
  static uint8_t Count3;
  uint8_t i = 0;
  char beacon_data3[40];
  uint16_t beconUUID = 0xFFFA;
  char Test_Buff3[27] = {0x00,0x00,0x11,0x00,0x06,0x03,0x08,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x22,0x00,0x21,0xD0,0x07,0x6C,0x64,0x00,0x00,0x00,0x00};
  BLEAdvertisementData oAdvertisementData = BLEAdvertisementData();
  BLEAdvertisementData oScanResponseData = BLEAdvertisementData();
  
  //oScanResponseData.setFlags(0x06); // GENERAL_DISC_MODE 0x02 | BR_EDR_NOT_SUPPORTED 0x04
  //oScanResponseData.setCompleteServices(BLEUUID(beconUUID));
  beacon_data3[0] = 0x0D;
  beacon_data3[1] = Count3;
  Count3++;
  if(Count3>252)
  Count3 = 0;
  for(i=2;i<27;i++)
  {
    beacon_data3[i] = Test_Buff3[i];
  }
 

  oScanResponseData.setServiceData(BLEUUID(beconUUID), std::string(beacon_data3, 27));
  oAdvertisementData.setName("TLMBeacon");
  pAdvertising->setAdvertisementData(oAdvertisementData);
  pAdvertising->setScanResponseData(oScanResponseData);
}


void setup()
{

  Serial.begin(115200);
  //gettimeofday(&nowTimeStruct, NULL);

  //Serial.printf("start ESP32 %d\n", bootcount++);

  //Serial.printf("deep sleep (%lds since last reset, %lds since last boot)\n", nowTimeStruct.tv_sec, nowTimeStruct.tv_sec - last);

  //last = nowTimeStruct.tv_sec;
  //lastTenth = nowTimeStruct.tv_sec * 10; // Time since last reset as 0.1 second resolution counter

  // Create the BLE Device
  BLEDevice::init("TLMBeacon");//设置蓝牙名字

  BLEDevice::setPower(ESP_PWR_LVL_N12); //设置蓝牙功率

  pAdvertising = BLEDevice::getAdvertising(); //创建BLEAdvertising 对象,可以用于设置和配置蓝牙广播数据。

  setBeacon1();
  // Start advertising
  pAdvertising->start(); //开始广播
  Serial.println("Advertizing started for 10s ...");
  delay(1000);
  setBeacon2();
  // Start advertising
  pAdvertising->start();//开始广播
  Serial.println("Advertizing started for 10s ...");
  delay(1000);
  setBeacon3();
  // Start advertising
  pAdvertising->start();//开始广播
  Serial.println("Advertizing started for 10s ...");
  delay(1000);
  pAdvertising->stop();//停止广播
  Serial.printf("enter deep sleep for 10s\n");
  //esp_deep_sleep(1000000LL * GPIO_DEEP_SLEEP_DURATION);
  Serial.printf("in deep sleep\n");
}

void loop()
{
  setBeacon1();//第一帧数据
  // Start advertising
  pAdvertising->start();//开始广播
  Serial.println("Advertizing started for 10s ...");
  delay(1000); //留时间给设备收数据
  setBeacon2();//第二帧数据
  // Start advertising
  pAdvertising->start();//开始广播
  Serial.println("Advertizing started for 10s ...");
  delay(1000);//留时间给设备收数据
  setBeacon3();//第三帧数据
  // Start advertising
  pAdvertising->start();//开始广播
  delay(1000);//留时间给设备收数据
}

第二种方法
#include <ArduinoBLE.h>


BLEService myService("fff0"); //创建了一个 UUID 为 "fff0" 的 BLEService 对象
BLEIntCharacteristic myCharacteristic("fff1", BLERead | BLEBroadcast); //创建了一个 UUID 为 "fff1" 的 BLEIntCharacteristic 对象,该特征具有 BLERead 和 BLEBroadcast 权限。

// Advertising parameters should have a global scope. Do NOT define them in 'setup' or in 'loop'
const uint8_t completeRawAdvertisingData1[31] = {0x1E,0x16,0xfa,0xff,0x0D,0x01,0x01,0x12,0x31,0x39,0x35,0x32,0x46,0x30,0x39,0x30,0x43,0x4e,0x32,0x33,0x31,0x32,0x30,0x36,0x30,0x31,0x33,0x34,0x00,0x00,0x00}; 
const uint8_t completeRawAdvertisingData2[31] = {0x1E,0x16,0xfa,0xff,0x0D,0x01,0x41,0x68,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x06,0x08,0x00,0x31,0x00,0x03,0x00,0x00,0x28,0x00,0x00,0x01,0x00,0x00};
const uint8_t completeRawAdvertisingData3[31] = {0x1E,0x16,0xfa,0xff,0x0D,0x01,0x11,0x00,0x06,0x03,0x08,0x00,0x01,0x01,0x00,0x00,0x01,0x01,0x00,0x00,0x22,0x00,0x21,0xD0,0x07,0x6C,0x64,0x00,0x00,0x00,0x00};  

BLEAdvertisingData advData; //创建了一个 BLEAdvertisingData 对象,用于设置蓝牙广播数据。

void setup() {
  Serial.begin(115200);
  while (!Serial);

  if (!BLE.begin()) {
    Serial.println("failed to initialize BLE!");
    while (1);
  }

  //myService.addCharacteristic(myCharacteristic);将特征 myCharacteristic 添加到服务 myService 中
  //BLE.addService(myService);将服务添加到 BLE 对象中,以便在蓝牙设备上广播该服务和特征
  //关闭了可以不广播
  // Build advertising data packet
  
  // If a packet has a raw data parameter, then all the other parameters of the packet will be ignored
  advData.setRawData(completeRawAdvertisingData1,    sizeof(completeRawAdvertisingData1));  //设置完整的原始广播数据到 advData 对象中
  // Copy set parameters in the actual advertising packet
  BLE.setAdvertisingData(advData);//将该数据设置到蓝牙广播中

  // Build scan response data packet
  BLEAdvertisingData scanData;//创建一个新的 BLEAdvertisingData 对象 scanData,用于构建扫描响应数据包。
  scanData.setLocalName("Test advertising raw data");
  // Copy set parameters in the actual scan response packet
  BLE.setScanResponseData(scanData);//将 scanData 设置为蓝牙设备的扫描响应数据,也就是看到的蓝牙名字为Test advertising raw data
  
  BLE.advertise(); //通过 advertise() 方法开始广播这些设置的数据

  Serial.println("advertising ...");
  
}

void loop() 
{
  static uint16_t count,Ble_Count;
  volatile static uint16_t i,j,k;
  //检测服务器端是否有活动的客户端连接
  Ble_Count++;
  if(Ble_Count == 100)  //不用死延时广播多包数据,这样不会影响添加的其他功能
  {
     advData.setRawData(completeRawAdvertisingData1, sizeof(completeRawAdvertisingData1));  //实时更新数据包
  // Copy set parameters in the actual advertising packet
    BLE.setAdvertisingData(advData);
    BLE.advertise(); //实时广播更新的数据包
  }
  else if(Ble_Count == 200)
  {
    advData.setRawData(completeRawAdvertisingData2, sizeof(completeRawAdvertisingData2));  
  // Copy set parameters in the actual advertising packet
    BLE.setAdvertisingData(advData);
    BLE.advertise();

  }
  else if(Ble_Count == 300)
  {
    Ble_Count = 0;
    advData.setRawData(completeRawAdvertisingData3, sizeof(completeRawAdvertisingData3));  
  // Copy set parameters in the actual advertising packet
    BLE.setAdvertisingData(advData);
    BLE.advertise();
  }

 // BLE.poll();//用于轮询 BLE 设备以查看是否有新的事件发生
}

<think>好的,用户想了解ESP32开发BLE,我需要先整理一下相关知识。首先,ESP32是乐鑫的芯片,支持BLE,这点要明确。然后,用户可能需要知道BLE的基本概念,比如低功耗蓝牙和经典蓝牙的区别,以及ESP32在这方面的优势,比如低功耗、双模支持、丰富的外设和成本效益。 接下来,我应该分步骤介绍开发流程。硬件准备部分要提到ESP32的开发板,比如NodeMCU或者DevKit,还有手机或PC作为测试设备。开发环境方面,Arduino和ESP-IDF是常用的,用户可能更熟悉Arduino,所以可能需要详细一点。 然后是关键的BLE基础知识,角色分为外围设备和中心设备,服务、特征值、UUID这些概念要解释清楚,但不用太深入,保持简洁。代码示例部分,用Arduino的例子比较合适,需要展示如何初始化BLE,创建服务和特征值,并处理事件。要注意代码的注释,方便用户理解。 应用场景部分,举几个常见的例子,比如智能家居、健康监测、室内定位,这样用户能明白实际用途。常见问题可能包括连接不稳定、数据长度限制和功耗优化,给出解决方案,比如优化天线设计、分包传输和低功耗模式。 最后推荐学习资源,官方文档、Arduino库、社区论坛,这些对用户进一步学习有帮助。需要确保整个回答结构清晰,逐步引导用户从基础到应用,同时避免使用Markdown,保持口语化但专业。检查一下LaTeX是否符合要求,行内公式用$...$,独立公式用$$...$$,不过这次内容可能不太涉及数学公式,但如果有提到相关参数可能需要用。总之,确保信息准确,逻辑顺畅,用户能一步步理解如何开始ESP32BLE开发。</think>好的,我将分步为您介绍ESP32BLE开发: 一、ESP32硬件基础 1. ESP32是乐鑫推出的Wi-Fi+蓝牙双模芯片 2. 支持BLE 4.2/5.0协议(具体版本取决于模组型号) 3. 典型蓝牙参数: - 发射功率:$+10\text{dBm}$ - 接收灵敏度:$-97\text{dBm}$ - 通信距离:理论最大$100\text{m}$ 二、BLE开发优势 $$ \text{低功耗} < \frac{\text{传统蓝牙功耗}}{10} $$ 1. 支持同时广播和扫描 2. 内置安全加密协处理器 3. 丰富的外设接口(GPIO/I2C/SPI等) 三、开发准备 1. 硬件: - ESP32开发板(推荐型号:ESP32-WROOM-32) - 手机/PC(作为BLE中心设备) 2. 软件环境: ```arduino // Arduino基础配置示例 #include <BLEDevice.h> #include <BLEUtils.h> #include <BLEServer.h> ``` 四、核心开发步骤 1. 角色定义: - 外围设备(Peripheral):通常为ESP32 - 中心设备(Central):手机/PC等 2. 服务架构: $$ \text{服务(Service)} \supset \text{特征值(Characteristic)} \supset \text{描述符(Descriptor)} $$ 3. 典型代码结构: ```arduino void setup() { 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->setValue("Hello BLE"); pService->start(); BLEAdvertising *pAdvertising = pServer->getAdvertising(); pAdvertising->start(); } ``` 五、关键API说明 1. `BLECharacteristic::PROPERTY_*` 权限设置 2. 事件回调处理: ```arduino pCharacteristic->setCallbacks(new MyCallbacks()); class MyCallbacks : public BLECharacteristicCallbacks { void onWrite(BLECharacteristic *pCharacteristic) { std::string value = pCharacteristic->getValue(); } } ``` 六、典型应用场景 1. 智能家居(温湿度传输) 2. 健康监测(心率/血氧数据) 3. 室内定位(Beacon广播) 七、常见问题处理 1. 连接不稳定: - 检查天线匹配电路 - 调整发射功率`BLEDevice::setPower()` 2. 数据长度限制: - 单包最大$20\text{bytes}$ - 需要分包传输时使用`BLECharacteristic::PROPERTY_NOTIFY` 八、学习资源推荐 1. 乐鑫官方文档《ESP32 BLE开发指南》 2. ArduinoBLE库示例代码 3. 开源项目:ESP32-BLE-Keyboard 4. 调试工具:nRF Connect(手机端) 实际开发中建议: 1. 先使用现成的BLE例程测试通信 2. 逐步添加自定义服务和特征值 3. 最后实现完整的数据交互逻辑
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

吾与春风皆过客

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值