1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use File;
use *;
use ParquetReader;
use SerReader;
use ParquetWriter;
use ;
async
// use bleasy::{Error, ScanConfig, Scanner};
// use futures::StreamExt;
// use std::sync::atomic::{AtomicU32, Ordering};
// use std::sync::Arc;
// use tokio::time::{sleep, Duration};
// #[tokio::main]
// async fn main() -> Result<(), Error> {
// pretty_env_logger::init();
// // Create a new BLE device scanner
// let mut scanner = Scanner::new();
// // Start the scanner with default configuration
// scanner.start(ScanConfig::default()).await?;
// // Create a stream that is provided with discovered devices
// let mut device_stream = scanner.device_stream();
// // Create a thread-safe counter
// let count = Arc::new(AtomicU32::new(0));
// // List devices in a separate thread as they are discovered
// let join_handle = {
// let count = count.clone();
// tokio::spawn(async move {
// while let Some(device) = device_stream.next().await {
// println!("Found device with name {:?}", device.local_name().await);
// count.fetch_add(1, Ordering::SeqCst);
// }
// })
// };
// // Wait until at least two devices are found
// while count.load(Ordering::SeqCst) < 2 {
// sleep(Duration::from_millis(100)).await;
// }
// // Stop the scanner after 2 devices are found
// scanner.stop().await?;
// join_handle.await.unwrap();
// Ok(())
// }
// See the "macOS permissions note" in README.md before running this on macOS
// Big Sur or later.
// use btleplug::api::{Central, CharPropFlags, Manager as _, Peripheral, ScanFilter};
// use btleplug::platform::Manager;
// use futures::stream::StreamExt;
// use std::error::Error;
// use std::time::Duration;
// use tokio::time;
// use uuid::Uuid;
// /// Only devices whose name contains this string will be tried.
// const PERIPHERAL_NAME_MATCH_FILTER: &str = "2301B";
// /// UUID of the characteristic for which we should subscribe to notifications.
// const NOTIFY_CHARACTERISTIC_UUID: Uuid = Uuid::from_u128(0x6e400002_b534_f393_67a9_e50e24dccA9e);
// #[tokio::main]
// async fn main() -> Result<(), Box<dyn Error>> {
// pretty_env_logger::init();
// let manager = Manager::new().await?;
// let adapter_list = manager.adapters().await?;
// if adapter_list.is_empty() {
// eprintln!("No Bluetooth adapters found");
// }
// for adapter in adapter_list.iter() {
// println!("Starting scan...");
// adapter
// .start_scan(ScanFilter::default())
// .await
// .expect("Can't scan BLE adapter for connected devices...");
// time::sleep(Duration::from_secs(2)).await;
// let peripherals = adapter.peripherals().await?;
// if peripherals.is_empty() {
// eprintln!("->>> BLE peripheral devices were not found, sorry. Exiting...");
// } else {
// // All peripheral devices in range.
// for peripheral in peripherals.iter() {
// let properties = peripheral.properties().await?;
// let is_connected = peripheral.is_connected().await?;
// let local_name = properties
// .unwrap()
// .local_name
// .unwrap_or(String::from("(peripheral name unknown)"));
// println!(
// "Peripheral {:?} is connected: {:?}",
// &local_name, is_connected
// );
// // Check if it's the peripheral we want.
// if local_name.contains(PERIPHERAL_NAME_MATCH_FILTER) {
// println!("Found matching peripheral {:?}...", &local_name);
// if !is_connected {
// // Connect if we aren't already connected.
// if let Err(err) = peripheral.connect().await {
// eprintln!("Error connecting to peripheral, skipping: {}", err);
// continue;
// }
// }
// let is_connected = peripheral.is_connected().await?;
// println!(
// "Now connected ({:?}) to peripheral {:?}.",
// is_connected, &local_name
// );
// if is_connected {
// println!("Discover peripheral {:?} services...", local_name);
// peripheral.discover_services().await?;
// for characteristic in peripheral.characteristics() {
// println!("Checking characteristic {:?}", characteristic);
// // Subscribe to notifications from the characteristic with the selected
// // UUID.
// if characteristic.uuid == NOTIFY_CHARACTERISTIC_UUID
// && characteristic.properties.contains(CharPropFlags::NOTIFY)
// {
// println!("Subscribing to characteristic {:?}", characteristic.uuid);
// peripheral.subscribe(&characteristic).await?;
// // Print the first 4 notifications received.
// let mut notification_stream =
// peripheral.notifications().await?.take(4);
// // Process while the BLE connection is not broken or stopped.
// while let Some(data) = notification_stream.next().await {
// println!(
// "Received data from {:?} [{:?}]: {:?}",
// local_name, data.uuid, data.value
// );
// }
// }
// }
// println!("Disconnecting from peripheral {:?}...", local_name);
// peripheral.disconnect().await?;
// }
// } else {
// println!("Skipping unknown peripheral {:?}", peripheral);
// }
// }
// }
// }
// Ok(())
// }