Skip to content

Commit 69bb782

Browse files
committed
Refactor NimBLERemoteCharacteristic::subscribe
* Rearrange subscribe parameters to remove requirement of specifying write response to register a callback. * Update examples.
1 parent 3d73ea8 commit 69bb782

File tree

4 files changed

+37
-26
lines changed

4 files changed

+37
-26
lines changed

examples/NimBLE_Client/NimBLE_Client.ino

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ class AdvertisedDeviceCallbacks: public NimBLEAdvertisedDeviceCallbacks {
107107
void notifyCB(NimBLERemoteCharacteristic* pRemoteCharacteristic, uint8_t* pData, size_t length, bool isNotify){
108108
std::string str = (isNotify == true) ? "Notification" : "Indication";
109109
str += " from ";
110-
str += pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress().toString();
111-
str += ": Service = " + pRemoteCharacteristic->getRemoteService()->getUUID().toString();
112-
str += ", Characteristic = " + pRemoteCharacteristic->getUUID().toString();
110+
/** NimBLEAddress and NimBLEUUID have std::string operators */
111+
str += std::string(pRemoteCharacteristic->getRemoteService()->getClient()->getPeerAddress());
112+
str += ": Service = " + std::string(pRemoteCharacteristic->getRemoteService()->getUUID());
113+
str += ", Characteristic = " + std::string(pRemoteCharacteristic->getUUID());
113114
str += ", Value = " + std::string((char*)pData, length);
114115
Serial.println(str.c_str());
115116
}
@@ -228,17 +229,22 @@ bool connectToServer() {
228229
}
229230
}
230231

232+
/** registerForNotify() has been deprecated and replaced with subscribe() / unsubscribe().
233+
* Subscribe parameter defaults are: notifications=true, notifyCallback=nullptr, response=false.
234+
* Unsubscribe parameter defaults are: response=false.
235+
*/
231236
if(pChr->canNotify()) {
232-
/** Must send a callback to subscribe, if nullptr it will unsubscribe */
233-
if(!pChr->registerForNotify(notifyCB)) {
237+
//if(!pChr->registerForNotify(notifyCB)) {
238+
if(!pChr->subscribe(true, notifyCB)) {
234239
/** Disconnect if subscribe failed */
235240
pClient->disconnect();
236241
return false;
237242
}
238243
}
239244
else if(pChr->canIndicate()) {
240-
/** Send false as second argument to subscribe to indications instead of notifications */
241-
if(!pChr->registerForNotify(notifyCB, false)) {
245+
/** Send false as first argument to subscribe to indications instead of notifications */
246+
//if(!pChr->registerForNotify(notifyCB, false)) {
247+
if(!pChr->subscribe(false, notifyCB)) {
242248
/** Disconnect if subscribe failed */
243249
pClient->disconnect();
244250
return false;
@@ -289,17 +295,22 @@ bool connectToServer() {
289295
}
290296
}
291297

298+
/** registerForNotify() has been deprecated and replaced with subscribe() / unsubscribe().
299+
* Subscribe parameter defaults are: notifications=true, notifyCallback=nullptr, response=false.
300+
* Unsubscribe parameter defaults are: response=false.
301+
*/
292302
if(pChr->canNotify()) {
293-
/** Must send a callback to subscribe, if nullptr it will unsubscribe */
294-
if(!pChr->registerForNotify(notifyCB)) {
303+
//if(!pChr->registerForNotify(notifyCB)) {
304+
if(!pChr->subscribe(true, notifyCB)) {
295305
/** Disconnect if subscribe failed */
296306
pClient->disconnect();
297307
return false;
298308
}
299309
}
300310
else if(pChr->canIndicate()) {
301-
/** Send false as second argument to subscribe to indications instead of notifications */
302-
if(!pChr->registerForNotify(notifyCB, false)) {
311+
/** Send false as first argument to subscribe to indications instead of notifications */
312+
//if(!pChr->registerForNotify(notifyCB, false)) {
313+
if(!pChr->subscribe(false, notifyCB)) {
303314
/** Disconnect if subscribe failed */
304315
pClient->disconnect();
305316
return false;

examples/NimBLE_Server/NimBLE_Server.ino

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99
*/
1010

1111
#include <NimBLEDevice.h>
12-
#include <NimBLE2904.h>
13-
#include <NimBLE2902.h>
1412

1513
static NimBLEServer* pServer;
1614

@@ -112,8 +110,10 @@ class DescriptorCallbacks : public NimBLEDescriptorCallbacks {
112110
NimBLE2902* p2902 = (NimBLE2902*)pDescriptor;
113111
if(p2902->getNotifications()) {
114112
Serial.println("Client Subscribed to notfications");
113+
} else if(p2902->getIndications()) {
114+
Serial.println("Client Subscribed to indications");
115115
} else {
116-
Serial.println("Client Unubscribed to notfications");
116+
Serial.println("Client Unubscribed");
117117
}
118118
} else {
119119
std::string dscVal((char*)pDescriptor->getValue(), pDescriptor->getLength());

src/NimBLERemoteCharacteristic.cpp

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -503,12 +503,12 @@ int NimBLERemoteCharacteristic::onReadCB(uint16_t conn_handle,
503503
/**
504504
* @brief Subscribe or unsubscribe for notifications or indications.
505505
* @param [in] val 0x00 to unsubscribe, 0x01 for notifications, 0x02 for indications.
506-
* @param [in] response If write response required set this to true.
507506
* @param [in] notifyCallback A callback to be invoked for a notification.
507+
* @param [in] response If write response required set this to true.
508508
* If NULL is provided then no callback is performed.
509509
* @return true if successful.
510510
*/
511-
bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_callback notifyCallback) {
511+
bool NimBLERemoteCharacteristic::setNotify(uint16_t val, notify_callback notifyCallback, bool response) {
512512
NIMBLE_LOGD(LOG_TAG, ">> setNotify(): %s, %02x", toString().c_str(), val);
513513

514514
NimBLERemoteDescriptor* desc = getDescriptor(NimBLEUUID((uint16_t)0x2902));
@@ -528,16 +528,16 @@ bool NimBLERemoteCharacteristic::setNotify(uint16_t val, bool response, notify_c
528528
/**
529529
* @brief Subscribe for notifications or indications.
530530
* @param [in] notifications If true, subscribe for notifications, false subscribe for indications.
531-
* @param [in] response If true, require a write response from the descriptor write operation.
532531
* @param [in] notifyCallback A callback to be invoked for a notification.
532+
* @param [in] response If true, require a write response from the descriptor write operation.
533533
* If NULL is provided then no callback is performed.
534534
* @return true if successful.
535535
*/
536-
bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, notify_callback notifyCallback) {
536+
bool NimBLERemoteCharacteristic::subscribe(bool notifications, notify_callback notifyCallback, bool response) {
537537
if(notifications) {
538-
return setNotify(0x01, response, notifyCallback);
538+
return setNotify(0x01, notifyCallback, response);
539539
} else {
540-
return setNotify(0x02, response, notifyCallback);
540+
return setNotify(0x02, notifyCallback, response);
541541
}
542542
} // subscribe
543543

@@ -548,7 +548,7 @@ bool NimBLERemoteCharacteristic::subscribe(bool notifications, bool response, no
548548
* @return true if successful.
549549
*/
550550
bool NimBLERemoteCharacteristic::unsubscribe(bool response) {
551-
return setNotify(0x00, response);
551+
return setNotify(0x00, nullptr, response);
552552
} // unsubscribe
553553

554554

@@ -564,7 +564,7 @@ bool NimBLERemoteCharacteristic::unsubscribe(bool response) {
564564
bool NimBLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback, bool notifications, bool response) {
565565
bool success;
566566
if(notifyCallback != nullptr) {
567-
success = subscribe(notifications, response, notifyCallback);
567+
success = subscribe(notifications, notifyCallback, response);
568568
} else {
569569
success = unsubscribe(response);
570570
}

src/NimBLERemoteCharacteristic.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ class NimBLERemoteCharacteristic {
104104
}
105105

106106
bool subscribe(bool notifications = true,
107-
bool response = true,
108-
notify_callback notifyCallback = nullptr);
109-
bool unsubscribe(bool response = true);
107+
notify_callback notifyCallback = nullptr,
108+
bool response = false);
109+
bool unsubscribe(bool response = false);
110110
bool registerForNotify(notify_callback notifyCallback,
111111
bool notifications = true,
112112
bool response = true)
@@ -138,7 +138,7 @@ class NimBLERemoteCharacteristic {
138138
friend class NimBLERemoteDescriptor;
139139

140140
// Private member functions
141-
bool setNotify(uint16_t val, bool response = true, notify_callback notifyCallback = nullptr);
141+
bool setNotify(uint16_t val, notify_callback notifyCallback = nullptr, bool response = true);
142142
bool retrieveDescriptors(const NimBLEUUID *uuid_filter = nullptr);
143143
static int onReadCB(uint16_t conn_handle, const struct ble_gatt_error *error,
144144
struct ble_gatt_attr *attr, void *arg);

0 commit comments

Comments
 (0)