From d4b3ec8ddd96b8619600d90d3e2a87f81a0b780c Mon Sep 17 00:00:00 2001 From: Linus Oleander Date: Sat, 25 Nov 2023 02:31:35 +0100 Subject: [PATCH] Add notification check and properties to BLE characteristic --- examples/ble_client.rs | 5 ++++ src/client/ble_remote_characteristic.rs | 36 +++++++++++++++++++++++++ 2 files changed, 41 insertions(+) diff --git a/examples/ble_client.rs b/examples/ble_client.rs index 5c4d55d..c704e05 100644 --- a/examples/ble_client.rs +++ b/examples/ble_client.rs @@ -57,6 +57,11 @@ fn main() { let uuid = uuid128!("a3c87500-8ed3-4bdf-8a39-a01bebede295"); let characteristic = service.get_characteristic(uuid).await.unwrap(); + + if !characteristic.can_notify() { + return ::log::error!("characteristic can't notify: {:?}", uuid); + } + ::log::info!("subscribe {:?}", uuid); characteristic .on_notify(|data| { diff --git a/src/client/ble_remote_characteristic.rs b/src/client/ble_remote_characteristic.rs index 4428233..a236f9c 100644 --- a/src/client/ble_remote_characteristic.rs +++ b/src/client/ble_remote_characteristic.rs @@ -221,6 +221,42 @@ impl BLERemoteCharacteristic { self } + pub fn can_notify(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::NOTIFY) + } + + pub fn can_indicate(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::INDICATE) + } + + pub fn can_read(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::READ) + } + + pub fn can_write(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::WRITE) + } + + pub fn can_write_no_response(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::WRITE_NO_RSP) + } + + pub fn can_broadcast(&self) -> bool { + self + .properties() + .contains(GattCharacteristicProperties::BROADCAST) + } + pub(crate) unsafe fn notify(&mut self, om: *mut esp_idf_sys::os_mbuf) { if let Some(no_notify) = self.state.on_notify.as_mut() { let data = unsafe { core::slice::from_raw_parts((*om).om_data, (*om).om_len as _) };