MediaTek-Labs / Arduino-Add-On-for-LinkIt-SDK

Arduino board support package for LinkIt 7697
https://docs.labs.mediatek.com/resource/linkit7697-arduino/en
34 stars 33 forks source link

LBLECentral read/write characteristic occasionally fails #82

Closed pablosun closed 6 years ago

pablosun commented 6 years ago

In the following code snippet, the read operation succeeds, while the write operation fails - even the UUID attribute is indeed having a WRITABLE permission.

     char a = client.readCharacteristicInt("00010203-0405-0607-0809-0A0B0C0D2B11"); 
     // write  
     int mm = client.writeCharacteristicInt("00010203-0405-0607-0809-0A0B0C0D2B11",6);
pablosun commented 6 years ago

The symptom here is that LBLEClient::writeCharacteristic may send the BLE write request with incorrect handle when the services have both 16-bit UUID and 128-bit UUID services.

This issue does not happen when there are only 16-bit UUIDs, e.g. the Example sketch.

pablosun commented 6 years ago

The root cause here is that LBLEClient stores all the scanned UUID and corresponding value handle in a std::map table, which relies on less(operator <) comparison to properly order the elements.

However, the implementation of LBLEUuid does not return proper ordering when comparing 16-bit UUID with 128-bit UUID, and vice versa. Therefore writeCharacteristic may retrieve wrong value handle when read/write the attribute.