@@ -39,6 +39,7 @@ BLERemoteCharacteristic::BLERemoteCharacteristic(
3939 m_charProp = charProp;
4040 m_pRemoteService = pRemoteService;
4141 m_notifyCallback = nullptr ;
42+ m_pRemoteCharacteristicCallbacks = nullptr ;
4243 m_rawData = nullptr ;
4344 m_auth = ESP_GATT_AUTH_REQ_NONE;
4445
@@ -164,6 +165,10 @@ void BLERemoteCharacteristic::gattClientEventHandler(esp_gattc_cb_event_t event,
164165 log_d (" Invoking callback for notification on characteristic %s" , toString ().c_str ());
165166 m_notifyCallback (this , evtParam->notify .value , evtParam->notify .value_len , evtParam->notify .is_notify );
166167 } // End we have a callback function ...
168+ if (m_pRemoteCharacteristicCallbacks != nullptr ) {
169+ log_d (" Invoking callback for notification on characteristic %s" , toString ().c_str ());
170+ m_pRemoteCharacteristicCallbacks->onNotify (this , evtParam->notify .value , evtParam->notify .value_len , evtParam->notify .is_notify );
171+ }
167172 break ;
168173 } // ESP_GATTC_NOTIFY_EVT
169174
@@ -461,44 +466,43 @@ void BLERemoteCharacteristic::registerForNotify(notify_callback notifyCallback,
461466 m_semaphoreRegForNotifyEvt.take (" registerForNotify" );
462467
463468 if (notifyCallback != nullptr ) { // If we have a callback function, then this is a registration.
464- esp_err_t errRc = ::esp_ble_gattc_register_for_notify (
465- m_pRemoteService->getClient ()->getGattcIf (),
466- *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
467- getHandle ()
468- );
469-
470- if (errRc != ESP_OK) {
471- log_e (" esp_ble_gattc_register_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
472- }
473-
474- uint8_t val[] = {0x01 , 0x00 };
475- if (!notifications) val[0 ] = 0x02 ;
476- BLERemoteDescriptor* desc = getDescriptor (BLEUUID ((uint16_t )0x2902 ));
477- if (desc != nullptr && descriptorRequiresRegistration)
478- desc->writeValue (val, 2 , true );
469+ registerNotifications (notifications, descriptorRequiresRegistration);
479470 } // End Register
480471 else { // If we weren't passed a callback function, then this is an unregistration.
481- esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify (
482- m_pRemoteService->getClient ()->getGattcIf (),
483- *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
484- getHandle ()
485- );
486-
487- if (errRc != ESP_OK) {
488- log_e (" esp_ble_gattc_unregister_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
489- }
490-
491- uint8_t val[] = {0x00 , 0x00 };
492- BLERemoteDescriptor* desc = getDescriptor ((uint16_t )0x2902 );
493- if (desc != nullptr && descriptorRequiresRegistration)
494- desc->writeValue (val, 2 , true );
472+ unregisterNotifications (descriptorRequiresRegistration);
495473 } // End Unregister
496474
497475 m_semaphoreRegForNotifyEvt.wait (" registerForNotify" );
498476
499477 log_v (" << registerForNotify()" );
500478} // registerForNotify
501479
480+ /* *
481+ * @brief Set the characteristics callbacks.
482+ *
483+ * @param [in] notifyCallback A callback to be invoked for a notification. If NULL is provided then we are
484+ * unregistering a notification.
485+ * @return N/A.
486+ */
487+ void BLERemoteCharacteristic::setCallbacks (BLERemoteCharacteristicCallbacks* pCallbacks, bool notifications, bool descriptorRequiresRegistration) {
488+ log_v (" >> setCallbacks(): %s" , toString ().c_str ());
489+
490+ m_pRemoteCharacteristicCallbacks = pCallbacks; // Save the notification callback.
491+
492+ m_semaphoreRegForNotifyEvt.take (" registerForNotify" );
493+
494+ if (pCallbacks != nullptr ) { // If we have a callback function, then this is a registration.
495+ registerNotifications (notifications, descriptorRequiresRegistration);
496+ } // End Register
497+ else { // If we weren't passed a callback function, then this is an unregistration.
498+ unregisterNotifications (descriptorRequiresRegistration);
499+ } // End Unregister
500+
501+ m_semaphoreRegForNotifyEvt.wait (" registerForNotify" );
502+
503+ log_v (" << setCallbacks()" );
504+ } // setCallbacks
505+
502506
503507/* *
504508 * @brief Delete the descriptors in the descriptor map.
@@ -516,6 +520,41 @@ void BLERemoteCharacteristic::removeDescriptors() {
516520 m_descriptorMap.clear (); // Technically not neeeded, but just to be sure.
517521} // removeCharacteristics
518522
523+ void BLERemoteCharacteristic::registerNotifications (bool notifications, bool descriptorRequiresRegistration) {
524+ esp_err_t errRc = ::esp_ble_gattc_register_for_notify (
525+ m_pRemoteService->getClient ()->getGattcIf (),
526+ *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
527+ getHandle ()
528+ );
529+
530+ if (errRc != ESP_OK) {
531+ log_e (" esp_ble_gattc_register_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
532+ }
533+
534+ uint8_t val[] = {0x01 , 0x00 };
535+ if (!notifications) val[0 ] = 0x02 ;
536+ BLERemoteDescriptor* desc = getDescriptor (BLEUUID ((uint16_t )0x2902 ));
537+ if (desc != nullptr && descriptorRequiresRegistration)
538+ desc->writeValue (val, 2 , true );
539+ } // registerNotifications
540+
541+ void BLERemoteCharacteristic::unregisterNotifications (bool descriptorRequiresRegistration) {
542+ esp_err_t errRc = ::esp_ble_gattc_unregister_for_notify (
543+ m_pRemoteService->getClient ()->getGattcIf (),
544+ *m_pRemoteService->getClient ()->getPeerAddress ().getNative (),
545+ getHandle ()
546+ );
547+
548+ if (errRc != ESP_OK) {
549+ log_e (" esp_ble_gattc_unregister_for_notify: rc=%d %s" , errRc, GeneralUtils::errorToString (errRc));
550+ }
551+
552+ uint8_t val[] = {0x00 , 0x00 };
553+ BLERemoteDescriptor* desc = getDescriptor ((uint16_t )0x2902 );
554+ if (desc != nullptr && descriptorRequiresRegistration)
555+ desc->writeValue (val, 2 , true );
556+ } // unregisterNotifications
557+
519558
520559/* *
521560 * @brief Convert a BLERemoteCharacteristic to a string representation;
0 commit comments