embassy-rs / trouble

A Rust Host BLE stack with a future goal of qualification.
Apache License 2.0
115 stars 19 forks source link

How to manage reconnection? #108

Closed Syphixs closed 1 month ago

Syphixs commented 1 month ago

After a disconnection event happens (eg. phone is out of range) what would be a smart way to implement an auto-reconnect feature? Should it be as simple as restarting the advertise_task?

lulf commented 1 month ago

Yes, but how simple you can do this depends on how many connections you want. If you want only 1 connection, you simply have a loop with advertise, accept connection, do stuff, and continue if connection is lost (advertise will start again).

If you want to handle > 1 connection, you may need to define an async task with embassy_executor::task(pool_size = 4), and spawn that with the connection handle you get from accept(), and let advertise continue to run until a new connection is established.

HaoboGu commented 1 month ago

Does someone work on the pairing manager or security manager? e.g. saving the bonding information and auto-reconnect when the device is detected.

Syphixs commented 1 month ago

alright thank you for clarification. Currently I am only working with one connection but if the use case arises I will try your suggested executor::task approach.