简介
蓝牙(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 设备以查看是否有新的事件发生
}









![[OpenGL] opengl切线空间](https://img-blog.csdnimg.cn/direct/d9f9b4cdabc44eb98c3f14a8a93b68e5.png)









