Skip to content

Commit

Permalink
feat: bluez adapter_info and StateUpdate implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
szymonlesisz committed Feb 19, 2024
1 parent d8f6560 commit 26af262
Showing 1 changed file with 24 additions and 2 deletions.
26 changes: 24 additions & 2 deletions src/bluez/adapter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use crate::{Error, Result};
use async_trait::async_trait;
use bluez_async::{
AdapterId, BluetoothError, BluetoothEvent, BluetoothSession, DeviceEvent, DiscoveryFilter,
AdapterEvent,
Transport,
};
use futures::stream::{self, Stream, StreamExt};
Expand All @@ -22,6 +23,13 @@ impl Adapter {
}
}

fn get_central_state(state: bool) -> CentralState {
match state {
true => CentralState::PoweredOn,
false => CentralState::PoweredOff,
}
}

#[async_trait]
impl Central for Adapter {
type Peripheral = Peripheral;
Expand Down Expand Up @@ -51,7 +59,7 @@ impl Central for Adapter {
let session = self.session.clone();
let adapter_id = self.adapter.clone();
let events = events
.filter_map(move |event| central_event(event, session.clone(), adapter_id.clone()));
.filter_map(move |event| central_event(event, session.clone(), adapter_id.clone()));

Ok(Box::pin(initial_events.chain(events)))
}
Expand Down Expand Up @@ -107,7 +115,11 @@ impl Central for Adapter {
}

async fn adapter_state(&self) -> Result<CentralState> {
Ok(CentralState::Unknown)
let mut powered = false;
if let Ok(info) = self.session.get_adapter_info(&self.adapter).await {
powered = info.powered;
}
Ok(get_central_state(powered))
}
}

Expand Down Expand Up @@ -166,6 +178,16 @@ async fn central_event(
}
_ => None,
},
BluetoothEvent::Adapter {
id,
event: adapter_event,
} if id == adapter_id => match adapter_event {
AdapterEvent::Powered { powered } => {
let state = get_central_state(powered);
Some(CentralEvent::StateUpdate(state.into()))
},
_ => None,
},
_ => None,
}
}

0 comments on commit 26af262

Please sign in to comment.