ESPHome  2024.9.0
ble_advertising.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include <array>
4 #include <functional>
5 #include <vector>
6 
7 #ifdef USE_ESP32
8 
9 #include <esp_bt.h>
10 #include <esp_gap_ble_api.h>
11 #include <esp_gatts_api.h>
12 
13 namespace esphome {
14 namespace esp32_ble {
15 
16 using raw_adv_data_t = struct {
17  uint8_t *data;
18  size_t length;
19  esp_power_level_t power_level;
20 };
21 
22 class ESPBTUUID;
23 
25  public:
26  BLEAdvertising(uint32_t advertising_cycle_time);
27 
28  void loop();
29 
30  void add_service_uuid(ESPBTUUID uuid);
31  void remove_service_uuid(ESPBTUUID uuid);
32  void set_scan_response(bool scan_response) { this->scan_response_ = scan_response; }
33  void set_min_preferred_interval(uint16_t interval) { this->advertising_data_.min_interval = interval; }
34  void set_manufacturer_data(const std::vector<uint8_t> &data);
35  void set_service_data(const std::vector<uint8_t> &data);
36  void register_raw_advertisement_callback(std::function<void(bool)> &&callback);
37 
38  void start();
39  void stop();
40 
41  protected:
42  esp_err_t services_advertisement_();
43 
45  esp_ble_adv_data_t advertising_data_;
46  esp_ble_adv_data_t scan_response_data_;
47  esp_ble_adv_params_t advertising_params_;
48  std::vector<ESPBTUUID> advertising_uuids_;
49 
50  std::vector<std::function<void(bool)>> raw_advertisements_callbacks_;
51 
52  const uint32_t advertising_cycle_time_;
54  int8_t current_adv_index_{-1}; // -1 means standard scan response
55 };
56 
57 } // namespace esp32_ble
58 } // namespace esphome
59 
60 #endif
BLEAdvertising(uint32_t advertising_cycle_time)
void set_min_preferred_interval(uint16_t interval)
struct { uint8_t *data raw_adv_data_t
void set_service_data(const std::vector< uint8_t > &data)
std::vector< std::function< void(bool)> > raw_advertisements_callbacks_
esp_ble_adv_params_t advertising_params_
void set_manufacturer_data(const std::vector< uint8_t > &data)
esp_power_level_t power_level
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void remove_service_uuid(ESPBTUUID uuid)
esp_ble_adv_data_t scan_response_data_
Implementation of SPI Controller mode.
Definition: a01nyub.cpp:7
std::vector< ESPBTUUID > advertising_uuids_
void set_scan_response(bool scan_response)