find_characteristic/
find_characteristic.rs1use ble_ledly::capability::color::*;
2use ble_ledly::capability::light::*;
3use ble_ledly::communication_protocol::GenericRGB;
4use ble_ledly::Controller;
5use ble_ledly::device::{LedDevice, OpKind};
6use ble_ledly::device::Device;
7
8use std::error::Error;
9use std::time::Duration;
10use tokio::time;
11
12#[tokio::main]
13async fn main() -> Result<(), Box<dyn Error>> {
14 let mut controller = Controller::<LedDevice>::new().await?;
16
17 let led_devices = controller.device_discovery().await?;
19
20 for device in led_devices.iter() {
22 println!("Found device: {}", device);
23 }
24 let lights: Vec<LedDevice> = led_devices
26 .into_iter()
27 .filter(|device| device.name.contains("QHM-"))
28 .collect();
29
30 controller.connect_with_devices(lights).await?;
32
33 let protocol = GenericRGB::default();
35
36 let connected_lights = controller.list();
38 for light in connected_lights.iter_mut() {
39 println!("--- Found characteristics for device {}: ---", light);
40
41 for characteristic in light.characteristics().unwrap().iter() {
43 println!(
44 "\tUuid: {:?}, Type: {:?}",
45 characteristic.uuid, characteristic.properties
46 );
47 }
48
49 println!("--- Filtered characteristics for device {}: ---", light);
50
51 let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
53
54 for characteristic in light
55 .characteristics_by_type(char_kind_filter)
56 .unwrap()
57 .iter()
58 {
59 println!(
60 "\tUuid: {:?}, Type: {:?}",
61 characteristic.uuid, characteristic.properties
62 );
63 }
64
65 let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67 println!("\nChosen {:?}\n", chosen.get(0));
68
69 light.set_write_char(&chosen.get(0).unwrap());
74
75 println!("Turning light on...");
81 light.turn_on(&protocol).await?;
82
83 println!("Setting color...");
85 light.color(&protocol, 255, 0, 0).await?;
86 time::sleep(Duration::from_millis(800)).await;
87 light.color(&protocol, 0, 255, 0).await?;
88 time::sleep(Duration::from_millis(800)).await;
89 light.color(&protocol, 0, 0, 255).await?;
90 time::sleep(Duration::from_millis(800)).await;
91
92 println!("Turning light off...");
93 light.turn_off(&protocol).await?;
94 }
95
96 Ok(())
97}