MCPcopy Create free account
hub / github.com/deviceplug/btleplug / main

Function main

examples/lights.rs:32–86  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

30
31#[tokio::main]
32async fn main() -> anyhow::Result<()> {
33 pretty_env_logger::init();
34
35 let manager = Manager::new().await.unwrap();
36
37 // get the first bluetooth adapter
38 let central = manager
39 .adapters()
40 .await
41 .expect("Unable to fetch adapter list.")
42 .into_iter()
43 .nth(0)
44 .expect("Unable to find adapters.");
45
46 // start scanning for devices
47 central.start_scan(ScanFilter::default()).await?;
48 // instead of waiting, you can use central.events() to get a stream which will
49 // notify you of new devices, for an example of that see examples/event_driven_discovery.rs
50 time::sleep(Duration::from_secs(2)).await;
51
52 // find the device we're interested in
53 let light = find_light(&central).await.expect("No lights found");
54
55 // connect to the device
56 light.connect().await?;
57
58 // discover services and characteristics
59 light.discover_services().await?;
60
61 // find the characteristic we want
62 let chars = light.characteristics();
63 let cmd_char = chars
64 .iter()
65 .find(|c| c.uuid == LIGHT_CHARACTERISTIC_UUID)
66 .expect("Unable to find characterics");
67
68 // dance party
69 let mut rng = rng();
70 for _ in 0..20 {
71 let color_cmd = vec![
72 0x56,
73 rng.random(),
74 rng.random(),
75 rng.random(),
76 0x00,
77 0xF0,
78 0xAA,
79 ];
80 light
81 .write(&cmd_char, &color_cmd, WriteType::WithoutResponse)
82 .await?;
83 time::sleep(Duration::from_millis(200)).await;
84 }
85 Ok(())
86}

Callers

nothing calls this directly

Calls 9

find_lightFunction · 0.85
iterMethod · 0.80
initFunction · 0.50
adaptersMethod · 0.45
start_scanMethod · 0.45
connectMethod · 0.45
discover_servicesMethod · 0.45
characteristicsMethod · 0.45
writeMethod · 0.45

Tested by

no test coverage detected