diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index fb5d3b4..493def4 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -494,7 +494,58 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, /** - * @brief Register for notifications. + * @brief Subscribe or unsubscribe for notifications or indications. + * @param [in] uint16_t val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications. + * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback + * is performed for notifications. + * @return true if successful. + */ +bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_callback notifyCallback) { + NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val); + + NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902)); + if(desc == nullptr) { + NIMBLE_LOGE(LOG_TAG, "<< setNotify(): Could not get descriptor"); + return false; + } + + m_notifyCallback = notifyCallback; // Save the notification callback. + + NIMBLE_LOGD(LOG_TAG, "<< setNotify()"); + + return desc->writeValue((uint8_t *)&val, 2, response); +} // setNotify + + +/** + * @brief Subscribe for notifications or indications. + * @param [in] bool if true, subscribe for notifications, false subscribe for indications. + * @param [in] bool if true, require a write response from the descriptor write operation. + * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then no callback + * is performed for notifications. + * @return true if successful. + */ +bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, notify_callback notifyCallback) { + if(notifications) { + return setNotify(0x01, response, notifyCallback); + } else { + return setNotify(0x02, response, notifyCallback); + } +} // subscribe + + +/** + * @brief Unsubscribe for notifications or indications. + * @param [in] bool if true, require a write response from the descriptor write operation. + * @return true if successful. + */ +bool NimBLERemoteCharacteristic::unsubscribe(bool response) { + return setNotify(0x00, response); +} // unsubscribe + + + /** + * @brief backward-compatibility method for subscribe/unsubscribe notifications/indications * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are * unregistering for notifications. * @param [in] bool if true, register for notifications, false register for indications. @@ -502,29 +553,13 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle, * @return true if successful. */ bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) { - NIMBLE_LOGD(LOG_TAG, ">> registerForNotify(): %s", toString().c_str()); - - m_notifyCallback = notifyCallback; // Save the notification callback. - - uint8_t val[] = {0x01, 0x00}; - - NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902)); - if(desc == nullptr) - return false; - - if(notifyCallback != nullptr){ - if(!notifications){ - val[0] = 0x02; - } + bool success; + if(notifyCallback != nullptr) { + success = subscribe(notifications, response, notifyCallback); + } else { + success = unsubscribe(response); } - - else if (notifyCallback == nullptr){ - val[0] = 0x00; - } - - NIMBLE_LOGD(LOG_TAG, "<< registerForNotify()"); - - return desc->writeValue(val, 2, response); + return success; } // registerForNotify diff --git a/src/NimBLERemoteCharacteristic.h b/src/NimBLERemoteCharacteristic.h index 6a17a55..ab67cd1 100644 --- a/src/NimBLERemoteCharacteristic.h +++ b/src/NimBLERemoteCharacteristic.h @@ -78,9 +78,13 @@ public: return *((T *)pData); } - bool registerForNotify(notify_callback _callback, + bool subscribe(bool notifications = true, + bool response = true, + notify_callback notifyCallback = nullptr); + bool unsubscribe(bool response = true); + bool registerForNotify(notify_callback notifyCallback, bool notifications = true, - bool response = true); + bool response = true) __attribute__ ((deprecated)); bool writeValue(const uint8_t* data, size_t length, bool response = false); @@ -100,6 +104,7 @@ private: friend class NimBLERemoteDescriptor; // Private member functions + bool setNotify(uint16_t val, bool response = true, notify_callback notifyCallback = nullptr); bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr); static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error, struct ble_gatt_attr *attr, void *arg);