feat: add BLE device name config

This commit is contained in:
Valentin Weber 2025-07-20 10:26:03 +02:00
parent 5d745ab8fd
commit c88a2a9ce0
No known key found for this signature in database
GPG key ID: 44797000F143F522
2 changed files with 19 additions and 10 deletions

View file

@ -73,8 +73,8 @@ impl Device {
/// Return all supported devices that are found in two seconds. /// Return all supported devices that are found in two seconds.
/// ///
/// Returns all badges that are in BLE range and are in Bluetooth transfer mode. /// Returns all badges that are in BLE range and are in Bluetooth transfer mode.
pub async fn enumerate() -> Result<Vec<Self>> { pub async fn enumerate(device_name: &String) -> Result<Vec<Self>> {
Self::enumerate_duration(Duration::from_secs(2)).await Self::enumerate_duration(Duration::from_secs(2), device_name).await
} }
/// Return all supported devices that are found in the given duration. /// 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. /// Returns all badges that are in BLE range and are in Bluetooth transfer mode.
/// # Panics /// # Panics
/// This function panics if it is unable to access the Bluetooth adapter. /// This function panics if it is unable to access the Bluetooth adapter.
pub async fn enumerate_duration(scan_duration: Duration) -> Result<Vec<Self>> { pub async fn enumerate_duration(
scan_duration: Duration,
device_name: &String,
) -> Result<Vec<Self>> {
// Run device scan // Run device scan
let manager = Manager::new().await.context("create BLE manager")?; let manager = Manager::new().await.context("create BLE manager")?;
let adapters = manager let adapters = manager
@ -106,7 +109,7 @@ impl Device {
.await .await
.context("enumerating bluetooth devices")? .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); led_badges.push(badge);
} }
} }
@ -114,7 +117,7 @@ impl Device {
Ok(led_badges) Ok(led_badges)
} }
async fn from_peripheral(peripheral: Peripheral) -> Option<Self> { async fn from_peripheral(peripheral: Peripheral, device_name: &String) -> Option<Self> {
// The existance of the service with the correct UUID // The existance of the service with the correct UUID
// exists is already checked by the scan filter. // exists is already checked by the scan filter.
// But we also need to check the device name to make sure // 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 props = peripheral.properties().await.ok()??;
let local_name = props.local_name.as_ref()?; let local_name = props.local_name.as_ref()?;
if local_name == BADGE_BLE_DEVICE_NAME { if local_name == device_name {
Some(Self { peripheral }) Some(Self { peripheral })
} else { } else {
None None
@ -134,8 +137,9 @@ impl Device {
/// ///
/// This function returns an error if no device could be found /// This function returns an error if no device could be found
/// or if multiple devices would match. /// or if multiple devices would match.
pub async fn single() -> Result<Self> { pub async fn single(device_name: Option<String>) -> Result<Self> {
let mut devices = Self::enumerate() let device_name = &device_name.unwrap_or(BADGE_BLE_DEVICE_NAME.to_string());
let mut devices = Self::enumerate(device_name)
.await .await
.context("enumerating badges")? .context("enumerating badges")?
.into_iter(); .into_iter();

View file

@ -44,6 +44,10 @@ struct Args {
#[clap(long)] #[clap(long)]
transport: TransportProtocol, transport: TransportProtocol,
/// Device name for BLE device discovery
#[clap(long)]
device_name: Option<String>,
/// List all devices visible to a transport and exit /// List all devices visible to a transport and exit
#[clap(long)] #[clap(long)]
list_devices: bool, list_devices: bool,
@ -105,7 +109,7 @@ fn main() -> Result<()> {
let payload = gnerate_payload(&mut args)?; 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<()> { fn list_devices(transport: &TransportProtocol) -> Result<()> {
@ -232,6 +236,7 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {
fn write_payload( fn write_payload(
transport: &TransportProtocol, transport: &TransportProtocol,
device_name: Option<String>,
payload: PayloadBuffer, payload: PayloadBuffer,
) -> Result<(), anyhow::Error> { ) -> Result<(), anyhow::Error> {
match transport { match transport {
@ -239,6 +244,6 @@ fn write_payload(
TransportProtocol::Ble => tokio::runtime::Builder::new_current_thread() TransportProtocol::Ble => tokio::runtime::Builder::new_current_thread()
.enable_all() .enable_all()
.build()? .build()?
.block_on(async { BleDevice::single().await?.write(payload).await }), .block_on(async { BleDevice::single(device_name).await?.write(payload).await }),
} }
} }