NimBLE-Arduino: BLERemoteCharacteristic::readValue() does not return the correct value
I would love to get this library running, but I think I found another issue…
I already thought it was my problem so I asked question #19 today, but it is an error of the library.
MVCE:
#include <NimBLEDevice.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("/n/nStarted");
BLEDevice::init("ESP32");
readBLEClients();
}
void readBLEClients() {
// LYWSD03MMC
static BLEClient * pClient = BLEDevice::createClient();
Serial.println("Connecting...");
bool success = pClient->connect(BLEAddress("a4:c1:38:5d:ef:16"));
if(!success) {
BLEDevice::deleteClient(pClient);
Serial.println("Failed to connect");
return;
}
Serial.println("Connected");
BLERemoteService * pRemoteService = pClient->getService(BLEUUID("ebe0ccb0-7a0a-4b0c-8a1a-6ff2997da3a6"));
if(pRemoteService == nullptr) {
Serial.print("Failed to find our service UUID: ");
} else {
Serial.println(" - Found our service");
// Obtain a reference to the characteristic in the service of the remote BLE server.
BLERemoteCharacteristic * pRemoteCharacteristic = pRemoteService->getCharacteristic(BLEUUID("ebe0ccc1-7a0a-4b0c-8a1a-6ff2997da3a6"));
if(pRemoteCharacteristic == nullptr) {
Serial.print("Failed to find our characteristic UUID: ");
} else {
Serial.println(" - Found our characteristic");
if(pRemoteCharacteristic->canRead()) {
Serial.println("Can read");
// Serial.printf("string value is '%s'\n", pRemoteCharacteristic->readValue().c_str());
// Serial.printf("uint8_t value is %u\n", pRemoteCharacteristic->readUInt8());
// Serial.printf("uint16_t value is %u\n", pRemoteCharacteristic->readUInt16());
// const uint8_t * pData = pRemoteCharacteristic->readRawData();
const uint8_t * pData = reinterpret_cast<const uint8_t*>(pRemoteCharacteristic->readValue().c_str());
float temperature = (pData[0] | (pData[1] << 8)) * 0.01; //little endian
uint8_t humidity = pData[2];
Serial.printf("LYWSD03MMC temperature = %.2f : humidity = %u\n", temperature, humidity);
}
if(pRemoteCharacteristic->canNotify()) {
Serial.println("Can notify");
// pRemoteCharacteristic->registerForNotify(notifyCallback);
}
}
}
pClient->disconnect();
BLEDevice::deleteClient(pClient);
}
void loop() {
}
returns 0.00 for temperature and 0 for humidity.
Digging for hours in the library resulted in:
Here the data is still correct. If I add the following line at this exact position:
characteristic->second->m_value = std::string((char*) event->notify_rx.om->om_data, event->notify_rx.om->om_len);
And comment out this line (otherwise the value just sets gets overwritten), the MVCE actually works correctly.
HOWEVER it does not feel good. My gut feeling says that onReadCB should indeed read the value from struct ble_gatt_attr *attr, but that value is not correct. At least the data from event->notify_rx.om in NimBLEClient differs from attr->om in NimbleRemoteCharacteristic.
I am not familiair enough with BLE and the library to solve this, and I am stuck now, any suggestions would be very welcome!.
About this issue
- Original URL
- State: closed
- Created 4 years ago
- Comments: 21 (7 by maintainers)
Hi,
the LYWSD03MMC definitely provides hum/temp via notifications and it is working without any problem with your library. You can check this behavior with every BLE-APP on your phone (i.e. BLE Scanner). In contrast the battery is a readValue and BTW it always gives you 99% as it seems to be hard coded 😉