find_characteristic/
find_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, 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    // Create a new Light controller
15    let mut controller = Controller::<LedDevice>::new().await?;
16
17    // Discover devices (scan)
18    let led_devices = controller.device_discovery().await?;
19
20    // inspect all found devices
21    for device in led_devices.iter() {
22        println!("Found device: {}", device);
23    }
24    // filter devices
25    let lights: Vec<LedDevice> = led_devices
26        .into_iter()
27        .filter(|device| device.name.contains("QHM-"))
28        .collect();
29
30    // Connect
31    controller.connect_with_devices(lights).await?;
32
33    // Choose your communication protocol
34    let protocol = GenericRGB::default();
35
36    // list all connected devices
37    let connected_lights = controller.list();
38    for light in connected_lights.iter_mut() {
39        println!("--- Found characteristics for device {}: ---", light);
40
41        // inspect all characteristic for every device
42        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        // otherwise inspect all characteristic by supported operation kind
52        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        // choose the characteristic to use to write to the device
66        let chosen = light.characteristics_by_type(char_kind_filter).unwrap();
67        println!("\nChosen {:?}\n", chosen.get(0));
68
69        // set it as a write_char for the current device
70        // you can set different write characteristics for different
71        // devices, as one controller support devices with different communication
72        // protocols
73        light.set_write_char(&chosen.get(0).unwrap());
74
75        /////////////////////////////////
76        // Control the lights as usual //
77        /////////////////////////////////
78
79        // Control the lights
80        println!("Turning light on...");
81        light.turn_on(&protocol).await?;
82
83        // Set color
84        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}