ESPHome  2024.10.2
esp32_ble_beacon.h
Go to the documentation of this file.
1 #pragma once
2 
5 
6 #ifdef USE_ESP32
7 
8 #include <esp_bt.h>
9 #include <esp_gap_ble_api.h>
10 
11 namespace esphome {
12 namespace esp32_ble_beacon {
13 
14 using esp_ble_ibeacon_head_t = struct {
15  uint8_t flags[3];
16  uint8_t length;
17  uint8_t type;
18  uint8_t company_id[2];
19  uint8_t beacon_type[2];
20 } __attribute__((packed));
21 
22 using esp_ble_ibeacon_vendor_t = struct {
23  uint8_t proximity_uuid[16];
24  uint16_t major;
25  uint16_t minor;
26  uint8_t measured_power;
27 } __attribute__((packed));
28 
29 using esp_ble_ibeacon_t = struct {
30  esp_ble_ibeacon_head_t ibeacon_head;
32 } __attribute__((packed));
33 
34 using namespace esp32_ble;
35 
36 class ESP32BLEBeacon : public Component, public GAPEventHandler, public Parented<ESP32BLE> {
37  public:
38  explicit ESP32BLEBeacon(const std::array<uint8_t, 16> &uuid) : uuid_(uuid) {}
39 
40  void setup() override;
41  void dump_config() override;
42  float get_setup_priority() const override;
43 
44  void set_major(uint16_t major) { this->major_ = major; }
45  void set_minor(uint16_t minor) { this->minor_ = minor; }
46  void set_min_interval(uint16_t val) { this->min_interval_ = val; }
47  void set_max_interval(uint16_t val) { this->max_interval_ = val; }
48  void set_measured_power(int8_t val) { this->measured_power_ = val; }
49  void set_tx_power(esp_power_level_t val) { this->tx_power_ = val; }
50  void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override;
51 
52  protected:
53  void on_advertise_();
54 
55  std::array<uint8_t, 16> uuid_;
56  uint16_t major_{};
57  uint16_t minor_{};
58  uint16_t min_interval_{};
59  uint16_t max_interval_{};
60  int8_t measured_power_{};
61  esp_power_level_t tx_power_{};
62  esp_ble_adv_params_t ble_adv_params_;
63  bool advertising_{false};
64 };
65 
66 } // namespace esp32_ble_beacon
67 } // namespace esphome
68 
69 #endif
void setup()
struct { uint8_t proximity_uuid[16] esp_ble_ibeacon_vendor_t
mopeka_std_values val[4]
struct { uint8_t flags[3] esp_ble_ibeacon_head_t
esp_ble_ibeacon_vendor_t ibeacon_vendor
ESP32BLEBeacon(const std::array< uint8_t, 16 > &uuid)
const uint32_t flags
Definition: stm32flash.h:85
struct { esp_ble_ibeacon_head_t ibeacon_head esp_ble_ibeacon_t
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
void set_tx_power(esp_power_level_t val)
Helper class to easily give an object a parent of type T.
Definition: helpers.h:521