8 #include <esp_bt_main.h> 9 #include <esp_bt_device.h> 10 #include <esp_gap_ble_api.h> 11 #include <freertos/FreeRTOS.h> 12 #include <freertos/FreeRTOSConfig.h> 13 #include <freertos/task.h> 14 #include <nvs_flash.h> 17 #include <esp32-hal-bt.h> 23 static const char *
const TAG =
"esp32_ble";
27 ESP_LOGCONFIG(TAG,
"Setting up BLE...");
30 ESP_LOGE(TAG,
"BLE could not be set up");
35 #ifdef USE_ESP32_BLE_SERVER 41 #endif // USE_ESP32_BLE_SERVER 43 ESP_LOGD(TAG,
"BLE setup complete");
47 esp_err_t err = nvs_flash_init();
49 ESP_LOGE(TAG,
"nvs_flash_init failed: %d", err);
55 ESP_LOGE(TAG,
"btStart failed: %d", esp_bt_controller_get_status());
59 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
61 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
62 esp_bt_controller_config_t cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
63 err = esp_bt_controller_init(&cfg);
65 ESP_LOGE(TAG,
"esp_bt_controller_init failed: %s", esp_err_to_name(err));
68 while (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE)
71 if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_INITED) {
72 err = esp_bt_controller_enable(ESP_BT_MODE_BLE);
74 ESP_LOGE(TAG,
"esp_bt_controller_enable failed: %s", esp_err_to_name(err));
78 if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
79 ESP_LOGE(TAG,
"esp bt controller enable failed");
85 esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
87 err = esp_bluedroid_init();
89 ESP_LOGE(TAG,
"esp_bluedroid_init failed: %d", err);
92 err = esp_bluedroid_enable();
94 ESP_LOGE(TAG,
"esp_bluedroid_enable failed: %d", err);
101 ESP_LOGE(TAG,
"esp_ble_gap_register_callback failed: %d", err);
109 ESP_LOGE(TAG,
"esp_ble_gatts_register_callback failed: %d", err);
117 ESP_LOGE(TAG,
"esp_ble_gattc_register_callback failed: %d", err);
123 if (name.length() > 20) {
125 name.erase(name.begin() + 13, name.end() - 7);
127 name = name.substr(0, 20);
131 err = esp_ble_gap_set_device_name(name.c_str());
133 ESP_LOGE(TAG,
"esp_ble_gap_set_device_name failed: %d", err);
137 err = esp_ble_gap_set_security_param(ESP_BLE_SM_IOCAP_MODE, &(this->
io_cap_),
sizeof(uint8_t));
139 ESP_LOGE(TAG,
"esp_ble_gap_set_security_param failed: %d", err);
151 while (ble_event !=
nullptr) {
152 switch (ble_event->
type_) {
178 ESP_LOGV(TAG,
"(BLE) gap_event_handler - %d", event);
180 gap_handler->gap_event_handler(event, param);
185 esp_ble_gatts_cb_param_t *param) {
191 esp_ble_gatts_cb_param_t *param) {
192 ESP_LOGV(TAG,
"(BLE) gatts_event [esp_gatt_if: %d] - %d", gatts_if, event);
194 gatts_handler->gatts_event_handler(event, gatts_if, param);
199 esp_ble_gattc_cb_param_t *param) {
205 esp_ble_gattc_cb_param_t *param) {
206 ESP_LOGV(TAG,
"(BLE) gattc_event [esp_gatt_if: %d] - %d", gattc_if, event);
208 gattc_handler->gattc_event_handler(event, gattc_if, param);
215 const uint8_t *mac_address = esp_bt_dev_get_address();
217 const char *io_capability_s;
220 io_capability_s =
"display_only";
223 io_capability_s =
"display_yes_no";
226 io_capability_s =
"keyboard_only";
228 case ESP_IO_CAP_NONE:
229 io_capability_s =
"none";
231 case ESP_IO_CAP_KBDISP:
232 io_capability_s =
"keyboard_display";
235 io_capability_s =
"invalid";
238 ESP_LOGCONFIG(TAG,
"ESP32 BLE:");
239 ESP_LOGCONFIG(TAG,
" MAC address: %02X:%02X:%02X:%02X:%02X:%02X", mac_address[0], mac_address[1], mac_address[2],
240 mac_address[3], mac_address[4], mac_address[5]);
241 ESP_LOGCONFIG(TAG,
" IO Capability: %s", io_capability_s);
243 ESP_LOGCONFIG(TAG,
"ESP32 BLE: bluetooth stack is not enabled");
249 u |= uint64_t(address[0] & 0xFF) << 40;
250 u |= uint64_t(address[1] & 0xFF) << 32;
251 u |= uint64_t(address[2] & 0xFF) << 24;
252 u |= uint64_t(address[3] & 0xFF) << 16;
253 u |= uint64_t(address[4] & 0xFF) << 8;
254 u |= uint64_t(address[5] & 0xFF) << 0;
uint64_t ble_addr_to_uint64(const esp_bd_addr_t address)
void set_min_preferred_interval(uint16_t interval)
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
struct esphome::esp32_ble::BLEEvent::@76::gattc_event gattc
std::vector< GAPEventHandler * > gap_event_handlers_
std::vector< GATTcEventHandler * > gattc_event_handlers_
Queue< BLEEvent > ble_events_
std::vector< GATTsEventHandler * > gatts_event_handlers_
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
static void gattc_event_handler(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
void real_gattc_event_handler_(esp_gattc_cb_event_t event, esp_gatt_if_t gattc_if, esp_ble_gattc_cb_param_t *param)
struct esphome::esp32_ble::BLEEvent::@76::gap_event gap
float get_setup_priority() const override
void real_gatts_event_handler_(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
Application App
Global storage of Application pointer - only one Application can exist.
enum esphome::esp32_ble::BLEEvent::ble_event_t type_
union esphome::esp32_ble::BLEEvent::@76 event_
const std::string & get_name() const
Get the name of this Application set by pre_setup().
bool is_name_add_mac_suffix_enabled() const
void dump_config() override
virtual void mark_failed()
Mark this component as failed.
void real_gap_event_handler_(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
struct esphome::esp32_ble::BLEEvent::@76::gatts_event gatts
void IRAM_ATTR HOT delay(uint32_t ms)
BLEAdvertising * advertising_
void set_scan_response(bool scan_response)