Skip to content

Commit 05b33d9

Browse files
DavidZemonqdot
authored andcommitted
fix: Increase resilience to characteristic read errors
Discovery of a peripheral's services should not error out merely because one of the characteristics encountered an "AccessDenied" or similar error
1 parent 72922aa commit 05b33d9

File tree

2 files changed

+23
-12
lines changed

2 files changed

+23
-12
lines changed

src/winrtble/ble/device.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,15 +100,27 @@ impl BLEDevice {
100100
let async_result = service
101101
.GetCharacteristicsWithCacheModeAsync(BluetoothCacheMode::Uncached)?
102102
.await?;
103-
let status = async_result.Status();
104-
if status == Ok(GattCommunicationStatus::Success) {
105-
let results = async_result.Characteristics()?;
106-
debug!("characteristics {:?}", results.Size());
107-
Ok(results.into_iter().collect())
108-
} else {
109-
Err(Error::Other(
110-
format!("get_characteristics for {:?} failed: {:?}", service, status).into(),
111-
))
103+
104+
match async_result.Status() {
105+
Ok(GattCommunicationStatus::Success) => {
106+
let results = async_result.Characteristics()?;
107+
debug!("characteristics {:?}", results.Size());
108+
Ok(results.into_iter().collect())
109+
}
110+
Ok(GattCommunicationStatus::ProtocolError) => Err(Error::Other(
111+
format!(
112+
"get_characteristics for {:?} encountered a protocol error",
113+
service
114+
)
115+
.into(),
116+
)),
117+
Ok(status) => {
118+
debug!("characteristic read failed due to {:?}", status);
119+
Ok(vec![])
120+
}
121+
Err(e) => Err(Error::Other(
122+
format!("get_characteristics for {:?} failed: {:?}", service, e).into(),
123+
)),
112124
}
113125
}
114126

src/winrtble/peripheral.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use crate::{
2727
use async_trait::async_trait;
2828
use dashmap::DashMap;
2929
use futures::stream::Stream;
30-
use log::{error, trace};
30+
use log::{trace, warn};
3131
#[cfg(feature = "serde")]
3232
use serde::{Deserialize, Serialize};
3333
#[cfg(feature = "serde")]
@@ -449,8 +449,7 @@ impl ApiPeripheral for Peripheral {
449449
);
450450
}
451451
Err(e) => {
452-
error!("get_characteristics_async {:?}", e);
453-
return Err(e);
452+
warn!("get_characteristics_async {:?}", e);
454453
}
455454
}
456455
}

0 commit comments

Comments
 (0)