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

Function main

examples/subscribe_notify_characteristic.rs:17–99  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

15
16#[tokio::main]
17async fn main() -> anyhow::Result<()> {
18 pretty_env_logger::init();
19
20 let manager = Manager::new().await?;
21 let adapter_list = manager.adapters().await?;
22 if adapter_list.is_empty() {
23 eprintln!("No Bluetooth adapters found");
24 }
25
26 for adapter in adapter_list.iter() {
27 println!("Starting scan...");
28 adapter
29 .start_scan(ScanFilter::default())
30 .await
31 .expect("Can't scan BLE adapter for connected devices...");
32 time::sleep(Duration::from_secs(2)).await;
33 let peripherals = adapter.peripherals().await?;
34
35 if peripherals.is_empty() {
36 eprintln!("->>> BLE peripheral devices were not found, sorry. Exiting...");
37 } else {
38 // All peripheral devices in range.
39 for peripheral in peripherals.iter() {
40 let properties = peripheral.properties().await?;
41 let is_connected = peripheral.is_connected().await?;
42 let local_name = properties
43 .unwrap()
44 .local_name
45 .unwrap_or(String::from("(peripheral name unknown)"));
46 println!(
47 "Peripheral {:?} is connected: {:?}",
48 &local_name, is_connected
49 );
50 // Check if it's the peripheral we want.
51 if local_name.contains(PERIPHERAL_NAME_MATCH_FILTER) {
52 println!("Found matching peripheral {:?}...", &local_name);
53 if !is_connected {
54 // Connect if we aren't already connected.
55 if let Err(err) = peripheral.connect().await {
56 eprintln!("Error connecting to peripheral, skipping: {}", err);
57 continue;
58 }
59 }
60 let is_connected = peripheral.is_connected().await?;
61 println!(
62 "Now connected ({:?}) to peripheral {:?}.",
63 is_connected, &local_name
64 );
65 if is_connected {
66 println!("Discover peripheral {:?} services...", local_name);
67 peripheral.discover_services().await?;
68 for characteristic in peripheral.characteristics() {
69 println!("Checking characteristic {:?}", characteristic);
70 // Subscribe to notifications from the characteristic with the selected
71 // UUID.
72 if characteristic.uuid == NOTIFY_CHARACTERISTIC_UUID
73 && characteristic.properties.contains(CharPropFlags::NOTIFY)
74 {

Callers

nothing calls this directly

Calls 15

iterMethod · 0.80
takeMethod · 0.80
nextMethod · 0.80
initFunction · 0.50
adaptersMethod · 0.45
start_scanMethod · 0.45
peripheralsMethod · 0.45
propertiesMethod · 0.45
is_connectedMethod · 0.45
connectMethod · 0.45
discover_servicesMethod · 0.45
characteristicsMethod · 0.45

Tested by

no test coverage detected