Skip to content

Commit b806b5a

Browse files
committed
Retrieve attributes with 16bit uuid if 128bit fails.
When retrieving attribute handles using the 128bit base version of a 16 bit UUID some devices will report "not found". This will attempt to convert the UUID to the 16bit version and try again. * Adds `to16()` method to NimBLEUUID.
1 parent 42ccd0b commit b806b5a

File tree

5 files changed

+56
-5
lines changed

5 files changed

+56
-5
lines changed

src/NimBLEClient.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -587,14 +587,23 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
587587
return m_servicesVector.back();
588588
}
589589

590-
// If the request was successful but 16/32 bit service not found
590+
// If the request was successful but 16/32 bit uuid not found
591591
// try again with the 128 bit uuid.
592592
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
593593
uuid.bitSize() == BLE_UUID_TYPE_32)
594594
{
595595
NimBLEUUID uuid128(uuid);
596596
uuid128.to128();
597597
return getService(uuid128);
598+
} else {
599+
// If the request was successful but the 128 bit uuid not found
600+
// try again with the 16 bit uuid.
601+
NimBLEUUID uuid16(uuid);
602+
uuid16.to16();
603+
// if the uuid was 128 bit but not of the BLE base type this check will fail
604+
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
605+
return getService(uuid16);
606+
}
598607
}
599608
}
600609

src/NimBLERemoteCharacteristic.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,23 @@ NimBLERemoteDescriptor* NimBLERemoteCharacteristic::getDescriptor(const NimBLEUU
319319
return m_descriptorVector.back();
320320
}
321321

322-
// If the request was successful but 16/32 bit descriptor not found
322+
// If the request was successful but 16/32 bit uuid not found
323323
// try again with the 128 bit uuid.
324324
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
325325
uuid.bitSize() == BLE_UUID_TYPE_32)
326326
{
327327
NimBLEUUID uuid128(uuid);
328328
uuid128.to128();
329329
return getDescriptor(uuid128);
330+
} else {
331+
// If the request was successful but the 128 bit uuid not found
332+
// try again with the 16 bit uuid.
333+
NimBLEUUID uuid16(uuid);
334+
uuid16.to16();
335+
// if the uuid was 128 bit but not of the BLE base type this check will fail
336+
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
337+
return getDescriptor(uuid16);
338+
}
330339
}
331340
}
332341

src/NimBLERemoteService.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,23 @@ NimBLERemoteCharacteristic* NimBLERemoteService::getCharacteristic(const NimBLEU
109109
return m_characteristicVector.back();
110110
}
111111

112-
// If the request was successful but 16/32 bit characteristic not found
112+
// If the request was successful but 16/32 bit uuid not found
113113
// try again with the 128 bit uuid.
114114
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
115115
uuid.bitSize() == BLE_UUID_TYPE_32)
116116
{
117117
NimBLEUUID uuid128(uuid);
118118
uuid128.to128();
119119
return getCharacteristic(uuid128);
120+
} else {
121+
// If the request was successful but the 128 bit uuid not found
122+
// try again with the 16 bit uuid.
123+
NimBLEUUID uuid16(uuid);
124+
uuid16.to16();
125+
// if the uuid was 128 bit but not of the BLE base type this check will fail
126+
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
127+
return getCharacteristic(uuid16);
128+
}
120129
}
121130
}
122131

src/NimBLEUUID.cpp

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -234,8 +234,8 @@ const ble_uuid_any_t* NimBLEUUID::getNative() const {
234234

235235
/**
236236
* @brief Convert a UUID to its 128 bit representation.
237-
* @details A UUID can be internally represented as 16bit, 32bit or the full 128bit. This method
238-
* will convert 16 or 32 bit representations to the full 128bit.
237+
* @details A UUID can be internally represented as 16bit, 32bit or the full 128bit.
238+
* This method will convert 16 or 32bit representations to the full 128bit.
239239
* @return The NimBLEUUID converted to 128bit.
240240
*/
241241
const NimBLEUUID &NimBLEUUID::to128() {
@@ -256,6 +256,29 @@ const NimBLEUUID &NimBLEUUID::to128() {
256256
} // to128
257257

258258

259+
/**
260+
* @brief Convert 128 bit UUID to its 16 bit representation.
261+
* @details A UUID can be internally represented as 16bit, 32bit or the full 128bit.
262+
* This method will convert a 128bit uuid to 16bit if it contains the ble base uuid.
263+
* @return The NimBLEUUID converted to 16bit if successful, otherwise the original uuid.
264+
*/
265+
const NimBLEUUID& NimBLEUUID::to16() {
266+
if (!m_valueSet || m_uuid.u.type == BLE_UUID_TYPE_16) {
267+
return *this;
268+
}
269+
270+
if (m_uuid.u.type == BLE_UUID_TYPE_128) {
271+
uint8_t base128[] = {0xfb, 0x34, 0x9b, 0x5f, 0x80, 0x00,
272+
0x00, 0x80, 0x00, 0x10, 0x00, 0x00};
273+
if (memcmp(m_uuid.u128.value, base128, sizeof(base128)) == 0 ) {
274+
*this = NimBLEUUID(*(uint16_t*)(m_uuid.u128.value + 12));
275+
}
276+
}
277+
278+
return *this;
279+
}
280+
281+
259282
/**
260283
* @brief Get a string representation of the UUID.
261284
* @details

src/NimBLEUUID.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class NimBLEUUID {
4242
bool equals(const NimBLEUUID &uuid) const;
4343
const ble_uuid_any_t* getNative() const;
4444
const NimBLEUUID & to128();
45+
const NimBLEUUID& to16();
4546
std::string toString() const;
4647
static NimBLEUUID fromString(const std::string &uuid);
4748

0 commit comments

Comments
 (0)