How to reinitialize BLE?

vonnieda
Posts: 82
Joined: Tue Nov 07, 2017 3:42 pm

How to reinitialize BLE?

Postby vonnieda » Wed Apr 18, 2018 5:25 pm

Hi folks,

I'm trying to reinitialize (shutdown, restart) BLE and every method I try seems to cause an assert or crash. Is there any documentation anywhere that describes the right sequence of calls to bring down BLE after having brought it up and initialized a GATTS service?

My init process looks like this:

Code: Select all

esp_bt_controller_mem_release(ESP_BT_MODE_CLASSIC_BT);
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
esp_bt_controller_init(&bt_cfg);
esp_bt_controller_enable(ESP_BT_MODE_BLE))
esp_bluedroid_init())
esp_bluedroid_enable())
esp_ble_gap_register_callback(gap_event_handler))
esp_ble_gatts_register_callback(gatts_event_handler))
esp_ble_gatts_app_register(0))
esp_ble_gatt_set_local_mtu(500))
esp_ble_gap_start_advertising(&adv_params);
If I try to run those in reverse, like this:

Code: Select all

esp_ble_gap_disconnect(connected_device_address);
esp_ble_gap_stop_advertising();
esp_ble_gatts_stop_service(gl_profile.service_handle);
esp_ble_gatts_delete_service(gl_profile.service_handle);
esp_ble_gatts_app_unregister(gl_profile.gatts_if);
esp_bluedroid_disable();
esp_bluedroid_deinit();
esp_bt_controller_disable();
esp_bt_controller_deinit();
I get:

Code: Select all

assertion "config != NULL" failed: file "/Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_config.c", line 312, function: btc_config_flush
abort() was called at PC 0x400f097f on core 0
0x400f097f: __assert_func at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdlib/../../../.././newlib/libc/stdlib/assert.c:63 (discriminator 8)


Backtrace: 0x400967fc:0x3ffb3bf0 0x400969f7:0x3ffb3c10 0x400f097f:0x3ffb3c30 0x401310f2:0x3ffb3c60 0x40131103:0x3ffb3c80 0x4011916f:0x3ffb3ca0 0x401191b4:0x3ffb3cc0 0x40119245:0x3ffb3ce0
0x400967fc: invoke_abort at /Users/jason/esp/esp-idf/components/esp32/./panic.c:648

0x400969f7: abort at /Users/jason/esp/esp-idf/components/esp32/./panic.c:648

0x400f097f: __assert_func at /home/jeroen/esp8266/esp32/newlib_xtensa-2.2.0-bin/newlib_xtensa-2.2.0/xtensa-esp32-elf/newlib/libc/stdlib/../../../.././newlib/libc/stdlib/assert.c:63 (discriminator 8)

0x401310f2: btc_config_flush at /Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_config.c:329

0x40131103: btc_config_shut_down at /Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_config.c:329

0x4011916f: btc_disable_bluetooth at /Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_main.c:47

0x401191b4: btc_main_call_handler at /Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_main.c:105

0x40119245: btc_task at /Users/jason/esp/esp-idf/components/bt/bluedroid/btc/core/btc_task.c:101
The offending call seems to be esp_bluedroid_disable().

So, can anyone point me to information about how to properly shutdown and restart BLE?

Thanks,
Jason

User avatar
kolban
Posts: 1683
Joined: Mon Nov 16, 2015 4:43 pm
Location: Texas, USA

Re: How to reinitialize BLE?

Postby kolban » Thu Apr 19, 2018 12:08 am

I may be completely off target here ... but a thought strikes me. Some of those commands may be asynchronous meaning that when you request something to be done, it is not immediately performed before return but instead you are notified by an event by callback when it is complete. Could it be that you have to wait for indication that the previous esp_ble_* function has completed before performing the next command?
Free book on ESP32 available here: https://leanpub.com/kolban-ESP32

vonnieda
Posts: 82
Joined: Tue Nov 07, 2017 3:42 pm

Re: How to reinitialize BLE?

Postby vonnieda » Thu Apr 19, 2018 9:27 pm

kolban wrote:I may be completely off target here ... but a thought strikes me. Some of those commands may be asynchronous meaning that when you request something to be done, it is not immediately performed before return but instead you are notified by an event by callback when it is complete. Could it be that you have to wait for indication that the previous esp_ble_* function has completed before performing the next command?
Hi kolban,

Thank you for your response. Your comment helped me get a little further, but I'm still stuck. I was able to finish the de-init process by moving some of the calls to the gatts_event_handler, but now when I try to re-initialize I get a number of errors.

I also found in the documentation that esp_bt_controller_deinit() is not really finished: "This function is not whole completed, esp_bt_controller_init cannot called after this function."

If I remove that call, then I get an error 0x0103 on esp_bt_controller_enable(ESP_BT_MODE_BLE), and if I remove that call I get an error 0x0103 on esp_bluedroid_init().

So, I'll repeat my question: Does anyone know how to correctly shutdown and then restart BLE?

Who is online

Users browsing this forum: Bing [Bot], Google [Bot] and 14 guests