summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-16 22:14:59 +0200
committerDario Nieuwenhuis <dirbaio@dirbaio.net>2020-09-16 22:14:59 +0200
commit1eeb07a028f08f819a5a579e8ca41f0fd143aa6c (patch)
tree7ccd7ccc515cea57dfe9c0e64d86c780c763a735 /examples
parent9aa2ad0377b4b75f7a8602a51bf9d4eea2a36e84 (diff)
downloadnrf-softdevice-1eeb07a028f08f819a5a579e8ca41f0fd143aa6c.zip
gatt_server: add support for notifications.
Diffstat (limited to 'examples')
-rw-r--r--examples/src/bin/ble_bas_peripheral.rs25
1 files changed, 24 insertions, 1 deletions
diff --git a/examples/src/bin/ble_bas_peripheral.rs b/examples/src/bin/ble_bas_peripheral.rs
index 8373d6d..c5c26ab 100644
--- a/examples/src/bin/ble_bas_peripheral.rs
+++ b/examples/src/bin/ble_bas_peripheral.rs
@@ -29,6 +29,8 @@ struct BatteryServiceServer {
enum BatteryServiceEvent {
BatteryLevelWrite(u8),
+ BatteryLevelNotificationsEnabled,
+ BatteryLevelNotificationsDisabled,
}
impl gatt_server::Server for BatteryServiceServer {
@@ -64,6 +66,13 @@ impl gatt_server::Server for BatteryServiceServer {
if handle == self.battery_level_value_handle {
return Some(BatteryServiceEvent::BatteryLevelWrite(data[0]));
}
+ if handle == self.battery_level_cccd_handle {
+ if data.len() != 0 && data[0] & 0x01 != 0 {
+ return Some(BatteryServiceEvent::BatteryLevelNotificationsEnabled);
+ } else {
+ return Some(BatteryServiceEvent::BatteryLevelNotificationsDisabled);
+ }
+ }
None
}
}
@@ -99,7 +108,21 @@ async fn bluetooth_task(sd: &'static Softdevice) {
// Run the GATT server on the connection. This returns when the connection gets disconnected.
let res = gatt_server::run(&conn, &server, |e| match e {
BatteryServiceEvent::BatteryLevelWrite(data) => {
- info!("wrote battery level: {:u8}", data)
+ info!("wrote battery level: {:u8}", data);
+ let res = gatt_server::send_notification(
+ &conn,
+ server.battery_level_value_handle,
+ &[data + 1],
+ );
+ if let Err(e) = res {
+ info!("send notification error: {:?}", e);
+ }
+ }
+ BatteryServiceEvent::BatteryLevelNotificationsEnabled => {
+ info!("battery notifications enabled")
+ }
+ BatteryServiceEvent::BatteryLevelNotificationsDisabled => {
+ info!("battery notifications disabled")
}
})
.await;