NimBLE-Arduino
NimBLE-Arduino copied to clipboard
getValue() doesn't work for descriptors in v1.4
I'm facing the following error while using version 1.4.0 in the onwrite callback.
src/bluetooth.cpp: In member function 'virtual void DescriptorCallbacks::onWrite(NimBLEDescriptor*)':
src/bluetooth.cpp:159:57: error: invalid cast from type 'NimBLEAttValue' to type 'char*'
std::string dscVal((char*)pDescriptor->getValue(), pDescriptor->getLength());
^
*** [.pio\build\esp32doit-devkit-v1\src\bluetooth.cpp.o] Error 1
The function is defined as
void onWrite(NimBLEDescriptor* pDescriptor) {
std::string dscVal((char*)pDescriptor->getValue(), pDescriptor->getLength());
log_d("Descriptor witten value:%s", dscVal.c_str());
};
Sorry, I neglected to document that change. You can simply use std::string val = pDescriptor->getValue(); now, just like characteristics. You could also use the Arduino String as well if you prefer.
So, you mean I don't need to cast it to char array and can write it as std::string dscVal((pDescriptor->getValue(), pDescriptor->getLength()); ?
Yes, you do not need to cast it. The getValue call now returns a NimBLEAttValue now, which has templates to use it as a std::string or String or a vector etc.