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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
//! Bluest is a cross-platform [Bluetooth Low Energy] (BLE) library for [Rust]. It currently supports Windows (version
//! 10 and later), MacOS/iOS, and Linux. Android support is planned.
//!
//! The goal of Bluest is to create a *thin* abstraction on top of the platform-specific Bluetooth APIs in order to
//! provide safe, cross-platform access to Bluetooth LE devices. The crate currently supports the GAP Central and
//! GATT Client roles. Peripheral and Server roles are not supported.
//!
//! [Rust]: https://www.rust-lang.org/
//! [Bluetooth Low Energy]: https://www.bluetooth.com/specifications/specs/
//!
//! # Usage
//!
//! ```rust,no_run
//!# use bluest::Adapter;
//!# use futures_lite::StreamExt;
//!# #[tokio::main]
//!# async fn main() -> Result<(), Box<dyn std::error::Error>> {
//!let adapter = Adapter::default().await.ok_or("Bluetooth adapter not found")?;
//!adapter.wait_available().await?;
//!
//!println!("starting scan");
//!let mut scan = adapter.scan(&[]).await?;
//!println!("scan started");
//!while let Some(discovered_device) = scan.next().await {
//! println!(
//! "{}{}: {:?}",
//! discovered_device.device.name().as_deref().unwrap_or("(unknown)"),
//! discovered_device
//! .rssi
//! .map(|x| format!(" ({}dBm)", x))
//! .unwrap_or_default(),
//! discovered_device.adv_data.services
//! );
//!}
//!#
//!# Ok(())
//!# }
//! ```
//!
//! # Overview
//!
//! The primary functions provided by Bluest are:
//!
//! - Device discovery:
//! - [Scanning][Adapter::scan] for devices and receiving advertisements
//! - Finding [connected devices][Adapter::connected_devices]
//! - [Opening][Adapter::open_device] previously found devices
//! - [Connecting][Adapter::connect_device] to discovered devices
//! - [Pairing][Device::pair] with devices
//! - Accessing remote GATT services:
//! - Discovering device [services][Device::discover_services]
//! - Discovering service [characteristics][Service::discover_characteristics]
//! - Discovering characteristic [descriptors][Characteristic::discover_descriptors]
//! - [Read][Characteristic::read], [write][Characteristic::write] (including
//! [write without response][Characteristic::write_without_response]), and
//! [notify/indicate][Characteristic::notify] operations on remote characteristics
//! - [Read][Descriptor::read] and [write][Descriptor::write] operations on characteristic descriptors
//!
//! # Asynchronous runtimes
//!
//! On non-linux platforms, Bluest should work with any asynchronous runtime. On linux the underlying `bluer` crate
//! requires the Tokio runtime and Bluest makes use of Tokio's `block_in_place` API (which requires Tokio's
//! multi-threaded runtime) to make a few methods synchronous. Linux-only asynchronous versions of those methods are
//! also provided, which should be preferred in platform-specific code.
//!
//! # Platform specifics
//!
//! Because Bluest aims to provide a thin abstraction over the platform-specific APIs, the available APIs represent the
//! lowest common denominator of APIs among the supported platforms. For example, CoreBluetooth never exposes the
//! Bluetooth address of devices to applications, therefore there is no method on `Device` for retrieving an address or
//! even any Bluetooth address struct in the crate.
//!
//! Most Bluest APIs should behave consistently across all supported platforms. Those APIs with significant differences
//! in behavior are summarized in the table below.
//!
//!| Method | MacOS/iOS | Windows | Linux |
//!|----------------------------------------------------------|:---------:|:-------:|:-----:|
//!| [`Adapter::connect_device`][Adapter::connect_device] | ✅ | ✨ | ✅ |
//!| [`Adapter::disconnect_device`][Adapter::disconnect_device] | ✅ | ✨ | ✅ |
//!| [`Device::name`][Device::name] | ✅ | ✅ | ⌛️ |
//!| [`Device::is_paired`][Device::is_paired] | ❌ | ✅ | ✅ |
//!| [`Device::pair`][Device::pair] | ✨ | ✅ | ✅ |
//!| [`Device::pair_with_agent`][Device::pair_with_agent] | ✨ | ✅ | ✅ |
//!| [`Device::unpair`][Device::unpair] | ❌ | ✅ | ✅ |
//!| [`Device::rssi`][Device::rssi] | ✅ | ❌ | ❌ |
//!| [`Service::uuid`][Service::uuid] | ✅ | ✅ | ⌛️ |
//!| [`Service::is_primary`][Service::is_primary] | ✅ | ❌ | ✅ |
//!| [`Characteristic::uuid`][Characteristic::uuid] | ✅ | ✅ | ⌛️ |
//!| [`Characteristic::max_write_len`][Characteristic::max_write_len] | ✅ | ✅ | ⌛️ |
//!| [`Descriptor::uuid`][Descriptor::uuid] | ✅ | ✅ | ⌛️ |
//!
//! ✅ = supported
//! ✨ = managed automatically by the OS, this method is a no-op
//! ⌛️ = the underlying API is async so this method uses Tokio's `block_in_place` API internally
//! ❌ = returns a [`NotSupported`][error::ErrorKind::NotSupported] error
//!
//! Also, the errors returned by APIs in a given situation may not be consistent from platform to platform. For example,
//! Linux's bluez API does not return the underlying Bluetooth protocol error in a useful way, whereas the other
//! platforms do. Where it is possible to return a meaningful error, Bluest will attempt to do so. In other cases,
//! Bluest may return an error with a [`kind`][Error::kind] of [`Other`][error::ErrorKind::Other] and you would need to
//! look at the platform-specific [`source`][std::error::Error::source] of the error for more information.
//!
//! # Feature flags
//!
//! The `serde` feature is available to enable serializing/deserializing device
//! identifiers.
//!
//! # Examples
//!
//! Examples demonstrating basic usage are available in the [examples folder].
//!
//! [examples folder]: https://github.com/alexmoon/bluest/tree/master/bluest/examples
compile_error!;
compile_error!;
compile_error!;
use HashMap;
pub use Uuid;
pub use Adapter;
pub use BluetoothUuidExt;
pub use Characteristic;
pub use Descriptor;
pub use ;
pub use Error;
pub use ;
pub use Service;
pub use DeviceId;
pub use Uuid;
use crateandroid as sys;
use cratebluer as sys;
use cratecorebluetooth as sys;
use cratewindows as sys;
/// Convenience alias for a result with [`Error`]
pub type Result<T, E = Error> = Result;
/// Events generated by [`Adapter::events`]
/// Events generated by [`Adapter::device_connection_events`]
/// Represents a device discovered during a scan operation
/// Data included in a Bluetooth advertisement or scan reponse.
/// Manufacturer specific data included in Bluetooth advertisements. See the Bluetooth Core Specification Supplement
/// §A.1.4 for details.
/// GATT characteristic properties as defined in the Bluetooth Core Specification, Vol 3, Part G, §3.3.1.1.
/// Extended properties are also included as defined in §3.3.3.1.