h2zero / NimBLE-Arduino

A fork of the NimBLE library structured for compilation with Arduino, for use with ESP32, nRF5x.
https://h2zero.github.io/NimBLE-Arduino/
Apache License 2.0
670 stars 138 forks source link

Impossible to reconnect after some time and multiple connections #582

Open Koxx3 opened 10 months ago

Koxx3 commented 10 months ago

Hello,

this is a bug that I have been tracking for a long time. It's difficult to reproduce.

After some time, it becomes impossible to reconnect to the ESP BLE. In 99.9% of the time, it works fine. But sometime, it's locked in a bad state.

I finally managed to capture the log (see next comment).

After that it is impossible to reconnect. I never seen the 'onAuthenticationComplete'.

I found a hack ... but it's really dirty. I have to deinit NimBLE and reinit. then it works again.

It's a complicated project, it's difficult to extract the BLE code.

Koxx3 commented 10 months ago

Here is the log : https://koxx3.fr.eu.org:8086/ble_conn_ko.txt

Note that at line 4704, I decide to reset the NimBLE (after 5 failure).

I've tried to connect another phone, it's not the phone or the phone BLE stack. When stuck. I tried with android and iphone, impossible to reconnect. They are stuck when "reading services" or "connect" state. Tried with my app, Ble Scanner or LighBlue app. Same thing.

After unbonding, impossible to rebond.

Koxx3 commented 10 months ago

As you can see, My ESP act as a BLE server, it also constinuously advertise and scan for devices.

No memory issue : MEM left = 54256 idle = 5584 tft = 1936 wifi = 3020 btn = 2452 canrx = 1600 cantx = 1884 sound = 408 serial = 4500 btCntr = 1580 btHost = 5212 CAN_WD_BI = 1544 CAN_LORX = 3524 Tmr = 1388 ipc0 = 256 esp_timer = 2816 loopTask = 5264

Koxx3 commented 10 months ago

Here is a the important code extraction : https://koxx3.fr.eu.org:8086/ble_conn_ko.cpp

Koxx3 commented 10 months ago

@h2zero , any help would be greatly appreciated. As said, it's very difficult to capture and I don't see anything wrong the my current code.

Koxx3 commented 10 months ago

Also, my PIO config :

[env]
platform = https://github.com/tasmota/platform-espressif32/releases/download/v.2.0.5/platform-espressif32-v.2.0.5.zip
framework = arduino, espidf
board = esp32dev

lib_deps = 
    nimble=https://github.com/h2zero/NimBLE-Arduino.git@1.4.1+sha.bc333cc

build_flags = 
    -DCONFIG_BT_NIMBLE_LOG_LEVEL=0 ; => debug
    -DCONFIG_NIMBLE_CPP_LOG_LEVEL=4 ; => debug
    -DCONFIG_BT_NIMBLE_TASK_STACK_SIZE=8096 
    -DCONFIG_BT_NIMBLE_HOST_TASK_STACK_SIZE=10000
    -DCONFIG_BT_NIMBLE_MAX_CONNECTIONS=2
    -DMYNEWT_VAL_BLE_STORE_MAX_CCCDS=20
h2zero commented 10 months ago

Hi @Koxx3, I don't see any obvious problem in the logs. I suspect an issue with the BLE controller as it's well known to hang, especially in the version of the arduino core/IDF you are on.

Unfortunately, if that's the case, what you are doing to workaround it is the best way to resolve the issue.

Koxx3 commented 10 months ago

thanks a lot for your feedback. do you known which version of arduino/idf could improve this sitation ? migration on a large project is always a pain. If I can target a version, it will help :D

h2zero commented 10 months ago

The old versions, such as 1.0.6 were better in this regard, but I doubt you want to go back to that. I believe this should be fixed when the new version based on IDF 5.x is released, you could try testing it now.

h2zero commented 1 month ago

@Koxx3 are you still having this issue?

h2zero commented 3 weeks ago

@Koxx3 A number of commits where just made to the master branch and 1 to the release branch that should resolve this.