use bleasy::{Error, ScanConfig, Scanner};
use futures::StreamExt;
use std::sync::atomic::{AtomicU32, Ordering};
use std::sync::Arc;
use tokio::time::{sleep, Duration};
#[tokio::main]
async fn main() -> Result<(), Error> {
pretty_env_logger::init();
let mut scanner = Scanner::new();
scanner.start(ScanConfig::default()).await?;
let mut device_stream = scanner.device_stream();
let count = Arc::new(AtomicU32::new(0));
let join_handle = {
let count = count.clone();
tokio::spawn(async move {
while let Some(device) = device_stream.next().await {
println!("Found device with name {:?}", device.local_name().await);
count.fetch_add(1, Ordering::SeqCst);
}
})
};
while count.load(Ordering::SeqCst) < 2 {
sleep(Duration::from_millis(100)).await;
}
scanner.stop().await?;
join_handle.await.unwrap();
Ok(())
}