diff --git a/src/ble.rs b/src/ble.rs index 51c8eb5..655949e 100644 --- a/src/ble.rs +++ b/src/ble.rs @@ -73,8 +73,8 @@ impl Device { /// Return all supported devices that are found in two seconds. /// /// Returns all badges that are in BLE range and are in Bluetooth transfer mode. - pub async fn enumerate() -> Result> { - Self::enumerate_duration(Duration::from_secs(2)).await + pub async fn enumerate(device_name: &String) -> Result> { + Self::enumerate_duration(Duration::from_secs(2), device_name).await } /// Return all supported devices that are found in the given duration. @@ -82,7 +82,10 @@ impl Device { /// Returns all badges that are in BLE range and are in Bluetooth transfer mode. /// # Panics /// This function panics if it is unable to access the Bluetooth adapter. - pub async fn enumerate_duration(scan_duration: Duration) -> Result> { + pub async fn enumerate_duration( + scan_duration: Duration, + device_name: &String, + ) -> Result> { // Run device scan let manager = Manager::new().await.context("create BLE manager")?; let adapters = manager @@ -106,7 +109,7 @@ impl Device { .await .context("enumerating bluetooth devices")? { - if let Some(badge) = Self::from_peripheral(p).await { + if let Some(badge) = Self::from_peripheral(p, device_name).await { led_badges.push(badge); } } @@ -114,7 +117,7 @@ impl Device { Ok(led_badges) } - async fn from_peripheral(peripheral: Peripheral) -> Option { + async fn from_peripheral(peripheral: Peripheral, device_name: &String) -> Option { // The existance of the service with the correct UUID // exists is already checked by the scan filter. // But we also need to check the device name to make sure @@ -123,7 +126,7 @@ impl Device { let props = peripheral.properties().await.ok()??; let local_name = props.local_name.as_ref()?; - if local_name == BADGE_BLE_DEVICE_NAME { + if local_name == device_name { Some(Self { peripheral }) } else { None @@ -134,8 +137,9 @@ impl Device { /// /// This function returns an error if no device could be found /// or if multiple devices would match. - pub async fn single() -> Result { - let mut devices = Self::enumerate() + pub async fn single(device_name: Option) -> Result { + let device_name = &device_name.unwrap_or(BADGE_BLE_DEVICE_NAME.to_string()); + let mut devices = Self::enumerate(device_name) .await .context("enumerating badges")? .into_iter(); diff --git a/src/main.rs b/src/main.rs index 116a66c..6cddd98 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,6 +44,10 @@ struct Args { #[clap(long)] transport: TransportProtocol, + /// Device name for BLE device discovery + #[clap(long)] + device_name: Option, + /// List all devices visible to a transport and exit #[clap(long)] list_devices: bool, @@ -105,7 +109,7 @@ fn main() -> Result<()> { let payload = gnerate_payload(&mut args)?; - write_payload(&args.transport, payload) + write_payload(&args.transport, args.device_name, payload) } fn list_devices(transport: &TransportProtocol) -> Result<()> { @@ -232,6 +236,7 @@ fn gnerate_payload(args: &mut Args) -> Result { fn write_payload( transport: &TransportProtocol, + device_name: Option, payload: PayloadBuffer, ) -> Result<(), anyhow::Error> { match transport { @@ -239,6 +244,6 @@ fn write_payload( TransportProtocol::Ble => tokio::runtime::Builder::new_current_thread() .enable_all() .build()? - .block_on(async { BleDevice::single().await?.write(payload).await }), + .block_on(async { BleDevice::single(device_name).await?.write(payload).await }), } }