known_characteristic/
known_characteristic.rs

1use ble_ledly::capability::color::*;
2use ble_ledly::capability::light::*;
3use ble_ledly::communication_protocol::GenericRGB;
4use ble_ledly::Controller;
5use ble_ledly::device::LedDevice;
6use ble_ledly::device::Device;
7use ble_ledly::device::{CharKind, UuidKind};
8
9use std::error::Error;
10use std::time::Duration;
11use tokio::time;
12
13#[tokio::main]
14async fn main() -> Result<(), Box<dyn Error>> {
15    // Create a new Light controller
16    let mut controller = Controller::<LedDevice>::new().await?;
17
18    // Discover devices (scan)
19    let led_devices = controller.device_discovery().await?;
20
21    // inspect all found devices
22    for device in led_devices.iter() {
23        println!("Found device: {}", device);
24    }
25    // filter devices
26    let lights: Vec<LedDevice> = led_devices
27        .into_iter()
28        .filter(|device| device.name.contains("QHM-"))
29        .collect();
30
31    // Connect
32    controller.connect_with_devices(lights).await?;
33
34    // Choose your communication protocol
35    let protocol = GenericRGB::default();
36
37    // list all connected devices
38    let connected_lights = controller.list();
39    for light in connected_lights.iter_mut() {
40        println!("-- Manipulating light {} --", light);
41
42        // set the write_char for the current device
43        // you can set different write characteristics for different
44        // devices, as one controller support devices with different communication
45        // protocols
46        // Set it with an Uuid, an u32, or u16
47        light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;
48
49        /////////////////////////////////
50        // Control the lights as usual //
51        /////////////////////////////////
52
53        // Control the lights
54        println!("Turning light on...");
55        light.turn_on(&protocol).await?;
56
57        // Set color
58        println!("Setting color...");
59        light.color(&protocol, 255, 0, 0).await?;
60        time::sleep(Duration::from_millis(800)).await;
61        light.color(&protocol, 0, 255, 0).await?;
62        time::sleep(Duration::from_millis(800)).await;
63        light.color(&protocol, 0, 0, 255).await?;
64        time::sleep(Duration::from_millis(800)).await;
65
66        println!("Turning light off...");
67        light.turn_off(&protocol).await?;
68    }
69
70    Ok(())
71}