Skip to content

Commit 523cc18

Browse files
committed
feat: winrtble adapter_info and StateUpdate implementation
1 parent 97aed8b commit 523cc18

File tree

2 files changed

+33
-4
lines changed

2 files changed

+33
-4
lines changed

src/winrtble/adapter.rs

+32-3
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,48 @@ use std::convert::TryInto;
2323
use std::fmt::{self, Debug, Formatter};
2424
use std::pin::Pin;
2525
use std::sync::{Arc, Mutex};
26+
use windows::{
27+
Devices::Radios::{Radio, RadioState},
28+
Foundation::TypedEventHandler,
29+
};
2630

2731
/// Implementation of [api::Central](crate::api::Central).
2832
#[derive(Clone)]
2933
pub struct Adapter {
3034
watcher: Arc<Mutex<BLEWatcher>>,
3135
manager: Arc<AdapterManager<Peripheral>>,
36+
radio: Radio,
37+
}
38+
39+
// https://github.com/microsoft/windows-rs/blob/master/crates/libs/windows/src/Windows/Devices/Radios/mod.rs
40+
fn get_central_state(radio: Radio) -> CentralState {
41+
let state = radio.State().unwrap_or(RadioState::Unknown);
42+
match state {
43+
RadioState::On => CentralState::PoweredOn,
44+
RadioState::Off => CentralState::PoweredOff,
45+
_ => CentralState::Unknown,
46+
}
3247
}
3348

3449
impl Adapter {
35-
pub(crate) fn new() -> Self {
50+
pub(crate) fn new(radio: Radio) -> Self {
3651
let watcher = Arc::new(Mutex::new(BLEWatcher::new()));
3752
let manager = Arc::new(AdapterManager::default());
38-
Adapter { watcher, manager }
53+
54+
let radio_clone = radio.clone();
55+
let manager_clone = manager.clone();
56+
let handler = TypedEventHandler::new(move |_sender, _args| {
57+
let state = get_central_state(radio_clone.clone());
58+
manager_clone.emit(CentralEvent::StateUpdate(state.into()));
59+
Ok(())
60+
});
61+
radio.StateChanged(&handler);
62+
63+
Adapter {
64+
watcher,
65+
manager,
66+
radio,
67+
}
3968
}
4069
}
4170

@@ -102,6 +131,6 @@ impl Central for Adapter {
102131
}
103132

104133
async fn adapter_state(&self) -> Result<CentralState> {
105-
Ok(CentralState::Unknown)
134+
Ok(get_central_state(self.radio.clone()))
106135
}
107136
}

src/winrtble/manager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ impl api::Manager for Manager {
3535
Ok(radios
3636
.into_iter()
3737
.filter(|radio| radio.Kind() == Ok(RadioKind::Bluetooth))
38-
.map(|_| Adapter::new())
38+
.map(|radio| Adapter::new(radio.clone()))
3939
.collect())
4040
}
4141
}

0 commit comments

Comments
 (0)