example(bluez): fix bluetoothctl example
This commit is contained in:
parent
5630ebe694
commit
01ee80cd97
1 changed files with 35 additions and 29 deletions
|
|
@ -1,12 +1,10 @@
|
||||||
use std::process::ExitCode;
|
use std::process::ExitCode;
|
||||||
|
|
||||||
use futures::StreamExt;
|
|
||||||
|
|
||||||
#[tokio::main]
|
#[tokio::main]
|
||||||
async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
let connection = zbus::Connection::system().await?;
|
let connection = zbus::Connection::system().await?;
|
||||||
|
|
||||||
let adapters = bluez_zbus::adapters(&connection).await?;
|
let adapters = bluez_zbus::get_adapters(&connection).await?;
|
||||||
|
|
||||||
// if adapters.is_empty() {
|
// if adapters.is_empty() {
|
||||||
// eprintln!("No adapter found");
|
// eprintln!("No adapter found");
|
||||||
|
|
@ -27,7 +25,7 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
for proxy in devices {
|
for (_path, proxy) in devices {
|
||||||
if !proxy.device.connected().await? {
|
if !proxy.device.connected().await? {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -55,7 +53,7 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
for proxy in devices {
|
for (_path, proxy) in devices {
|
||||||
if !proxy.device.paired().await? {
|
if !proxy.device.paired().await? {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -78,15 +76,19 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
},
|
},
|
||||||
|
|
||||||
Some("nearby-devices") => {
|
Some("nearby-devices") => {
|
||||||
futures::future::join_all(adapters.iter().map(|adapter| adapter.start_discovery()))
|
futures_util::future::join_all(
|
||||||
.await;
|
adapters
|
||||||
|
.iter()
|
||||||
|
.map(|(_path, adapter)| adapter.start_discovery()),
|
||||||
|
)
|
||||||
|
.await;
|
||||||
match bluez_zbus::get_devices(&connection, None).await {
|
match bluez_zbus::get_devices(&connection, None).await {
|
||||||
Err(why) => {
|
Err(why) => {
|
||||||
eprintln!("error: could not get devices: {why}");
|
eprintln!("error: could not get devices: {why}");
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
for proxy in devices {
|
for (_path, proxy) in devices {
|
||||||
if proxy.device.paired().await? {
|
if proxy.device.paired().await? {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
@ -116,18 +118,19 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
let devices: Vec<bluez_zbus::BluetoothDevice> =
|
let devices: Vec<bluez_zbus::BluetoothDevice> = futures_util::future::join_all(
|
||||||
futures::future::join_all(devices.into_iter().map(|proxy| async {
|
devices.into_iter().map(|(_path, proxy)| async {
|
||||||
match (proxy.device.name().await, proxy.device.address().await) {
|
match (proxy.device.name().await, proxy.device.address().await) {
|
||||||
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
||||||
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}))
|
}),
|
||||||
.await
|
)
|
||||||
.into_iter()
|
.await
|
||||||
.flatten()
|
.into_iter()
|
||||||
.collect();
|
.flatten()
|
||||||
|
.collect();
|
||||||
if devices.is_empty() {
|
if devices.is_empty() {
|
||||||
eprintln!("No device found");
|
eprintln!("No device found");
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
|
|
@ -164,18 +167,20 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
let devices: Vec<bluez_zbus::BluetoothDevice> =
|
let devices: Vec<bluez_zbus::BluetoothDevice> = futures_util::future::join_all(
|
||||||
futures::future::join_all(devices.into_iter().map(|proxy| async {
|
devices.into_iter().map(|(_path, proxy)| async {
|
||||||
match (proxy.device.name().await, proxy.device.address().await) {
|
match (proxy.device.name().await, proxy.device.address().await) {
|
||||||
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
||||||
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}))
|
}),
|
||||||
.await
|
)
|
||||||
.into_iter()
|
.await
|
||||||
.flatten()
|
.into_iter()
|
||||||
.collect();
|
.flatten()
|
||||||
|
.collect();
|
||||||
|
|
||||||
if devices.is_empty() {
|
if devices.is_empty() {
|
||||||
eprintln!("No device found");
|
eprintln!("No device found");
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
|
|
@ -212,18 +217,19 @@ async fn main() -> Result<ExitCode, Box<dyn std::error::Error>> {
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
}
|
}
|
||||||
Ok(devices) => {
|
Ok(devices) => {
|
||||||
let devices: Vec<bluez_zbus::BluetoothDevice> =
|
let devices: Vec<bluez_zbus::BluetoothDevice> = futures_util::future::join_all(
|
||||||
futures::future::join_all(devices.into_iter().map(|proxy| async {
|
devices.into_iter().map(|(_path, proxy)| async {
|
||||||
match (proxy.device.name().await, proxy.device.address().await) {
|
match (proxy.device.name().await, proxy.device.address().await) {
|
||||||
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
(Ok(alias), _) if alias == addr_or_alias => Some(proxy),
|
||||||
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
(_, Ok(addr)) if addr == addr_or_alias => Some(proxy),
|
||||||
_ => None,
|
_ => None,
|
||||||
}
|
}
|
||||||
}))
|
}),
|
||||||
.await
|
)
|
||||||
.into_iter()
|
.await
|
||||||
.flatten()
|
.into_iter()
|
||||||
.collect();
|
.flatten()
|
||||||
|
.collect();
|
||||||
if devices.is_empty() {
|
if devices.is_empty() {
|
||||||
eprintln!("No device found");
|
eprintln!("No device found");
|
||||||
return Ok(ExitCode::FAILURE);
|
return Ok(ExitCode::FAILURE);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue