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
use std::error::Error;

use bluest::Adapter;
use tracing::info;
use tracing::metadata::LevelFilter;

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    use tracing_subscriber::prelude::*;
    use tracing_subscriber::{fmt, EnvFilter};

    tracing_subscriber::registry()
        .with(fmt::layer())
        .with(
            EnvFilter::builder()
                .with_default_directive(LevelFilter::INFO.into())
                .from_env_lossy(),
        )
        .init();

    let adapter = Adapter::default().await.ok_or("Bluetooth adapter not found")?;
    adapter.wait_available().await?;

    info!("getting connected devices");
    let devices = adapter.connected_devices().await?;
    for device in devices {
        info!("found {:?}", device);
        adapter.connect_device(&device).await?;
        let services = device.services().await?;
        for service in services {
            info!("  {:?}", service);
            let characteristics = service.characteristics().await?;
            for characteristic in characteristics {
                info!("    {:?}", characteristic);
                let props = characteristic.properties().await?;
                info!("      props: {:?}", props);
                if props.read {
                    info!("      value: {:?}", characteristic.read().await);
                }
                if props.write_without_response {
                    info!("      max_write_len: {:?}", characteristic.max_write_len());
                }

                let descriptors = characteristic.descriptors().await?;
                for descriptor in descriptors {
                    info!("      {:?}: {:?}", descriptor, descriptor.read().await);
                }
            }
        }
    }
    info!("done");

    Ok(())
}