From f841f030ae69e176bad8535c203479fca50a59fe Mon Sep 17 00:00:00 2001 From: David Robertson Date: Wed, 29 Dec 2021 03:11:37 +0000 Subject: [PATCH] Add method to send notifications/indications with custom values (#63) --- src/NimBLECharacteristic.cpp | 17 +++++++++++++---- src/NimBLECharacteristic.h | 2 ++ 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/src/NimBLECharacteristic.cpp b/src/NimBLECharacteristic.cpp index 5645a5e..9a27301 100644 --- a/src/NimBLECharacteristic.cpp +++ b/src/NimBLECharacteristic.cpp @@ -397,6 +397,7 @@ void NimBLECharacteristic::indicate() { NIMBLE_LOGD(LOG_TAG, "<< indicate"); } // indicate + /** * @brief Send a notification.\n * A notification is a transmission of up to the first 20 bytes of the characteristic value.\n @@ -404,8 +405,18 @@ void NimBLECharacteristic::indicate() { * @param[in] is_notification if true sends a notification, false sends an indication. */ void NimBLECharacteristic::notify(bool is_notification) { - NIMBLE_LOGD(LOG_TAG, ">> notify: length: %d", getDataLength()); + notify(getValue(), is_notification); +} +/** + * @brief Send a notification.\n + * A notification is a transmission of up to the first 20 bytes of the characteristic value.\n + * A notification will not block; it is a fire and forget. + * @param[in] is_notification if true sends a notification, false sends an indication. + */ +void NimBLECharacteristic::notify(std::string value, bool is_notification) { + size_t length = value.length(); + NIMBLE_LOGD(LOG_TAG, ">> notify: length: %d", length); if(!(m_properties & NIMBLE_PROPERTY::NOTIFY) && !(m_properties & NIMBLE_PROPERTY::INDICATE)) @@ -421,9 +432,7 @@ void NimBLECharacteristic::notify(bool is_notification) { } m_pCallbacks->onNotify(this); - - std::string value = getValue(); - size_t length = value.length(); + bool reqSec = (m_properties & BLE_GATT_CHR_F_READ_AUTHEN) || (m_properties & BLE_GATT_CHR_F_READ_AUTHOR) || (m_properties & BLE_GATT_CHR_F_READ_ENC); diff --git a/src/NimBLECharacteristic.h b/src/NimBLECharacteristic.h index 33528e3..b636d6d 100644 --- a/src/NimBLECharacteristic.h +++ b/src/NimBLECharacteristic.h @@ -84,6 +84,8 @@ public: void indicate(); void notify(bool is_notification = true); + void notify(std::string value, bool is_notification = true); + size_t getSubscribedCount(); NimBLEDescriptor* createDescriptor(const char* uuid,