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

Function main

examples/discover_adapters_peripherals.rs:11–77  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

9
10#[tokio::main]
11async fn main() -> anyhow::Result<()> {
12 pretty_env_logger::init();
13
14 let manager = Manager::new().await?;
15 let adapter_list = manager.adapters().await?;
16 if adapter_list.is_empty() {
17 eprintln!("No Bluetooth adapters found");
18 }
19
20 for adapter in adapter_list.iter() {
21 println!("Starting scan on {}...", adapter.adapter_info().await?);
22 adapter
23 .start_scan(ScanFilter::default())
24 .await
25 .expect("Can't scan BLE adapter for connected devices...");
26 time::sleep(Duration::from_secs(10)).await;
27 let peripherals = adapter.peripherals().await?;
28 if peripherals.is_empty() {
29 eprintln!("->>> BLE peripheral devices were not found, sorry. Exiting...");
30 } else {
31 // All peripheral devices in range
32 for peripheral in peripherals.iter() {
33 let properties = peripheral.properties().await?;
34 let is_connected = peripheral.is_connected().await?;
35 let local_name = properties
36 .unwrap()
37 .local_name
38 .unwrap_or(String::from("(peripheral name unknown)"));
39 println!(
40 "Peripheral {:?} is connected: {:?}",
41 local_name, is_connected
42 );
43 if !is_connected {
44 println!("Connecting to peripheral {:?}...", &local_name);
45 if let Err(err) = peripheral.connect().await {
46 eprintln!("Error connecting to peripheral, skipping: {}", err);
47 continue;
48 }
49 }
50 let is_connected = peripheral.is_connected().await?;
51 println!(
52 "Now connected ({:?}) to peripheral {:?}...",
53 is_connected, &local_name
54 );
55 peripheral.discover_services().await?;
56 println!("Discover peripheral {:?} services...", &local_name);
57 for service in peripheral.services() {
58 println!(
59 "Service UUID {}, primary: {}",
60 service.uuid, service.primary
61 );
62 for characteristic in service.characteristics {
63 println!(" {:?}", characteristic);
64 }
65 }
66 if is_connected {
67 println!("Disconnecting from peripheral {:?}...", &local_name);
68 peripheral

Callers

nothing calls this directly

Calls 11

iterMethod · 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
servicesMethod · 0.45
disconnectMethod · 0.45

Tested by

no test coverage detected