Improve error handling in BLE device enumeration

This commit is contained in:
Lilian 2024-07-30 19:26:51 +02:00
parent 3fe70906e6
commit 9330fee127
No known key found for this signature in database
GPG key ID: 1650987A001CB2CB

View file

@ -36,8 +36,13 @@ impl Device {
/// 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) -> Result<Vec<Self>> {
// Run device scan // Run device scan
let manager = Manager::new().await.unwrap(); let manager = Manager::new().await?;
let adapter = manager.adapters().await.unwrap().pop().unwrap(); let adapter = manager.adapters().await?.pop();
if adapter.is_none() {
return Err(anyhow::anyhow!("No Bluetooth adapter found"));
}
let adapter = adapter.unwrap();
adapter.start_scan(ScanFilter::default()).await?; adapter.start_scan(ScanFilter::default()).await?;
task::sleep(scan_duration).await; task::sleep(scan_duration).await;