feat(bluetooth): add discoverable & pairable toggles + fix bug where the receiver for bluetooth events is accidentally dropped
This commit is contained in:
parent
c068343c4d
commit
b1a5468547
3 changed files with 48 additions and 30 deletions
|
|
@ -67,10 +67,7 @@ async fn start_listening<I: Copy + Debug>(id: I, state: State) -> (Option<(I, Bl
|
|||
let event = if let Some(event) = session_rx.recv().await {
|
||||
match event {
|
||||
BluerSessionEvent::ChangesProcessed(state) => {
|
||||
return (
|
||||
Some((id, BluerEvent::DevicesChanged { state })),
|
||||
State::Waiting { session_state },
|
||||
);
|
||||
Some((id, BluerEvent::DevicesChanged { state }))
|
||||
}
|
||||
BluerSessionEvent::RequestResponse {
|
||||
req,
|
||||
|
|
@ -90,7 +87,6 @@ async fn start_listening<I: Copy + Debug>(id: I, state: State) -> (Option<(I, Bl
|
|||
} else {
|
||||
return (None, State::Finished);
|
||||
};
|
||||
|
||||
session_state.rx = Some(session_rx);
|
||||
(event, State::Waiting { session_state })
|
||||
}
|
||||
|
|
@ -101,6 +97,8 @@ async fn start_listening<I: Copy + Debug>(id: I, state: State) -> (Option<(I, Bl
|
|||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub enum BluerRequest {
|
||||
SetBluetoothEnabled(bool),
|
||||
SetPairable(bool),
|
||||
SetDiscoverable(bool),
|
||||
PairDevice(Address),
|
||||
ConnectDevice(Address),
|
||||
DisconnectDevice(Address),
|
||||
|
|
@ -130,6 +128,8 @@ pub enum BluerEvent {
|
|||
pub struct BluerState {
|
||||
pub devices: Vec<BluerDevice>,
|
||||
pub bluetooth_enabled: bool,
|
||||
pub discoverable: bool,
|
||||
pub pairable: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
|
|
@ -178,6 +178,7 @@ impl BluerDevice {
|
|||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum BluerSessionEvent {
|
||||
RequestResponse {
|
||||
req: BluerRequest,
|
||||
|
|
@ -334,7 +335,6 @@ impl BluerSessionState {
|
|||
))
|
||||
.await;
|
||||
let res = rx.recv().await;
|
||||
dbg!(res);
|
||||
match res {
|
||||
Some(res) if res => Ok(()),
|
||||
_ => Err(bluer::agent::ReqError::Rejected),
|
||||
|
|
@ -404,8 +404,9 @@ impl BluerSessionState {
|
|||
tx,
|
||||
active_requests: Arc::new(Mutex::new(HashMap::new())),
|
||||
};
|
||||
self_.process_changes();
|
||||
self_.process_requests(request_rx);
|
||||
self_.process_changes();
|
||||
|
||||
Ok(self_)
|
||||
}
|
||||
|
||||
|
|
@ -433,7 +434,15 @@ impl BluerSessionState {
|
|||
let _ = tx
|
||||
.send(BluerSessionEvent::ChangesProcessed(BluerState {
|
||||
devices: build_device_list(&adapter_clone).await,
|
||||
bluetooth_enabled: true,
|
||||
bluetooth_enabled: adapter_clone
|
||||
.is_powered()
|
||||
.await
|
||||
.unwrap_or_default(),
|
||||
discoverable: adapter_clone
|
||||
.is_discoverable()
|
||||
.await
|
||||
.unwrap_or_default(),
|
||||
pairable: adapter_clone.is_pairable().await.unwrap_or_default(),
|
||||
}))
|
||||
.await;
|
||||
// reset timeout
|
||||
|
|
@ -518,11 +527,25 @@ impl BluerSessionState {
|
|||
}
|
||||
}
|
||||
BluerRequest::StateUpdate => {}
|
||||
BluerRequest::SetPairable(enabled) => {
|
||||
let res = adapter_clone.set_pairable(*enabled).await;
|
||||
if let Err(e) = res {
|
||||
err_msg = Some(e.to_string());
|
||||
}
|
||||
}
|
||||
BluerRequest::SetDiscoverable(enabled) => {
|
||||
let res = adapter_clone.set_discoverable(*enabled).await;
|
||||
if let Err(e) = res {
|
||||
err_msg = Some(e.to_string());
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let state = BluerState {
|
||||
devices: build_device_list(&adapter_clone).await,
|
||||
bluetooth_enabled: adapter_clone.is_powered().await.unwrap_or_default(),
|
||||
discoverable: adapter_clone.is_discoverable().await.unwrap_or_default(),
|
||||
pairable: adapter_clone.is_pairable().await.unwrap_or_default(),
|
||||
};
|
||||
|
||||
let _ = tx_clone
|
||||
|
|
@ -550,6 +573,8 @@ impl BluerSessionState {
|
|||
devices: build_device_list(&self.adapter).await,
|
||||
// TODO is this a proper way of checking if bluetooth is enabled?
|
||||
bluetooth_enabled: self.adapter.is_powered().await.unwrap_or_default(),
|
||||
discoverable: self.adapter.is_discoverable().await.unwrap_or_default(),
|
||||
pairable: self.adapter.is_pairable().await.unwrap_or_default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue