@@ -56,10 +56,8 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
5656 void setCallbacks (NimBLECharacteristicCallbacks* pCallbacks);
5757 void indicate (uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
5858 void indicate (const uint8_t * value, size_t length, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
59- void indicate (const std::vector<uint8_t >& value, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
6059 void notify (uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
6160 void notify (const uint8_t * value, size_t length, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
62- void notify (const std::vector<uint8_t >& value, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const ;
6361
6462 NimBLEDescriptor* createDescriptor (const char * uuid,
6563 uint32_t properties = NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,
@@ -77,36 +75,47 @@ class NimBLECharacteristic : public NimBLELocalValueAttribute {
7775 /* ********************** Template Functions ************************/
7876
7977 /* *
80- * @brief Template to send a notification from a class type that has a c_str() and length() method.
78+ * @brief Template to send a notification for classes which may have
79+ * data()/size() or c_str()/length() methods. Falls back to sending
80+ * the data by casting the first element of the array to a uint8_t
81+ * pointer and getting the length of the array using sizeof.
8182 * @tparam T The a reference to a class containing the data to send.
8283 * @param[in] value The <type\>value to set.
83- * @param[in] is_notification if true sends a notification, false sends an indication .
84- * @details Only used if the < type\> has a `c_str()` method .
84+ * @param[in] conn_handle The connection handle to send the notification to .
85+ * @note This function is only available if the type T is not a pointer .
8586 */
8687 template <typename T>
87- # ifdef _DOXYGEN_
88- void
89- # else
90- typename std::enable_if<Has_c_str_len<T>::value, void >::type
91- # endif
92- notify (const T& value, bool is_notification = true ) const {
93- notify (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length (), is_notification);
88+ typename std::enable_if<!std::is_pointer<T>::value, void >::type
89+ notify (const T& value, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const {
90+ if constexpr (Has_data_size<T>::value) {
91+ notify (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), conn_handle);
92+ } else if constexpr (Has_c_str_length<T>::value) {
93+ notify (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length (), conn_handle);
94+ } else {
95+ notify (reinterpret_cast <const uint8_t *>(&value), sizeof (value), conn_handle);
96+ }
9497 }
9598
9699 /* *
97- * @brief Template to send an indication from a class type that has a c_str() and length() method.
100+ * @brief Template to send an indication for classes which may have
101+ * data()/size() or c_str()/length() methods. Falls back to sending
102+ * the data by casting the first element of the array to a uint8_t
103+ * pointer and getting the length of the array using sizeof.
98104 * @tparam T The a reference to a class containing the data to send.
99105 * @param[in] value The <type\>value to set.
100- * @details Only used if the <type\> has a `c_str()` method.
106+ * @param[in] conn_handle The connection handle to send the indication to.
107+ * @note This function is only available if the type T is not a pointer.
101108 */
102109 template <typename T>
103- # ifdef _DOXYGEN_
104- void
105- # else
106- typename std::enable_if<Has_c_str_len<T>::value, void >::type
107- # endif
108- indicate (const T& value) const {
109- indicate (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length ());
110+ typename std::enable_if<!std::is_pointer<T>::value, void >::type
111+ indicate (const T& value, uint16_t conn_handle = BLE_HS_CONN_HANDLE_NONE) const {
112+ if constexpr (Has_data_size<T>::value) {
113+ indicate (reinterpret_cast <const uint8_t *>(value.data ()), value.size (), conn_handle);
114+ } else if constexpr (Has_c_str_length<T>::value) {
115+ indicate (reinterpret_cast <const uint8_t *>(value.c_str ()), value.length (), conn_handle);
116+ } else {
117+ indicate (reinterpret_cast <const uint8_t *>(&value), sizeof (value), conn_handle);
118+ }
110119 }
111120
112121 private:
0 commit comments