From 39a3a63f80b0541a6e57a5dcf5dd0083e6a31e1d Mon Sep 17 00:00:00 2001 From: h2zero Date: Mon, 28 Dec 2020 15:40:01 -0700 Subject: [PATCH] Fix attributes not found with 16/32bit UUIDs. * Some peripherals will advertise 16/32bit UUIDs but when queried for their handles they do not convert the UUID to 128 bit locally and will return attribute not found. This patch will query the peripheral a second time with a 128bit converted UUID. --- src/NimBLEClient.cpp | 10 ++++++++++ src/NimBLERemoteCharacteristic.cpp | 13 ++++++++++++- src/NimBLERemoteService.cpp | 14 ++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) diff --git a/src/NimBLEClient.cpp b/src/NimBLEClient.cpp index 94d2dbb..04520a4 100644 --- a/src/NimBLEClient.cpp +++ b/src/NimBLEClient.cpp @@ -454,6 +454,16 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) { if(m_servicesVector.size() > prev_size) { return m_servicesVector.back(); } + + // If the request was successful but 16/32 bit service not found + // try again with the 128 bit uuid. + if(uuid.bitSize() == BLE_UUID_TYPE_16 || + uuid.bitSize() == BLE_UUID_TYPE_32) + { + NimBLEUUID uuid128(uuid); + uuid128.to128(); + return getService(uuid128); + } } NIMBLE_LOGD(LOG_TAG, "<< getService: not found"); diff --git a/src/NimBLERemoteCharacteristic.cpp b/src/NimBLERemoteCharacteristic.cpp index 4dac42a..a81d48d 100644 --- a/src/NimBLERemoteCharacteristic.cpp +++ b/src/NimBLERemoteCharacteristic.cpp @@ -244,7 +244,7 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU for(auto &it: m_descriptorVector) { if(it->getUUID() == uuid) { - NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found"); + NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found the descriptor with uuid: %s", uuid.toString().c_str()); return it; } } @@ -254,7 +254,18 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU if(m_descriptorVector.size() > prev_size) { return m_descriptorVector.back(); } + + // If the request was successful but 16/32 bit descriptor not found + // try again with the 128 bit uuid. + if(uuid.bitSize() == BLE_UUID_TYPE_16 || + uuid.bitSize() == BLE_UUID_TYPE_32) + { + NimBLEUUID uuid128(uuid); + uuid128.to128(); + return getDescriptor(uuid128); + } } + NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: Not found"); return nullptr; } // getDescriptor diff --git a/src/NimBLERemoteService.cpp b/src/NimBLERemoteService.cpp index 9476871..0b22992 100644 --- a/src/NimBLERemoteService.cpp +++ b/src/NimBLERemoteService.cpp @@ -94,8 +94,11 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u * @return A pointer to the characteristic object, or nullptr if not found. */ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEUUID &uuid) { + NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str()); + for(auto &it: m_characteristicVector) { if(it->getUUID() == uuid) { + NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: found the characteristic with uuid: %s", uuid.toString().c_str()); return it; } } @@ -105,8 +108,19 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU if(m_characteristicVector.size() > prev_size) { return m_characteristicVector.back(); } + + // If the request was successful but 16/32 bit characteristic not found + // try again with the 128 bit uuid. + if(uuid.bitSize() == BLE_UUID_TYPE_16 || + uuid.bitSize() == BLE_UUID_TYPE_32) + { + NimBLEUUID uuid128(uuid); + uuid128.to128(); + return getCharacteristic(uuid128); + } } + NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: not found"); return nullptr; } // getCharacteristic