Skip to content

Support custom BLE device name discovery #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions src/ble.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,16 +73,19 @@ 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<Vec<Self>> {
Self::enumerate_duration(Duration::from_secs(2)).await
pub async fn enumerate(device_name: &str) -> Result<Vec<Self>> {
Self::enumerate_duration(Duration::from_secs(2), device_name).await
}

/// Return all supported devices that are found in the given duration.
///
/// 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<Vec<Self>> {
pub async fn enumerate_duration(
scan_duration: Duration,
device_name: &str,
) -> Result<Vec<Self>> {
// Run device scan
let manager = Manager::new().await.context("create BLE manager")?;
let adapters = manager
Expand All @@ -106,15 +109,15 @@ 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);
}
}

Ok(led_badges)
}

async fn from_peripheral(peripheral: Peripheral) -> Option<Self> {
async fn from_peripheral(peripheral: Peripheral, device_name: &str) -> Option<Self> {
// 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
Expand All @@ -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
Expand All @@ -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<Self> {
let mut devices = Self::enumerate()
pub async fn single(device_name: Option<&str>) -> Result<Self> {
let device_name = device_name.unwrap_or(BADGE_BLE_DEVICE_NAME);
let mut devices = Self::enumerate(device_name)
.await
.context("enumerating badges")?
.into_iter();
Expand Down
14 changes: 12 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ struct Args {
#[clap(long)]
transport: TransportProtocol,

/// Device name for BLE device discovery
#[clap(long)]
device_name: Option<String>,

/// List all devices visible to a transport and exit
#[clap(long)]
list_devices: bool,
Expand Down Expand Up @@ -105,7 +109,7 @@ fn main() -> Result<()> {

let payload = gnerate_payload(&mut args)?;

write_payload(&args.transport, payload)
write_payload(&args.transport, Option::from(&args.device_name), payload)
}

fn list_devices(transport: &TransportProtocol) -> Result<()> {
Expand Down Expand Up @@ -232,13 +236,19 @@ fn gnerate_payload(args: &mut Args) -> Result<PayloadBuffer> {

fn write_payload(
transport: &TransportProtocol,
device_name: Option<&String>,
payload: PayloadBuffer,
) -> Result<(), anyhow::Error> {
match transport {
TransportProtocol::Usb => UsbDevice::single()?.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.map(String::as_str))
.await?
.write(payload)
.await
}),
}
}