()
| 14 | |
| 15 | #[tokio::main] |
| 16 | async fn main() -> anyhow::Result<()> { |
| 17 | pretty_env_logger::init(); |
| 18 | |
| 19 | let manager = Manager::new().await?; |
| 20 | |
| 21 | // get the first bluetooth adapter |
| 22 | // connect to the adapter |
| 23 | let central = get_central(&manager).await; |
| 24 | |
| 25 | let central_state = central.adapter_state().await.unwrap(); |
| 26 | println!("CentralState: {:?}", central_state); |
| 27 | |
| 28 | // Each adapter has an event stream, we fetch via events(), |
| 29 | // simplifying the type, this will return what is essentially a |
| 30 | // Future<Result<Stream<Item=CentralEvent>>>. |
| 31 | let mut events = central.events().await?; |
| 32 | |
| 33 | // start scanning for devices |
| 34 | central.start_scan(ScanFilter::default()).await?; |
| 35 | |
| 36 | // Print based on whatever the event receiver outputs. Note that the event |
| 37 | // receiver blocks, so in a real program, this should be run in its own |
| 38 | // thread (not task, as this library does not yet use async channels). |
| 39 | while let Some(event) = events.next().await { |
| 40 | match event { |
| 41 | CentralEvent::DeviceDiscovered(id) => { |
| 42 | let peripheral = central.peripheral(&id).await?; |
| 43 | let properties = peripheral.properties().await?; |
| 44 | let name = properties |
| 45 | .and_then(|p| p.local_name) |
| 46 | .map(|local_name| format!("Name: {local_name}")) |
| 47 | .unwrap_or_default(); |
| 48 | println!("DeviceDiscovered: {:?} {}", id, name); |
| 49 | } |
| 50 | CentralEvent::StateUpdate(state) => { |
| 51 | println!("AdapterStatusUpdate {:?}", state); |
| 52 | } |
| 53 | CentralEvent::DeviceConnected(id) => { |
| 54 | println!("DeviceConnected: {:?}", id); |
| 55 | } |
| 56 | CentralEvent::DeviceDisconnected(id) => { |
| 57 | println!("DeviceDisconnected: {:?}", id); |
| 58 | } |
| 59 | CentralEvent::ManufacturerDataAdvertisement { |
| 60 | id, |
| 61 | manufacturer_data, |
| 62 | } => { |
| 63 | println!( |
| 64 | "ManufacturerDataAdvertisement: {:?}, {:?}", |
| 65 | id, manufacturer_data |
| 66 | ); |
| 67 | } |
| 68 | CentralEvent::ServiceDataAdvertisement { id, service_data } => { |
| 69 | println!("ServiceDataAdvertisement: {:?}, {:?}", id, service_data); |
| 70 | } |
| 71 | CentralEvent::ServicesAdvertisement { id, services } => { |
| 72 | let services: Vec<String> = |
| 73 | services.into_iter().map(|s| s.to_short_string()).collect(); |
nothing calls this directly
no test coverage detected