Skip to content

Commit 4f25234

Browse files
committed
Introduce BleDevice struct
It's better than a tuple
1 parent ff86a7c commit 4f25234

File tree

2 files changed

+27
-16
lines changed

2 files changed

+27
-16
lines changed

src/connections/ble_handler.rs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,14 @@ impl Display for BleId {
8686
}
8787
}
8888

89+
/// A Meshtastic device discovered via Bluetooth LE.
90+
pub struct BleDevice {
91+
/// The broadcast name of the device.
92+
pub name: Option<String>,
93+
/// The MAC address of the device.
94+
pub mac_address: BDAddr,
95+
}
96+
8997
impl BleHandler {
9098
pub async fn new(ble_id: &BleId, scan_duration: Duration) -> Result<Self, Error> {
9199
let (radio, adapter) = Self::find_ble_radio(ble_id, scan_duration).await?;
@@ -155,16 +163,17 @@ impl BleHandler {
155163
/// Returns a list of all available Meshtastic BLE devices.
156164
///
157165
/// This function scans for devices that expose the Meshtastic service UUID
158-
/// (`6ba1b218-15a8-461f-9fa8-5dcae273eafd`) and returns a list of (name, MAC address) tuples
166+
/// (`6ba1b218-15a8-461f-9fa8-5dcae273eafd`) and returns a list of [`BleDevice`]s
159167
/// that can be used to connect to them.
160-
pub async fn available_ble_devices(
161-
scan_duration: Duration,
162-
) -> Result<Vec<(Option<String>, BDAddr)>, Error> {
168+
pub async fn available_ble_devices(scan_duration: Duration) -> Result<Vec<BleDevice>, Error> {
163169
let peripherals = Self::available_peripherals(scan_duration).await?;
164170
let mut devices = Vec::new();
165171
for (p, _) in &peripherals {
166172
if let Ok(Some(properties)) = p.properties().await {
167-
devices.push((properties.local_name, properties.address));
173+
devices.push(BleDevice {
174+
name: properties.local_name,
175+
mac_address: properties.address,
176+
});
168177
}
169178
}
170179
Ok(devices)

src/utils_internal.rs

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
#[cfg(feature = "bluetooth-le")]
2-
use crate::connections::ble_handler::BleHandler;
2+
use crate::connections::ble_handler::{BleDevice, BleHandler};
33
use crate::errors_internal::Error;
44
#[cfg(feature = "bluetooth-le")]
5-
use btleplug::api::BDAddr;
6-
#[cfg(feature = "bluetooth-le")]
75
use futures::stream::StreamExt;
86
use std::time::Duration;
97
use std::time::UNIX_EPOCH;
@@ -214,23 +212,27 @@ pub async fn build_tcp_stream(
214212
///
215213
/// # Returns
216214
///
217-
/// A vector of Bluetooth devices identified by a MAC address and optionally also a name.
215+
/// A vector of [`BleDevice`]s, each identified by a MAC address and optionally also a name.
218216
///
219217
/// # Examples
220218
///
221-
/// ```
222-
/// let ble_devices = utils::available_ble_devices().await.unwrap();
223-
/// println!("Available Meshtastic BLE devices: {:?}", ble_devices);
224-
/// let stream = build_ble_stream(&ble_devices[0].1.into(), Duration::from_secs(10)).await;
219+
/// ```no_run
220+
/// use std::time::Duration;
221+
/// use meshtastic::utils;
222+
/// use meshtastic::BleId;
223+
///
224+
/// let ble_devices = utils::available_ble_devices(Duration::from_secs(5)).await?;
225+
/// if let Some(device) = ble_devices.first() {
226+
/// let stream = utils::build_ble_stream(&device.mac_address.into(), Duration::from_secs(10)).await;
227+
/// ...
228+
/// }
225229
/// ```
226230
///
227231
/// # Errors
228232
///
229233
/// Fails if the Blueetooth scan fails.
230234
#[cfg(feature = "bluetooth-le")]
231-
pub async fn available_ble_devices(
232-
scan_duration: Duration,
233-
) -> Result<Vec<(Option<String>, BDAddr)>, Error> {
235+
pub async fn available_ble_devices(scan_duration: Duration) -> Result<Vec<BleDevice>, Error> {
234236
BleHandler::available_ble_devices(scan_duration).await
235237
}
236238

0 commit comments

Comments
 (0)