deviceplug / btleplug

Rust Cross-Platform Host-Side Bluetooth LE Access Library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Ability to update or reset the discovered Peripherals

GilShoshan94 opened this issue · comments

Goal

Be able to update the list of discovered peripherals and drop the ones that does not advertised anymore after a certain timeout.
Like in a android phone when you search for Bluetooth headphone, the list get updated and if you turn off your headphone, it will be drop of the list fairly quickly.

Context

I am currently using the polling method due to the "Linux Device Discovery Caveat" in order to discover the available peripherals.
So I am getting my list of adapter and then call start_scan(), wait a bit and then call peripherals(), very much like it is shown in the example subscribe_notify_characteristic.rs.

But, I am running the "wait a bit and then call peripherals()" in a loop in a spawn tokio task.
And the idea was to send to my main task an updated list of available peripherals.

My issue is that peripherals() as its documentation explain (Returns the list of Peripherals that have been discovered so far. Note that this list may contain peripherals that are no longer available.) never remove peripheral that are not available anymore (For example, when I turn those devices off).

When digging around into the lib it seems to me that the Adapter struct hold the discovered peripherals and never release them.
It seems that my only work around is to completely drop the adapter and recreate a new one from the manager with .adapters().

Feature Description

A method on the adapter to explicitly clear the internal list of discovered peripheral.

Or even better but might be harder to implement: to add a u8 field to the peripheral struct, I would called it ttl for Time To Live, and whenever a peripheral get discover or rediscover, this field would be set to TTL_MAX, for example 5, and whenever the adapter is polled with peripherals() call, each peripheral the adapter holds see their ttl decreased by 1, and then they pass a check, if ttl reach 0, the peripheral get removed from the adapter. This way, calls to peripherals() will returns the list of Peripherals that have been discovered so far and that their advertising was receive at least in the passed ttl polls. So the list may still contain peripherals that are no longer available, but far less and more just.