Skip to content

Commit 1bf80af

Browse files
authored
Merge pull request #535 from pyro9/fix_discovery
Fix BLEDiscovery::discoverCharacteristic when the central device returns more than 4 Characteristics in a discovery request
2 parents 604a8c8 + df5f6c4 commit 1bf80af

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

libraries/Bluefruit52Lib/src/BLEDiscovery.cpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,10 @@ bool BLEDiscovery::_discoverService(uint16_t conn_handle, BLEClientService& svc,
107107
uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientCharacteristic* chr[], uint8_t count)
108108
{
109109
// We could found more characteristic than we looking for. Buffer must be large enough
110-
enum { MAX_DISC_CHARS = 4 };
110+
enum { MAX_DISC_CHARS = 8 };
111111

112-
uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t);
112+
// -1 because the first ble_gattc_char_t is built in to ble_gattc_evt_char_disc_rsp_t
113+
uint16_t bufsize = sizeof(ble_gattc_evt_char_disc_rsp_t) + (MAX_DISC_CHARS-1)*sizeof(ble_gattc_char_t);
113114
ble_gattc_evt_char_disc_rsp_t* disc_chr = (ble_gattc_evt_char_disc_rsp_t*) rtos_malloc( bufsize );
114115

115116
uint8_t found = 0;
@@ -129,6 +130,9 @@ uint8_t BLEDiscovery::discoverCharacteristic(uint16_t conn_handle, BLEClientChar
129130
// timeout or has no data (due to GATT Error)
130131
if ( bytecount <= 0 ) break;
131132

133+
// if we truncated the response, adjust the count to match
134+
if ( disc_chr->count > MAX_DISC_CHARS ) disc_chr->count = MAX_DISC_CHARS;
135+
132136
// Look for matched uuid in the discovered list
133137
for(uint8_t d=0 ; d<disc_chr->count; d++)
134138
{

0 commit comments

Comments
 (0)