ESPHome 2026.11.0b6
Loading...
Searching...
No Matches
ble_advertising.h
Go to the documentation of this file.
1#pragma once
2
4#include "ble_uuid.h"
5
6#include <array>
7#include <functional>
8#include <span>
9#include <vector>
10
11#ifdef USE_ESP32
12#ifdef USE_ESP32_BLE_ADVERTISING
13
14#include <esp_gap_ble_api.h>
15
16namespace esphome::esp32_ble {
17
19 public:
20 BLEAdvertising(uint32_t advertising_cycle_time);
21
22 void loop();
23
24 void add_service_uuid(ESPBTUUID uuid);
26 void set_manufacturer_data(const std::vector<uint8_t> &data);
27 void set_appearance(uint16_t appearance) { this->appearance_ = appearance; }
28 void set_service_data(const std::vector<uint8_t> &data);
29 void set_service_data(std::span<const uint8_t> data);
30 void set_include_name(bool include_name) { this->include_name_in_adv_ = include_name; }
32 void set_priority_uuid(ESPBTUUID uuid) { this->priority_uuid_ = uuid; }
33 void clear_priority_uuid() { this->priority_uuid_ = {}; }
34 void register_raw_advertisement_callback(std::function<void(bool)> &&callback);
35
36 void start();
37 void stop();
39 void request_start() { this->start_pending_ = true; }
40 void cancel_start() { this->start_pending_ = false; }
41
42 protected:
43 // An element longer than this never fits in a 31 byte packet
44 static constexpr size_t MAX_DATA_LEN = 29;
45
46 esp_err_t services_advertisement_();
47
48 esp_ble_adv_params_t advertising_params_;
49 std::vector<ESPBTUUID> advertising_uuids_;
50
51 std::vector<std::function<void(bool)>> raw_advertisements_callbacks_;
52
55 std::array<uint8_t, MAX_DATA_LEN> manufacturer_data_;
56 std::array<uint8_t, MAX_DATA_LEN> service_data_;
57 ESPBTUUID priority_uuid_; // unset when no service has priority
58 uint16_t appearance_{0};
59 int8_t current_adv_index_{-1}; // -1 means standard scan response
63 bool start_pending_{false};
64};
65
66} // namespace esphome::esp32_ble
67
68#endif // USE_ESP32_BLE_ADVERTISING
69#endif // USE_ESP32
void set_manufacturer_data(const std::vector< uint8_t > &data)
void set_include_name(bool include_name)
std::vector< ESPBTUUID > advertising_uuids_
std::array< uint8_t, MAX_DATA_LEN > service_data_
std::vector< std::function< void(bool)> > raw_advertisements_callbacks_
BLEAdvertising(uint32_t advertising_cycle_time)
void set_priority_uuid(ESPBTUUID uuid)
Give uuid and the service data the advertising packet; the rest moves to the scan response.
static constexpr size_t MAX_DATA_LEN
esp_ble_adv_params_t advertising_params_
void set_service_data(const std::vector< uint8_t > &data)
std::array< uint8_t, MAX_DATA_LEN > manufacturer_data_
void register_raw_advertisement_callback(std::function< void(bool)> &&callback)
void set_appearance(uint16_t appearance)
void request_start()
Send from the next loop(): a burst of refreshes becomes one send instead of flooding the Bluetooth ta...
static void uint32_t