Skip to content

Commit

Permalink
End discovery stream when stopped
Browse files Browse the repository at this point in the history
Watch the adapter's Discovering property for
changes and end the discovery stream when it
becomes false.
  • Loading branch information
surban committed Apr 4, 2024
1 parent 5a76acb commit a1a4f01
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
11 changes: 6 additions & 5 deletions bluer-tools/src/blumon.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl DeviceMonitor {
// bluetoothd stops discovery automatically after some time,
// thus we restart it periodically.
tokio::select! {
res = this.perform(&mut logger) => break res,
res = this.perform(&mut logger) => res?,
() = sleep(RESTART_INTERVAL) => (),
}
sleep(Duration::from_secs(1)).await;
Expand All @@ -124,9 +124,9 @@ impl DeviceMonitor {

loop {
tokio::select! {
Some(device_event) = device_events.next() => {
device_event = device_events.next() => {
match device_event {
AdapterEvent::DeviceAdded(addr) => {
Some(AdapterEvent::DeviceAdded(addr)) => {
match self.devices.get_mut(&addr) {
Some(data) => data.last_seen = Instant::now(),
None => self.add_device(addr).await,
Expand All @@ -135,8 +135,9 @@ impl DeviceMonitor {
logger.log_device(&device).await?;
}
},
AdapterEvent::DeviceRemoved(addr) => self.remove_device(addr).await,
_ => (),
Some(AdapterEvent::DeviceRemoved(addr)) => self.remove_device(addr).await,
Some(_) => (),
None => break,
}
},
_ = &mut next_update => {
Expand Down
15 changes: 11 additions & 4 deletions bluer/src/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ use dbus::{
Path,
};
use futures::{
future,
stream::{self, SelectAll},
Stream, StreamExt,
};
Expand Down Expand Up @@ -169,10 +170,16 @@ impl Adapter {
/// The discovery filter can be configured using [set_discovery_filter](Self::set_discovery_filter).
pub async fn discover_devices(&self) -> Result<impl Stream<Item = AdapterEvent>> {
let token = self.discovery_session().await?;
let change_events = self.events().await?.map(move |evt| {
let _token = &token;
evt
});
let change_events = self
.events()
.await?
.map(move |evt| {
let _token = &token;
evt
})
.take_while(|evt| {
future::ready(!matches!(evt, AdapterEvent::PropertyChanged(AdapterProperty::Discovering(false))))
});

let known = self.device_addresses().await?;
let known_events = stream::iter(known).map(AdapterEvent::DeviceAdded);
Expand Down

0 comments on commit a1a4f01

Please sign in to comment.