Skip to content

Commit 97aed8b

Browse files
committed
feat: bluez adapter_info and StateUpdate implementation
1 parent f2b2b7c commit 97aed8b

File tree

1 file changed

+24
-3
lines changed

1 file changed

+24
-3
lines changed

src/bluez/adapter.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ use crate::api::{Central, CentralEvent, CentralState, ScanFilter};
33
use crate::{Error, Result};
44
use async_trait::async_trait;
55
use bluez_async::{
6-
AdapterId, BluetoothError, BluetoothEvent, BluetoothSession, DeviceEvent, DiscoveryFilter,
7-
Transport,
6+
AdapterEvent, AdapterId, BluetoothError, BluetoothEvent, BluetoothSession, DeviceEvent,
7+
DiscoveryFilter, Transport,
88
};
99
use futures::stream::{self, Stream, StreamExt};
1010
use std::pin::Pin;
@@ -22,6 +22,13 @@ impl Adapter {
2222
}
2323
}
2424

25+
fn get_central_state(powered: bool) -> CentralState {
26+
match powered {
27+
true => CentralState::PoweredOn,
28+
false => CentralState::PoweredOff,
29+
}
30+
}
31+
2532
#[async_trait]
2633
impl Central for Adapter {
2734
type Peripheral = Peripheral;
@@ -107,7 +114,11 @@ impl Central for Adapter {
107114
}
108115

109116
async fn adapter_state(&self) -> Result<CentralState> {
110-
Ok(CentralState::Unknown)
117+
let mut powered = false;
118+
if let Ok(info) = self.session.get_adapter_info(&self.adapter).await {
119+
powered = info.powered;
120+
}
121+
Ok(get_central_state(powered))
111122
}
112123
}
113124

@@ -166,6 +177,16 @@ async fn central_event(
166177
}
167178
_ => None,
168179
},
180+
BluetoothEvent::Adapter {
181+
id,
182+
event: adapter_event,
183+
} if id == adapter_id => match adapter_event {
184+
AdapterEvent::Powered { powered } => {
185+
let state = get_central_state(powered);
186+
Some(CentralEvent::StateUpdate(state))
187+
}
188+
_ => None,
189+
},
169190
_ => None,
170191
}
171192
}

0 commit comments

Comments
 (0)