Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE Start scan error while a connection is being established. (IDFGH-4894) #6688

Closed
alutov opened this issue Mar 10, 2021 · 8 comments
Closed
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally

Comments

@alutov
Copy link

alutov commented Mar 10, 2021

Maybe someone will come in handy. Recently I came across one feature of the esp32 BLE library. If I try to start a scan (esp_ble_gap_start_scanning (...)) while a connection is being established with some ble device (already called esp_ble_gattc_open (...) but even before ESP_GATTC_OPEN_EVT), the command is executed as expected with the error "scan start failed ". But after that it will be impossible to start or stop the scan (I suspect the connection command does not work either) until the esp32 is restarted. That is, the existing connections are still working, but it is impossible to establish new ones. In reality this situation occurs when two connections are lost with a small time difference. The scan has already started and stopped, the first device has been found, a connection command has been given (here I note that the default connection timeout is 30 seconds). And at this time ESP_GATTC_DISCONNECT_EVT comes from the second device, which starts the scan again. In order to avoid this you need to check two conditions in the starting scan procedure and don't start a scan if any of them is true:
1 - whether a scan has already been started or whether there has already been a command to start a scan.
2 - whether a connection is being established with any device at the moment.
In the example I found, the second check is not performed.
After the correction my program works much more reliably. Maybe it will be interesting for fix issues #6379 and #6073.

@github-actions github-actions bot changed the title Start scan error while a connection is being established. Start scan error while a connection is being established. (IDFGH-4894) Mar 10, 2021
@alutov alutov changed the title Start scan error while a connection is being established. (IDFGH-4894) BLE Start scan error while a connection is being established. (IDFGH-4894) Mar 10, 2021
@espressif-bot espressif-bot added the Status: In Progress Work is in progress label Mar 19, 2021
@xulongzhuang
Copy link

Hi,alutov,According to the problem you described above, my recurrence is as follows: Use gattc_multi_connect to connect gatts_a and gatts_b, and make some small changes to gattc_multi_connect: After calling esp_ble_gattc_open() to connect to gatts_a, immediately call esp_ble_gap_start_scanning() to start scanning, and also encountered "Scan start failed" error, and no longer scan gatts_b, is my above test the problem you described?

@alutov
Copy link
Author

alutov commented Mar 19, 2021

Yes, that's what I wrote about. But after that I could not run any scan (scan gatts_a, scan gatts_b, etc) at all before restarting esp. All the time the "scan start error" with code 1. I don't know if it's worth editing the library, you can just do an extra check in your code. But you just need to know about it.

@xulongzhuang
Copy link

When gatts_a is actively disconnected from it, ESP_GATTC_DISCONNECT_EVT will be triggered at this time. If the start scan is performed at this time, I think it can scan gatts_a or gatts_b again without waiting for esp to restart, right? I am using IDF releasev/4.3 gattc_multi_connect example like this

@alutov
Copy link
Author

alutov commented Mar 19, 2021

If the no any active esp_ble_gattc_open (...) tasks at that moment, then yes, the scan will start. If at this moment there was an establishment of a connection with gatts_b the scan will not start. It's normal because ble library is busy. The connection with gatts_b most likely will not happen (I don’t remember exactly here), and after that ESP_GATTC_OPEN_EVT returns an error, the moment of the start of the scan comes again. The problem is that the scan will not start here with error 1.

@xulongzhuang
Copy link

Hi, alutov, I think the mechanism of this library is set up like this: when esp_ble_gattc_open (...) triggers to return the result of ESP_GATTC_OPEN_EVT, scanning or other actions can be performed. It does not support concurrent operations. Of course, adding some detection at the application layer Better, it is better to avoid this concurrency in actual use. For example, in gattc_multi_connect, the scan is placed after the ESP_GATTC_WRITE_CHAR_EVT event

@alutov
Copy link
Author

alutov commented Mar 22, 2021

Totally agree with you but the fact of the matter is that in the given example there is a call to the scan procedure in the ESP_GATTC_DISCONNECT_EVT. But for some reason, only for the gatts_a device, the scan is not called in the gatts_b and gatts_c. I came across a description somewhere that explained the principle of multiconnect. First, device A, then B, and only then C. In my code the devices must be connected in any order and independently of each other, and therefore I added the scan call to all ESP_GATTC_DISCONNECT_EVT procedures. Perhaps it is possible to call a scan only in a ESP_GATTC_WRITE_CHAR_EVT, but in this case this situation is also possible. I also agree that you can just check the additional condition and the problem is solved. You just need to pay attention to this, especially since the problem can appear after hours and even days of work and the loss of all connections without restart or any other indication besides the scan start error. I read at least 2 issues where there are similar problems.

@xulongzhuang
Copy link

In addition to checking before use, another method can also be to ensure that the two processes are not disturbed, such as starting a user task and using semaphores to ensure that each process is fully executed. Thank you for your feedback. This will help many people.

@Alvin1Zhang
Copy link
Collaborator

Thanks for reporting, feel free to reopen.

@espressif-bot espressif-bot added Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally and removed Status: In Progress Work is in progress labels Nov 30, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: NA Issue resolution is unavailable Status: Done Issue is done internally
Projects
None yet
Development

No branches or pull requests

4 participants