Arduino ESP32 蓝牙(BLE)发送beacon帧

5 篇文章 0 订阅
1 篇文章 0 订阅

简介

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

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
东南亚位于我国倡导推进的“一带一路”海陆交汇地带,作为当今全球发展最为迅速的地区之一,近年来区域内生产总值实现了显著且稳定的增长。根据东盟主要经济体公布的最新数据,印度尼西亚2023年国内生产总值(GDP)增长5.05%;越南2023年经济增长5.05%;马来西亚2023年经济增速为3.7%;泰国2023年经济增长1.9%;新加坡2023年经济增长1.1%;柬埔寨2023年经济增速预计为5.6%。 东盟国家在“一带一路”沿线国家中的总体GDP经济规模、贸易总额与国外直接投资均为最大,因此有着举足轻重的地位和作用。当前,东盟与中国已互相成为双方最大的交易伙伴。中国-东盟贸易总额已从2013年的443亿元增长至 2023年合计超逾6.4万亿元,占中国外贸总值的15.4%。在过去20余年中,东盟国家不断在全球多变的格局里面临挑战并寻求机遇。2023东盟国家主要经济体受到国内消费、国外投资、货币政策、旅游业复苏、和大宗商品出口价企稳等方面的提振,经济显现出稳步增长态势和强韧性的潜能。 本调研报告旨在深度挖掘东南亚市场的增长潜力与发展机会,分析东南亚市场竞争态势、销售模式、客户偏好、整体市场营商环境,为国内企业出海开展业务提供客观参考意见。 本文核心内容: 市场空间:全球行业市场空间、东南亚市场发展空间。 竞争态势:全球份额,东南亚市场企业份额。 销售模式:东南亚市场销售模式、本地代理商 客户情况:东南亚本地客户及偏好分析 营商环境:东南亚营商环境分析 本文纳入的企业包括国外及印尼本土企业,以及相关上下游企业等,部分名单 QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。邮箱:market@qyresearch.com

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值