Patching for BLE co-existence

This commit is contained in:
RoboMagus 2024-11-16 20:51:13 +01:00
parent ec06744bd5
commit ad5967ad56
3 changed files with 44 additions and 19 deletions

View file

@ -149,18 +149,31 @@ bool ESP32BLE::ble_setup_() {
} }
#endif #endif
#ifdef BT_CLASSIC_INCLUDED_PLEASE_DONT_MEM_RELEASE
esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT); esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
#endif
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
ESP_LOGD(TAG, "Initializing BlueDroid");
err = esp_bluedroid_init(); err = esp_bluedroid_init();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_init failed: %d", err); ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Initialized!");
}
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
ESP_LOGD(TAG, "Enabling BlueDroid");
err = esp_bluedroid_enable(); err = esp_bluedroid_enable();
if (err != ESP_OK) { if (err != ESP_OK) {
ESP_LOGE(TAG, "esp_bluedroid_enable failed: %d", err); ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Enabled!");
}
if (!this->gap_event_handlers_.empty()) { if (!this->gap_event_handlers_.empty()) {
err = esp_ble_gap_register_callback(ESP32BLE::gap_event_handler); err = esp_ble_gap_register_callback(ESP32BLE::gap_event_handler);

View file

@ -191,3 +191,5 @@ async def to_code(config):
add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL_DEBUG", True) add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL_DEBUG", True)
add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL", 5) add_idf_sdkconfig_option("CONFIG_BT_LOG_BTC_TRACE_LEVEL", 5)
cg.add_define("BT_CLASSIC_INCLUDED_PLEASE_DONT_MEM_RELEASE")

View file

@ -56,6 +56,12 @@ bool ESP32BtClassic::bt_setup_() {
return false; return false;
} }
#else #else
ESP_LOGI(TAG, "BT_MODE: %d", BT_MODE);
// ESP_BT_MODE_IDLE = 0x00
// ESP_BT_MODE_BLE = 0x01
// ESP_BT_MODE_CLASSIC_BT = 0x02
// ESP_BT_MODE_BTDM = 0x03
if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) { if (esp_bt_controller_get_status() != ESP_BT_CONTROLLER_STATUS_ENABLED) {
// start bt controller // start bt controller
if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) { if (esp_bt_controller_get_status() == ESP_BT_CONTROLLER_STATUS_IDLE) {
@ -82,20 +88,24 @@ bool ESP32BtClassic::bt_setup_() {
} }
#endif #endif
ESP_LOGD(TAG, "Initializing BlueDroid");
if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) { if (esp_bluedroid_get_status() == ESP_BLUEDROID_STATUS_UNINITIALIZED) {
ESP_LOGD(TAG, "Initializing BlueDroid");
if ((err = esp_bluedroid_init()) != ESP_OK) { if ((err = esp_bluedroid_init()) != ESP_OK) {
ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err)); ESP_LOGE(TAG, "%s initialize bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Initialized!");
} }
ESP_LOGD(TAG, "Enabling BlueDroid");
if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) { if (esp_bluedroid_get_status() != ESP_BLUEDROID_STATUS_ENABLED) {
ESP_LOGD(TAG, "Enabling BlueDroid");
if ((err = esp_bluedroid_enable()) != ESP_OK) { if ((err = esp_bluedroid_enable()) != ESP_OK) {
ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err)); ESP_LOGE(TAG, "%s enable bluedroid failed: %s\n", __func__, esp_err_to_name(err));
return false; return false;
} }
} else {
ESP_LOGD(TAG, "BlueDroid Already Enabled!");
} }
bool success = gap_startup(); bool success = gap_startup();
@ -156,11 +166,10 @@ void ESP32BtClassic::startScan(const uint64_t u64_addr) {
for (auto *listener : scan_start_listners_) { for (auto *listener : scan_start_listners_) {
listener->on_scan_start(); listener->on_scan_start();
} }
} else {
ESP_LOGE(TAG, "Could not start scan! Error: %s\n BlueDroid status: %d\n Controller status: %d",
esp_err_to_name(result), esp_bluedroid_get_status(), esp_bt_controller_get_status());
} }
else {
ESP_LOGE(TAG, "Could not start scan! Error: %s\n BlueDroid status: %d\n Controller status: %d", esp_err_to_name(result), esp_bluedroid_get_status(), esp_bt_controller_get_status());
}
} }
void ESP32BtClassic::addScan(const bt_scan_item &scan) { void ESP32BtClassic::addScan(const bt_scan_item &scan) {
@ -233,7 +242,8 @@ optional<bt_scan_item> ESP32BtClassic::handle_scan_result(const rmt_name_result
ESP_LOGD(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' scan result: %s (%d) in %lu ms", EXPAND_MAC_F(result.bda), ESP_LOGD(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' scan result: %s (%d) in %lu ms", EXPAND_MAC_F(result.bda),
esp_bt_status_to_str(result.stat), result.stat, scanDuration); esp_bt_status_to_str(result.stat), result.stat, scanDuration);
ESP_LOGD(TAG, "BlueDroid status: %d\n Controller status: %d", esp_bluedroid_get_status(), esp_bt_controller_get_status()); ESP_LOGD(TAG, "BlueDroid status: %d\n Controller status: %d", esp_bluedroid_get_status(),
esp_bt_controller_get_status());
if (it->scans_remaining == 0) { if (it->scans_remaining == 0) {
ESP_LOGW(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' not found on final scan. Removing from scan list.", ESP_LOGW(TAG, "Device '%02X:%02X:%02X:%02X:%02X:%02X' not found on final scan. Removing from scan list.",