Skip to content

Commit bc52eac

Browse files
committed
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.
1 parent 9327517 commit bc52eac

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

src/NimBLEClient.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,16 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
454454
if(m_servicesVector.size() > prev_size) {
455455
return m_servicesVector.back();
456456
}
457+
458+
// If the request was successful but 16/32 bit service not found
459+
// try again with the 128 bit uuid.
460+
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
461+
uuid.bitSize() == BLE_UUID_TYPE_32)
462+
{
463+
NimBLEUUID uuid128(uuid);
464+
uuid128.to128();
465+
return getService(uuid128);
466+
}
457467
}
458468

459469
NIMBLE_LOGD(LOG_TAG, "<< getService: not found");

src/NimBLERemoteCharacteristic.cpp

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
244244

245245
for(auto &it: m_descriptorVector) {
246246
if(it->getUUID() == uuid) {
247-
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found");
247+
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: found the descriptor with uuid: %s", uuid.toString().c_str());
248248
return it;
249249
}
250250
}
@@ -254,7 +254,18 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
254254
if(m_descriptorVector.size() > prev_size) {
255255
return m_descriptorVector.back();
256256
}
257+
258+
// If the request was successful but 16/32 bit descriptor not found
259+
// try again with the 128 bit uuid.
260+
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
261+
uuid.bitSize() == BLE_UUID_TYPE_32)
262+
{
263+
NimBLEUUID uuid128(uuid);
264+
uuid128.to128();
265+
return getDescriptor(uuid128);
266+
}
257267
}
268+
258269
NIMBLE_LOGD(LOG_TAG, "<< getDescriptor: Not found");
259270
return nullptr;
260271
} // getDescriptor

src/NimBLERemoteService.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,11 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const char* u
9494
* @return A pointer to the characteristic object, or nullptr if not found.
9595
*/
9696
NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEUUID &uuid) {
97+
NIMBLE_LOGD(LOG_TAG, ">> getCharacteristic: uuid: %s", uuid.toString().c_str());
98+
9799
for(auto &it: m_characteristicVector) {
98100
if(it->getUUID() == uuid) {
101+
NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: found the characteristic with uuid: %s", uuid.toString().c_str());
99102
return it;
100103
}
101104
}
@@ -105,8 +108,19 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU
105108
if(m_characteristicVector.size() > prev_size) {
106109
return m_characteristicVector.back();
107110
}
111+
112+
// If the request was successful but 16/32 bit characteristic not found
113+
// try again with the 128 bit uuid.
114+
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
115+
uuid.bitSize() == BLE_UUID_TYPE_32)
116+
{
117+
NimBLEUUID uuid128(uuid);
118+
uuid128.to128();
119+
return getCharacteristic(uuid128);
120+
}
108121
}
109122

123+
NIMBLE_LOGD(LOG_TAG, "<< getCharacteristic: not found");
110124
return nullptr;
111125
} // getCharacteristic
112126

0 commit comments

Comments
 (0)