fix: connect to paired and trusted devices when they reappear

This commit is contained in:
Ashley Wulber 2024-03-19 14:18:58 -04:00 committed by Jeremy Soller
parent 829ae04cde
commit 40bae48195
No known key found for this signature in database
GPG key ID: D02FD439211AF56F

View file

@ -1,4 +1,4 @@
use std::{collections::HashMap, fmt::Debug, hash::Hash, sync::Arc, time::Duration}; use std::{collections::HashMap, fmt::Debug, hash::Hash, mem, sync::Arc, time::Duration};
use bluer::{ use bluer::{
agent::{Agent, AgentHandle}, agent::{Agent, AgentHandle},
@ -65,17 +65,7 @@ async fn start_listening(
// reconnect to paired and trusted devices // reconnect to paired and trusted devices
if state.bluetooth_enabled { if state.bluetooth_enabled {
for d in &state.devices { for d in &state.devices {
if d.properties if d.paired_and_trusted() {
.iter()
.filter(|p| {
matches!(
p,
DeviceProperty::Trusted(true) | DeviceProperty::Paired(true)
)
})
.count()
== 2
{
_ = session_state _ = session_state
.req_tx .req_tx
.send(BluerRequest::ConnectDevice(d.address)) .send(BluerRequest::ConnectDevice(d.address))
@ -260,6 +250,19 @@ impl BluerDevice {
icon, icon,
} }
} }
fn paired_and_trusted(&self) -> bool {
self.properties
.iter()
.filter(|p| {
matches!(
p,
DeviceProperty::Trusted(true) | DeviceProperty::Paired(true)
)
})
.count()
== 2
}
} }
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -513,18 +516,7 @@ impl BluerSessionState {
}; };
if state.bluetooth_enabled { if state.bluetooth_enabled {
for d in &state.devices { for d in &state.devices {
if d.properties if d.paired_and_trusted() {
.iter()
.filter(|p| {
matches!(
p,
DeviceProperty::Trusted(true)
| DeviceProperty::Paired(true)
)
})
.count()
== 2
{
_ = req_tx.send(BluerRequest::ConnectDevice(d.address)).await; _ = req_tx.send(BluerRequest::ConnectDevice(d.address)).await;
} }
} }
@ -539,12 +531,14 @@ impl BluerSessionState {
// Note: For some reason, this doesn't actually seem to work so well. it seems unreliable... // Note: For some reason, this doesn't actually seem to work so well. it seems unreliable...
pub(crate) fn process_changes(&self) { pub(crate) fn process_changes(&self) {
let tx = self.tx.clone(); let tx = self.tx.clone();
let req_tx = self.req_tx.clone();
let adapter_clone = self.adapter.clone(); let adapter_clone = self.adapter.clone();
let _monitor_devices: tokio::task::JoinHandle<Result<(), anyhow::Error>> = let _monitor_devices: tokio::task::JoinHandle<Result<(), anyhow::Error>> =
spawn(async move { spawn(async move {
let mut change_stream = adapter_clone.discover_devices_with_changes().await?; let mut change_stream = adapter_clone.discover_devices_with_changes().await?;
let mut devices_changed = false; let mut changed = false;
let mut milli_timeout = 10; let mut milli_timeout = 10;
let mut devices: Vec<BluerDevice> = Vec::new();
'outer: loop { 'outer: loop {
while let Ok(event) = while let Ok(event) =
timeout(Duration::from_millis(milli_timeout), change_stream.next()).await timeout(Duration::from_millis(milli_timeout), change_stream.next()).await
@ -552,10 +546,19 @@ impl BluerSessionState {
if event.is_none() { if event.is_none() {
break 'outer; break 'outer;
} }
devices_changed = true; changed = true;
} }
if devices_changed { if changed {
devices_changed = false; let mut new_devices = build_device_list(&adapter_clone).await;
for d in new_devices
.iter()
.filter(|d| !devices.contains(d) && d.paired_and_trusted())
{
_ = req_tx.send(BluerRequest::ConnectDevice(d.address)).await;
}
devices = mem::take(&mut new_devices);
changed = false;
let _ = tx let _ = tx
.send(BluerSessionEvent::ChangesProcessed(BluerState { .send(BluerSessionEvent::ChangesProcessed(BluerState {
devices: build_device_list(&adapter_clone).await, devices: build_device_list(&adapter_clone).await,