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> 18 #include <esp32-hal-bt.h> 22 namespace esp32_ble_beacon {
24 static const char *
const TAG =
"esp32_ble_beacon";
27 .flags = {0x02, 0x01, 0x06}, .length = 0x1A, .type = 0xFF, .company_id = {0x4C, 0x00}, .beacon_type = {0x02, 0x15}};
30 ESP_LOGCONFIG(TAG,
"ESP32 BLE Beacon:");
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,
"-");
41 " UUID: %s, Major: %u, Minor: %u, Min Interval: %ums, Max Interval: %ums, Measured Power: %d" 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,
72 memcpy(&ibeacon_adv_data.ibeacon_vendor.proximity_uuid, this->uuid_.data(),
73 sizeof(ibeacon_adv_data.ibeacon_vendor.proximity_uuid));
76 ibeacon_adv_data.ibeacon_vendor.measured_power =
static_cast<uint8_t
>(this->
measured_power_);
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_);
81 ESP_LOGW(TAG,
"esp_ble_tx_power_set failed: %s", esp_err_to_name(err));
83 err = esp_ble_gap_config_adv_data_raw((uint8_t *) &ibeacon_adv_data,
sizeof(ibeacon_adv_data));
85 ESP_LOGE(TAG,
"esp_ble_gap_config_adv_data_raw failed: %s", esp_err_to_name(err));
96 case ESP_GAP_BLE_ADV_DATA_RAW_SET_COMPLETE_EVT: {
99 ESP_LOGE(TAG,
"esp_ble_gap_start_advertising failed: %s", esp_err_to_name(err));
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));
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));
115 ESP_LOGD(TAG,
"BLE stopped advertising successfully");
struct { uint8_t flags[3] esp_ble_ibeacon_head_t
std::array< uint8_t, 16 > uuid_
const float AFTER_BLUETOOTH
void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) override
esp_power_level_t tx_power_
void advertising_register_raw_advertisement_callback(std::function< void(bool)> &&callback)
esp_ble_adv_params_t ble_adv_params_
struct { esp_ble_ibeacon_head_t ibeacon_head esp_ble_ibeacon_t
constexpr14 T byteswap(T n)
Implementation of SPI Controller mode.
float get_setup_priority() const override
void dump_config() override