use futures_executor::block_on;
use std::collections::HashMap;
use std::time::Duration;
use webbluetooth::prelude::*;
use webbluetooth::{
Bluetooth, Candidates, DeviceChooser, DeviceFilter, Error, RequestDeviceOptions,
};
const WINDOW: Duration = Duration::from_secs(8);
struct Printer;
impl DeviceChooser for Printer {
fn choose(&self, mut candidates: Candidates) -> BoxFuture<'static, Option<String>> {
Box::pin(async move {
let mut seen: HashMap<String, i32> = HashMap::new();
let listen = async {
while let Some(c) = candidates.next().await {
let rssi = c.rssi().unwrap_or(i32::MIN);
let is_new = match seen.get(&c.id) {
None => true,
Some(best) => rssi > best + 8,
};
if !is_new {
continue;
}
seen.insert(c.id.clone(), rssi);
let services = c.advertisement.service_uuids.len();
println!(
" {:>4} {:<30} {}{}",
c.rssi()
.map(|d| format!("{d}"))
.unwrap_or_else(|| "—".into()),
c.label(),
c.id,
if services > 0 {
format!(" ({services} services)")
} else {
String::new()
}
);
for uuid in &c.advertisement.service_uuids {
println!(" service {uuid}");
}
for (company, data) in &c.advertisement.manufacturer_data {
println!(
" manufacturer 0x{company:04x}: {} bytes",
data.len()
);
}
}
};
let _ = webbluetooth::timeout(WINDOW, listen).await;
println!("\n{} device(s) seen.", seen.len());
None })
}
}
fn main() {
let bluetooth = Bluetooth::new();
if let Err(why) = block_on(bluetooth.availability()) {
eprintln!("Bluetooth unavailable: {why}");
eprintln!("Run `cargo run -p webbluetooth --example doctor` for the details.");
std::process::exit(1);
}
println!(" dBm name id");
println!(" ---- ----------------------------- --------------------------------");
let options = RequestDeviceOptions::new().accept_all_devices();
match block_on(bluetooth.request_device_with(options, &Printer)) {
Err(Error::NotFound(_)) => {}
Err(e) => eprintln!("scan failed: {e}"),
Ok(device) => println!("unexpectedly granted {}", device.id()),
}
let _unused = DeviceFilter::new().name_prefix("Polar");
}