ESPHome  2024.11.1
esp32_ble_beacon.cpp
Go to the documentation of this file.
1 #include "esp32_ble_beacon.h"
2 #include "esphome/core/log.h"
3 
4 #ifdef USE_ESP32
5 
6 #include <esp_bt.h>
7 #include <esp_bt_main.h>
8 #include <esp_gap_ble_api.h>
9 #include <freertos/FreeRTOS.h>
10 #include <freertos/task.h>
11 #include <nvs_flash.h>
12 #include <cstring>
13 
14 #include "esphome/core/hal.h"
15 #include "esphome/core/helpers.h"
16 
17 #ifdef USE_ARDUINO
18 #include <esp32-hal-bt.h>
19 #endif
20 
21 namespace esphome {
22 namespace esp32_ble_beacon {
23 
24 static const char *const TAG = "esp32_ble_beacon";
25 
26 static const esp_ble_ibeacon_head_t IBEACON_COMMON_HEAD = {
27  .flags = {0x02, 0x01, 0x06}, .length = 0x1A, .type = 0xFF, .company_id = {0x4C, 0x00}, .beacon_type = {0x02, 0x15}};
28 
30  ESP_LOGCONFIG(TAG, "ESP32 BLE Beacon:");
31  char uuid[37];
32  char *bpos = uuid;
33  for (int8_t ii = 0; ii < 16; ++ii) {
34  bpos += sprintf(bpos, "%02X", this->uuid_[ii]);
35  if (ii == 3 || ii == 5 || ii == 7 || ii == 9) {
36  bpos += sprintf(bpos, "-");
37  }
38  }
39  uuid[36] = '\0';
40  ESP_LOGCONFIG(TAG,
41  " UUID: %s, Major: %u, Minor: %u, Min Interval: %ums, Max Interval: %ums, Measured Power: %d"
42  ", TX Power: %ddBm",
43  uuid, this->major_, this->minor_, this->min_interval_, this->max_interval_, this->measured_power_,
44  (this->tx_power_ * 3) - 12);
45 }
46 
48 
50  this->ble_adv_params_ = {
51  .adv_int_min = static_cast<uint16_t>(this->min_interval_ / 0.625f),
52  .adv_int_max = static_cast<uint16_t>(this->max_interval_ / 0.625f),
53  .adv_type = ADV_TYPE_NONCONN_IND,
54  .own_addr_type = BLE_ADDR_TYPE_PUBLIC,
55  .peer_addr = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
56  .peer_addr_type = BLE_ADDR_TYPE_PUBLIC,
57  .channel_map = ADV_CHNL_ALL,
58  .adv_filter_policy = ADV_FILTER_ALLOW_SCAN_ANY_CON_ANY,
59  };
60 
62  this->advertising_ = advertise;
63  if (advertise) {
64  this->on_advertise_();
65  }
66  });
67 }
68 
70  esp_ble_ibeacon_t ibeacon_adv_data;
71  memcpy(&ibeacon_adv_data.ibeacon_head, &IBEACON_COMMON_HEAD, sizeof(esp_ble_ibeacon_head_t));
72  memcpy(&ibeacon_adv_data.ibeacon_vendor.proximity_uuid, this->uuid_.data(),
73  sizeof(ibeacon_adv_data.ibeacon_vendor.proximity_uuid));
74  ibeacon_adv_data.ibeacon_vendor.minor = byteswap(this->minor_);
75  ibeacon_adv_data.ibeacon_vendor.major = byteswap(this->major_);
76  ibeacon_adv_data.ibeacon_vendor.measured_power = static_cast<uint8_t>(this->measured_power_);
77 
78  ESP_LOGD(TAG, "Setting BLE TX power");
79  esp_err_t err = esp_ble_tx_power_set(ESP_BLE_PWR_TYPE_ADV, this->tx_power_);
80  if (err != ESP_OK) {
81  ESP_LOGW(TAG, "esp_ble_tx_power_set failed: %s", esp_err_to_name(err));
82  }
83  err = esp_ble_gap_config_adv_data_raw((uint8_t *) &ibeacon_adv_data, sizeof(ibeacon_adv_data));
84  if (err != ESP_OK) {
85  ESP_LOGE(TAG, "esp_ble_gap_config_adv_data_raw failed: %s", esp_err_to_name(err));
86  return;
87  }
88 }
89 
90 void ESP32BLEBeacon::gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
91  if (!this->advertising_)
92  return;
93 
94  esp_err_t err;
95  switch (event) {
96  case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: {
97  err = esp_ble_gap_start_advertising(&this->ble_adv_params_);
98  if (err != ESP_OK) {
99  ESP_LOGE(TAG, "esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
100  }
101  break;
102  }
103  case ESP_GAP_BLE_ADV_START_COMPLETE_EVT: {
104  err = param->adv_start_cmpl.status;
105  if (err != ESP_BT_STATUS_SUCCESS) {
106  ESP_LOGE(TAG, "BLE adv start failed: %s", esp_err_to_name(err));
107  }
108  break;
109  }
110  case ESP_GAP_BLE_ADV_STOP_COMPLETE_EVT: {
111  err = param->adv_stop_cmpl.status;
112  if (err != ESP_BT_STATUS_SUCCESS) {
113  ESP_LOGE(TAG, "BLE adv stop failed: %s", esp_err_to_name(err));
114  } else {
115  ESP_LOGD(TAG, "BLE stopped advertising successfully");
116  }
117  // this->advertising_ = false;
118  break;
119  }
120  default:
121  break;
122  }
123 }
124 
125 } // namespace esp32_ble_beacon
126 } // namespace esphome
127 
128 #endif
ESP32BLE * global_ble
Definition: ble.cpp:411
struct { uint8_t flags[3] esp_ble_ibeacon_head_t
const float AFTER_BLUETOOTH
Definition: component.cpp:22
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
Definition: ble.cpp:81
struct { esp_ble_ibeacon_head_t ibeacon_head esp_ble_ibeacon_t
constexpr14 T byteswap(T n)
Definition: helpers.h:130
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7