From f3ebf77ed21d2c9ab4b65f8bd4fba2815119943b Mon Sep 17 00:00:00 2001 From: Anay Wadhera Date: Thu, 23 Feb 2023 12:52:57 -0800 Subject: [PATCH] Add GattClient#SetCharacteristicNotification for subscribing to a GATT characteristic PiperOrigin-RevId: 511862183 --- internal/platform/implementation/apple/ble.h | 3 +++ internal/platform/implementation/apple/ble.mm | 6 ++++++ internal/platform/implementation/ble_v2.h | 11 +++++++++++ internal/platform/implementation/g3/ble_v2.cc | 7 +++++++ internal/platform/implementation/g3/ble_v2.h | 4 ++++ 5 files changed, 31 insertions(+) diff --git a/internal/platform/implementation/apple/ble.h b/internal/platform/implementation/apple/ble.h index ca734824..a2b7c5ae 100644 --- a/internal/platform/implementation/apple/ble.h +++ b/internal/platform/implementation/apple/ble.h @@ -184,6 +184,9 @@ class BleMedium : public api::ble_v2::BleMedium { bool WriteCharacteristic(const api::ble_v2::GattCharacteristic &characteristic, const ByteArray &value) override; + bool SetCharacteristicNotification(const api::ble_v2::GattCharacteristic &characteristic, + bool enable) override; + void Disconnect() override; private: diff --git a/internal/platform/implementation/apple/ble.mm b/internal/platform/implementation/apple/ble.mm index 8fb5c74b..ee388125 100644 --- a/internal/platform/implementation/apple/ble.mm +++ b/internal/platform/implementation/apple/ble.mm @@ -585,6 +585,12 @@ bool BleMedium::GattClient::WriteCharacteristic( return false; } +bool BleMedium::GattClient::SetCharacteristicNotification( + const api::ble_v2::GattCharacteristic& characteristic, bool enable) { + // No op since we can't write characteristics. + return false; +} + void BleMedium::GattClient::Disconnect() { [central_ disconnectGattServiceWithPeripheralID:ObjCStringFromCppString(peripheral_id_)]; } diff --git a/internal/platform/implementation/ble_v2.h b/internal/platform/implementation/ble_v2.h index fa128220..d7a41098 100644 --- a/internal/platform/implementation/ble_v2.h +++ b/internal/platform/implementation/ble_v2.h @@ -25,6 +25,7 @@ #include #include "absl/container/flat_hash_map.h" +#include "absl/functional/any_invocable.h" #include "absl/status/status.h" #include "absl/strings/string_view.h" #include "internal/platform/byte_array.h" @@ -180,6 +181,12 @@ class GattClient { virtual bool WriteCharacteristic(const GattCharacteristic& characteristic, const ByteArray& value) = 0; + // https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#setCharacteristicNotification(android.bluetooth.BluetoothGattCharacteristic,%20boolean) + // + // Enable or disable notifications/indications for a given characteristic. + virtual bool SetCharacteristicNotification( + const GattCharacteristic& characteristic, bool enable) = 0; + // https://developer.android.com/reference/android/bluetooth/BluetoothGatt.html#disconnect() virtual void Disconnect() = 0; }; @@ -223,6 +230,10 @@ class GattServer { // Callback for asynchronous events on the client side of a GATT connection. struct ClientGattConnectionCallback { public: + // Called when the characteristic is changed + absl::AnyInvocable + on_characteristic_changed_cb = [](const GattCharacteristic&) {}; + // Called when the client is disconnected from the GATT server. absl::AnyInvocable disconnected_cb = []() {}; }; diff --git a/internal/platform/implementation/g3/ble_v2.cc b/internal/platform/implementation/g3/ble_v2.cc index 1689f8d2..80b92d85 100644 --- a/internal/platform/implementation/g3/ble_v2.cc +++ b/internal/platform/implementation/g3/ble_v2.cc @@ -20,6 +20,7 @@ #include #include #include +#include #include "absl/status/status.h" #include "absl/strings/escaping.h" @@ -452,6 +453,12 @@ bool BleV2Medium::GattClient::WriteCharacteristic( return false; } +bool BleV2Medium::GattClient::SetCharacteristicNotification( + const api::ble_v2::GattCharacteristic& characteristic, bool enable) { + // No op since we can't write characteristics. + return false; +} + void BleV2Medium::GattClient::Disconnect() { absl::MutexLock lock(&mutex_); NEARBY_LOGS(INFO) << "G3 Ble GattClient Disconnect"; diff --git a/internal/platform/implementation/g3/ble_v2.h b/internal/platform/implementation/g3/ble_v2.h index 6e9e79f6..ccf86551 100644 --- a/internal/platform/implementation/g3/ble_v2.h +++ b/internal/platform/implementation/g3/ble_v2.h @@ -240,6 +240,10 @@ class BleV2Medium : public api::ble_v2::BleMedium { const api::ble_v2::GattCharacteristic& characteristic, const ByteArray& value) override; + bool SetCharacteristicNotification( + const api::ble_v2::GattCharacteristic& characteristic, + bool enable) override; + void Disconnect() override; private: