How to disable/enable BLE? esp-idf (solved)

phantomBlurrr
Posts: 19
Joined: Mon Jun 10, 2019 4:01 pm

How to disable/enable BLE? esp-idf (solved)

Postby phantomBlurrr » Thu Jul 25, 2019 4:26 pm

Hello,
In my project I need to use BLE and Wi-Fi. There's only one radio. I want to disable the BLE to use Wi-Fi. After Wi-Fi serves it's purpose, disable Wi-Fi, enable BLE. If user finished using BLE, disable BLE, enable Wi-Fi. So on and so on.

I tried using a command which is called FROM GATT (the user sends a command to the ESP from phone saying that he's finished using BLE). The command calls bluedroid disable and BT Controller disable. But the ESP gets stuck as soon as the bluedroid disable is called. My guess is that since I'm calling the disable functionality from the GATT itself, the rest of the code in that function (the BT Controller disable) doesn't get executed (since the bluedroid is disabled, the function is interrupted from completing).

I also tried moving the disable function to the main .c file, and calling the function from GATT, but the same behavior occurs, probably because though I moved the function, it's STILL called from GATT, so that's why I see same behavior.

Lastly, I moved the function to the app_main() just to test if the code is doing what I want. After the BLE server is set up there's a vTaskDelay which waits for 10 seconds then disables the BLE. This works as expected, on start I can see the GATT server and connect to it, the delay runs so after 10 seconds the GATT disconnects and the signal is lost (since it was disabled). Then there is a second vTaskDelay for another 10 seconds and then I want to enable BLE. Again, this is just to test that I'm doing BLE disable/enable correctly. Clearly, I am not, because when I call bluedroid and BT Controller enables (after second vTaskDelay), the ESP throws error:

BT_HCI: command_timed_out hci layer timeout waiting for response to a command. opcode: 0xc03

The BLE does not enable at this point.

So my question is: How can I enable/disable the BLE at will at any point in my code? I need my phone to connect for a bit, then say it's done which the ESP will disconnect and disable BLE. Then later, the BLE will be reenabled by some conditional statement, and the phone can reconnect!

I thought I just had to use bluedroid & BT Controller disable, then later, use bluedroid & BT Controller enable, but that isn't working. Also what's the correct way to make the ESP disable if I send the command from the phone itself?

By the way: I am using only esp-idf; NO Kolban, NO Arduino

Anything helps!
Last edited by phantomBlurrr on Mon Jul 29, 2019 5:06 pm, edited 1 time in total.

chegewara
Posts: 2378
Joined: Wed Jun 14, 2017 9:00 pm

Re: How to disable/enable BLE? esp-idf

Postby chegewara » Sat Jul 27, 2019 2:16 am

Code: Select all

esp_bluedroid_disable();
esp_bluedroid_deinit();
esp_bt_controller_disable();
esp_bt_controller_deinit();

phantomBlurrr
Posts: 19
Joined: Mon Jun 10, 2019 4:01 pm

Re: How to disable/enable BLE? esp-idf

Postby phantomBlurrr » Mon Jul 29, 2019 1:55 pm

chegewara wrote:
Sat Jul 27, 2019 2:16 am

Code: Select all

esp_bluedroid_disable();
esp_bluedroid_deinit();
esp_bt_controller_disable();
esp_bt_controller_deinit();
Hello,
Thanks for your reply; I am aware of the commands needed to disable BT. What I am having difficulty is *where* to place this code. Currently, the ESP boots to BLE mode, later I call wifi_start() from a command in GATT server (this is received by ESP from phone app). When the WiFi start, it is there in the beginning of the wifi handler that the BT is disabled with the commands you mentioned, and the WiFi continues normally in its event handler and connects to access point. Now, if the WiFi disconnects, I want it to go back to BLE, so in the wifi disconnect case in the wifi event handler, I am attempting to re-enable BT and stop wifi using BT_enable() then wifi_stop(). Is this the correct method for using the commands. At the moment this kind of works but seems bugged (I get BT signal for a split second and then wifi stops, but then BT disables and wifi restarts, I'm assuming this is correct method but my wifi_handler logic is incorrect so it loops back to wifi mode).
Again, thanks for your response!

phantomBlurrr
Posts: 19
Joined: Mon Jun 10, 2019 4:01 pm

Re: How to disable/enable BLE? esp-idf

Postby phantomBlurrr » Mon Jul 29, 2019 5:06 pm

Hello,
It took a few tries but I have figured out the proper sequence for enabling/disabling BLE in order to use WiFi. Start the ESP in BLE mode and add a command that will start the WiFi. During WiFi init, do NOT start the WiFi, so don't use wifi_start(). When you call the command to start the wifi, then use wifi_start(). Now inside the wifi event handler, add the code to disable the BLE before it gets to the event cases. When you disable the BLE make sure you use both disable AND deinit commands. I was trying to do it without any deinit, which caused errors when I'd try to reenable BLE. After that, go to disconnect case and add code so that wifi is stopped, so wifi_stop(). For some reason, the wifi has one last event handler callback, you can capitalize on this and add the BLE enable code. So reinitialize BLE just like in the beginning of the program. And there you go, now you can get the ESP to connect to wifi, and if the wifi connection drops or becomes unavailable, the ESP will switch back to BLE so that it can be configured once more. This method is nice because you don't have to do some weird nvs/reboot loop to keep track of states and switch from BLE to WiFi and only initializing one at a time through the programs lifespan.

Who is online

Users browsing this forum: ESP_ondrej, Google [Bot] and 105 guests