@@ -233,9 +233,9 @@ NimBLECharacteristic* NimBLEService::createCharacteristic(const char* uuid, uint
233233 */
234234NimBLECharacteristic* NimBLEService::createCharacteristic (const NimBLEUUID &uuid, uint32_t properties) {
235235 NimBLECharacteristic* pCharacteristic = new NimBLECharacteristic (uuid, properties, this );
236- // Check that we don't add the same characteristic twice.
236+
237237 if (getCharacteristic (uuid) != nullptr ) {
238- NIMBLE_LOGW (LOG_TAG, " << Adding a duplicate characteristic with UUID: %s" ,
238+ NIMBLE_LOGD (LOG_TAG, " << Adding a duplicate characteristic with UUID: %s" ,
239239 std::string (uuid).c_str ());
240240 }
241241
@@ -249,28 +249,72 @@ NimBLECharacteristic* NimBLEService::createCharacteristic(const NimBLEUUID &uuid
249249/* *
250250 * @brief Get a pointer to the characteristic object with the specified UUID.
251251 * @param [in] uuid The UUID of the characteristic.
252+ * @param instanceId The index of the characteristic to return (used when multiple characteristics have the same UUID).
252253 * @return A pointer to the characteristic object or nullptr if not found.
253254 */
254- NimBLECharacteristic* NimBLEService::getCharacteristic (const char * uuid) {
255- return getCharacteristic (NimBLEUUID (uuid));
255+ NimBLECharacteristic* NimBLEService::getCharacteristic (const char * uuid, uint16_t instanceId ) {
256+ return getCharacteristic (NimBLEUUID (uuid), instanceId );
256257}
257258
258-
259259/* *
260260 * @brief Get a pointer to the characteristic object with the specified UUID.
261261 * @param [in] uuid The UUID of the characteristic.
262+ * @param instanceId The index of the characteristic to return (used when multiple characteristics have the same UUID).
262263 * @return A pointer to the characteristic object or nullptr if not found.
263264 */
264- NimBLECharacteristic* NimBLEService::getCharacteristic (const NimBLEUUID &uuid) {
265+ NimBLECharacteristic* NimBLEService::getCharacteristic (const NimBLEUUID &uuid, uint16_t instanceId) {
266+ uint16_t position = 0 ;
265267 for (auto &it : m_chrVec) {
266268 if (it->getUUID () == uuid) {
267- return it;
269+ if (position == instanceId) {
270+ return it;
271+ }
272+ position++;
268273 }
269274 }
275+ return nullptr ;
276+ }
270277
278+ /* *
279+ * @brief Get a pointer to the characteristic object with the specified handle.
280+ * @param handle The handle of the characteristic.
281+ * @return A pointer to the characteristic object or nullptr if not found.
282+ */
283+ NimBLECharacteristic *NimBLEService::getCharacteristicByHandle (uint16_t handle) {
284+ for (auto &it : m_chrVec) {
285+ if (it->getHandle () == handle) {
286+ return it;
287+ }
288+ }
271289 return nullptr ;
272290}
273291
292+ /* *
293+ * @return A vector containing pointers to each characteristic associated with this service.
294+ */
295+ std::vector<NimBLECharacteristic *> NimBLEService::getCharacteristics () {
296+ return m_chrVec;
297+ }
298+
299+ /* *
300+ * @return A vector containing pointers to each characteristic with the provided UUID associated with this service.
301+ */
302+ std::vector<NimBLECharacteristic *> NimBLEService::getCharacteristics (const char *uuid) {
303+ return getCharacteristics (NimBLEUUID (uuid));
304+ }
305+
306+ /* *
307+ * @return A vector containing pointers to each characteristic with the provided UUID associated with this service.
308+ */
309+ std::vector<NimBLECharacteristic *> NimBLEService::getCharacteristics (const NimBLEUUID &uuid) {
310+ std::vector<NimBLECharacteristic*> result;
311+ for (auto &it : m_chrVec) {
312+ if (it->getUUID () == uuid) {
313+ result.push_back (it);
314+ }
315+ }
316+ return result;
317+ }
274318
275319/* *
276320 * @brief Return a string representation of this service.
@@ -295,7 +339,7 @@ std::string NimBLEService::toString() {
295339 */
296340NimBLEServer* NimBLEService::getServer () {
297341 return m_pServer;
298- } // getServer
342+ }// getServer
299343
300344#endif // #if defined(CONFIG_BT_NIMBLE_ROLE_PERIPHERAL)
301345#endif // CONFIG_BT_ENABLED
0 commit comments