-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathpower_on.rs
More file actions
61 lines (54 loc) · 2.13 KB
/
power_on.rs
File metadata and controls
61 lines (54 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! Basic example: PowerOn the Bluetooth device using freedesktop-bluez-client
use freedesktop_bluez_client::errors::BluezError;
use freedesktop_bluez_client::handler::{BluezRequest, BluezClient};
use tokio::sync::{mpsc, oneshot};
#[tokio::main]
async fn main() -> anyhow::Result<()> {
// Create a channel for sending Bluetooth requests
let (bt_tx, bt_rx) = mpsc::channel(10);
// Spawn the Bluetooth handler in a background task
let _handler = tokio::spawn(async move {
let mut bt_handler = BluezClient::new().await.unwrap();
// Run the handler event loop
let _ = bt_handler.run(bt_rx).await;
});
println!("handler spawned");
let (res_tx, res_rx) = oneshot::channel();
// Example: Enable Bluetooth
let request = BluezRequest::SetPoweredOn { reply_to: res_tx };
bt_tx
.try_send(request)
.expect("Failed to send Bluetooth request");
println!("Bluetooth enable request sent");
if let Ok(result) = res_rx.await {
match result {
Ok(_) => println!("bluetooth powered on"),
Err(e) => match e {
BluezError::Generic => {
println!("Generic error occurred");
}
BluezError::ProxyError(err) => {
println!("Proxy error occurred: {:?}", err);
}
BluezError::InitBusError(_) => {
println!("Failed to initialize system bus");
}
BluezError::CreateBluezProxyError(_) => {
println!("Failed to create Bluez proxy");
}
BluezError::CreateAdapterProxyError(_) => {
println!("Failed to create adapter proxy");
}
_ => {
println!("An unexpected error occurred: {:?}", e);
}
},
}
} else {
eprintln!("Did not receive a response for power on request");
}
// (Optional) gracefully shut down or send more requests...
// Wait for the handler to finish (in real code, you'd keep the handler running)
// handler.await?;
Ok(())
}