Skip to content

Commit aeaa432

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 cbebbd0 commit aeaa432

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
@@ -611,14 +611,23 @@ NimBLERemoteService* NimBLEClient::getService(const NimBLEUUID &uuid) {
611611
return m_servicesVector.back();
612612
}
613613

614-
// If the request was successful but 16/32 bit service not found
614+
// If the request was successful but 16/32 bit uuid not found
615615
// try again with the 128 bit uuid.
616616
if(uuid.bitSize() == BLE_UUID_TYPE_16 ||
617617
uuid.bitSize() == BLE_UUID_TYPE_32)
618618
{
619619
NimBLEUUID uuid128(uuid);
620620
uuid128.to128();
621621
return getService(uuid128);
622+
} else {
623+
// If the request was successful but the 128 bit uuid not found
624+
// try again with the 16 bit uuid.
625+
NimBLEUUID uuid16(uuid);
626+
uuid16.to16();
627+
// if the uuid was 128 bit but not of the BLE base type this check will fail
628+
if (uuid16.bitSize() == BLE_UUID_TYPE_16) {
629+
return getService(uuid16);
630+
}
622631
}
623632
}
624633

src/NimBLERemoteCharacteristic.cpp

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

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

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
@@ -235,8 +235,8 @@ const ble_uuid_any_t* NimBLEUUID::getNative() const {
235235

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

259259

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

src/NimBLEUUID.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ class NimBLEUUID {
4848
bool equals(const NimBLEUUID &uuid) const;
4949
const ble_uuid_any_t* getNative() const;
5050
const NimBLEUUID & to128();
51+
const NimBLEUUID& to16();
5152
std::string toString() const;
5253
static NimBLEUUID fromString(const std::string &uuid);
5354

0 commit comments

Comments
 (0)