NimBLE-Arduino icon indicating copy to clipboard operation
NimBLE-Arduino copied to clipboard

getValue() doesn't work for descriptors in v1.4

Open Hassaan18 opened this issue 3 years ago • 3 comments

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());
    };

Hassaan18 avatar Jul 20 '22 12:07 Hassaan18

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.

h2zero avatar Jul 20 '22 12:07 h2zero

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()); ?

Hassaan18 avatar Jul 20 '22 12:07 Hassaan18

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.

h2zero avatar Jul 20 '22 14:07 h2zero