pub trait Device: Display {
Show 18 methods
// Required methods
fn new(
name: &str,
alias: &str,
peripheral: Option<Peripheral>,
write_char: Option<Characteristic>,
read_char: Option<Characteristic>,
) -> Self;
fn alias(&self) -> &str;
fn name(&self) -> &str;
fn address(&self) -> Option<String>;
fn peripheral(&self) -> Option<&Peripheral>;
fn write_char(&self) -> Option<&Characteristic>;
fn read_char(&self) -> Option<&Characteristic>;
fn default_write_characteristic_uuid(&self) -> Uuid;
fn set_alias(&mut self, alias: &str);
fn set_name(&mut self, name: &str);
fn set_peripheral(&mut self, peripheral: Peripheral);
fn set_write_char(&mut self, characteristic: &Characteristic);
// Provided methods
fn characteristics(&self) -> Option<Vec<Characteristic>> { ... }
fn characteristics_by_type(
&self,
kinds: BitFlags<OpKind>,
) -> Option<Vec<Characteristic>> { ... }
fn set_char(
&mut self,
char_kind: &CharKind,
uuid_kind: &UuidKind,
) -> Result<(), BluetoothError> { ... }
fn set_char_with_uuid(
&mut self,
char_kind: &CharKind,
uuid: &Uuid,
) -> Result<(), BluetoothError> { ... }
fn set_char_with_u16(
&mut self,
char_kind: &CharKind,
u16: u16,
) -> Result<(), BluetoothError> { ... }
fn set_char_with_u32(
&mut self,
char_kind: &CharKind,
u32: u32,
) -> Result<(), BluetoothError> { ... }
}Required Methods§
fn new( name: &str, alias: &str, peripheral: Option<Peripheral>, write_char: Option<Characteristic>, read_char: Option<Characteristic>, ) -> Self
fn alias(&self) -> &str
fn name(&self) -> &str
fn address(&self) -> Option<String>
fn peripheral(&self) -> Option<&Peripheral>
fn write_char(&self) -> Option<&Characteristic>
fn read_char(&self) -> Option<&Characteristic>
fn default_write_characteristic_uuid(&self) -> Uuid
fn set_alias(&mut self, alias: &str)
fn set_name(&mut self, name: &str)
fn set_peripheral(&mut self, peripheral: Peripheral)
Sourcefn set_write_char(&mut self, characteristic: &Characteristic)
fn set_write_char(&mut self, characteristic: &Characteristic)
Provided Methods§
Sourcefn characteristics(&self) -> Option<Vec<Characteristic>>
fn characteristics(&self) -> Option<Vec<Characteristic>>
Return all the discovered device characteristic.
§Examples
ⓘ
for characteristic in light.characteristics().unwrap().iter() {
println!(
"\tUuid: {:?}, Type: {:?}",
characteristic.uuid, characteristic.properties
);
}Examples found in repository?
examples/find_characteristic.rs (line 42)
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}Sourcefn characteristics_by_type(
&self,
kinds: BitFlags<OpKind>,
) -> Option<Vec<Characteristic>>
fn characteristics_by_type( &self, kinds: BitFlags<OpKind>, ) -> Option<Vec<Characteristic>>
Return all the discovered device characteristic and allows to filter them by type
§Examples
ⓘ
let char_kind_filter = OpKind::Write | OpKind::WriteWithoutResponse;
for characteristic in light
.characteristics_by_type(char_kind_filter)
.unwrap()
.iter()
{
println!(
"\tUuid: {:?}, Type: {:?}",
characteristic.uuid, characteristic.properties
);
}Examples found in repository?
examples/find_characteristic.rs (line 55)
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}Sourcefn set_char(
&mut self,
char_kind: &CharKind,
uuid_kind: &UuidKind,
) -> Result<(), BluetoothError>
fn set_char( &mut self, char_kind: &CharKind, uuid_kind: &UuidKind, ) -> Result<(), BluetoothError>
Allows to set the default characteristic (Write or Read),
per-device by providing the UuidKind of the characteristic.
§Examples
ⓘ
// Set it with an Uuid, an u32, or u16
light.set_char(&CharKind::Write, &UuidKind::Uuid16(0xFFD9))?;Examples found in repository?
examples/known_characteristic.rs (line 47)
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}fn set_char_with_uuid( &mut self, char_kind: &CharKind, uuid: &Uuid, ) -> Result<(), BluetoothError>
fn set_char_with_u16( &mut self, char_kind: &CharKind, u16: u16, ) -> Result<(), BluetoothError>
fn set_char_with_u32( &mut self, char_kind: &CharKind, u32: u32, ) -> Result<(), BluetoothError>
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.