From 2d94048b0d893c0b37258502d3aad5766104ca7f Mon Sep 17 00:00:00 2001 From: Igor Karatayev Date: Thu, 25 Jan 2024 18:39:14 +0300 Subject: [PATCH] Fixed bluetooth initialization crash on espressif esp32 boards. --- src/utility/HCIVirtualTransport.cpp | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/src/utility/HCIVirtualTransport.cpp b/src/utility/HCIVirtualTransport.cpp index 046a0e72..abd167dd 100644 --- a/src/utility/HCIVirtualTransport.cpp +++ b/src/utility/HCIVirtualTransport.cpp @@ -65,11 +65,13 @@ HCIVirtualTransportClass::~HCIVirtualTransportClass() int HCIVirtualTransportClass::begin() { +#ifdef ARDUINO_ARCH_ESP32 + if (!btStart()) { + return 0; + } +#else btStarted(); // this somehow stops the arduino ide from initializing bluedroid - rec_buffer = xStreamBufferCreate(258, 1); - send_buffer = xStreamBufferCreate(258, 1); - esp_err_t ret = nvs_flash_init(); if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { ESP_ERROR_CHECK(nvs_flash_erase()); @@ -86,17 +88,26 @@ int HCIVirtualTransportClass::begin() esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); esp_bt_controller_init(&bt_cfg); esp_bt_controller_enable(ESP_BT_MODE_BLE); +#endif + + rec_buffer = xStreamBufferCreate(258, 1); + send_buffer = xStreamBufferCreate(258, 1); + xTaskCreatePinnedToCore(&bleTask, "bleTask", 2048, NULL, 5, &bleHandle, 0); return 1; } void HCIVirtualTransportClass::end() { + vTaskDelete(bleHandle); vStreamBufferDelete(rec_buffer); vStreamBufferDelete(send_buffer); +#ifdef ARDUINO_ARCH_ESP32 + btStop(); +#else esp_bt_controller_disable(); esp_bt_controller_deinit(); - vTaskDelete(bleHandle); +#endif } void HCIVirtualTransportClass::wait(unsigned long timeout)