use std::time::Duration;
use matter_ble::central::{BleCentral, CentralError};
const PER_NIBBLE: Duration = Duration::from_secs(2);
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
if std::env::var("MATTER_BLE_LIVE").as_deref() != Ok("1") {
println!(
"ble_scan is gated: it opens CoreBluetooth and may raise the macOS \
Bluetooth permission prompt.\n\
Set MATTER_BLE_LIVE=1 to run a live scan:\n\
\n MATTER_BLE_LIVE=1 cargo run -p matter-controller \
--example ble_scan --features ble\n\n\
See docs/runbooks/ble-commissioning.md."
);
return Ok(());
}
println!("Acquiring Bluetooth adapter (may prompt for permission)...");
let central = BleCentral::new().await?;
println!("Sweeping for commissionable Matter devices (~30 s)...");
let mut count = 0u32;
for nibble in 0u16..16 {
match central.find_device(nibble, true, PER_NIBBLE).await {
Ok(dev) => {
count += 1;
println!(
" found: discriminator=0x{:03x} vid=0x{:04x} pid=0x{:04x} id={:?}",
dev.advert.discriminator,
dev.advert.vendor_id,
dev.advert.product_id,
dev.peripheral_id,
);
}
Err(CentralError::ScanTimeout) => {}
Err(e) => return Err(e.into()),
}
}
println!("Scan complete: {count} device(s) answered.");
Ok(())
}