Casting BLECharacteristic to BLEBoolCharacteristic. How do I get a BLEBoolCharactertistic value()

I have my ArduinoBLE program mostly working but I am struggling to read the value of a BLEBoolCharacteristic in the eventHandler. My pointer knowledge is only suspect so that may be the issue. The definitions and the eventhandler are shown below.

//Definition of boolean characteristic
BLEBoolCharacteristic runmodeChar(BLE_UUID_RUNMODE,BLERead | BLEWrite | BLENotify);

// Add eventhandler.  
runmodeChar.setEventHandler(BLEWritten, writeHandler);

//Definition of eventhandler
void writeHandler(BLEDevice central, BLECharacteristic characteristic){
  //Event Handler passes a generic BLECharacteristic
 
//  *** This is my problem.  It always prints "runmode set to TRUE"
// *** even when I have set the boolean characteristic value =0
  if (characteristic.value())
    {
      //If True -> in Run Mode
      Serial.println("runmode set to TRUE");
    } else
    {
      Serial.println("runmode set to FALSE");
    }
}

So I was thinking, do I need to cast the BLECharacteristic to a BLEBoolCharacteristic inside the eventHandler hand then retrieve it's value?

newChar=(BLEBoolCharacteristic) characteristic
//Now use the value property to get a true or false.
if(newChar.value() ){
     //Would I get a true now?
}else{
     //would I get a false now?
}

Ultimately, in the eventHandler I want to read the value of my BLEBoolCharacteristic and make a decision accordingly, but for some reason I cannot. In other parts of the program when I have type BLEBoolCharacteristic I can read the value() directly and I don't have the same issue.

Thank You

void writeHandler(BLEDevice central, BLECharacteristic characteristic)

I think the writeHandler needs to be specific. Try

void writeHandler(BLEDevice central, BLECharacteristic runmodeChar ){

I'm not totally clear if the reading of the value is correct, and this may be better

void writeHandler(BLEDevice central, BLECharacteristic runmodeChar)
{
  //if (characteristic.value())
  if (runmodeChar.written())  //this may always be true because of the handler?
  {
    bool reading = runmodeChar.value();
    if (reading)
    {
      //If True -> in Run Mode
      Serial.println("runmode set to TRUE");
    }
    else
    {
      Serial.println("runmode set to FALSE");
    }
  }
}

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.