Esp32 Bluetooth buffer issue

Hi Guys,
i don´t know if my post belongs here since its about an esp32 but I hope its ok anyway.
My current issue is that I need to do a lot of exclusions in my if´s and i dont know why...

#include <BluetoothSerial.h>

BluetoothSerial SerialBT;

uint8_t cmd, cmdOld;
bool animations = true;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("Esp32");
  Serial.println("The device started, now you can pair it with bluetooth!");
  while(!SerialBT.hasClient()){};
  Serial.println("Device connected");
}

void loop() {
  handleBT();
  delay(20);
  
}

void handleBT() {
  if (SerialBT.available()) {
    cmd = SerialBT.read();
  }
  if (cmd != 0 && cmd != 10 && cmd != 13) {  ---------Dont know why i need to exclude 10 and 13-------
    handleCommand(cmd);
    cmd = 0;
  }
}

void sendBT(uint8_t message) {
  SerialBT.write(message);
}

void handleCommand(uint8_t com) {
  Serial.printf("HANDLING %02D\n", com);

  switch (com) {
    case 1:
      Serial.printf("case %02D\n", com);
      changeColor();
      break;
    case 2:
      Serial.printf("case %02D\n", com);
      toggleAnimation();
      break;
    case 3:
      Serial.printf("case %02D\n", com);
      break;
  }
}
void toggleAnimation(){
  animations = !animations;
  Serial.print("Animations: ");
  Serial.println(animations);
}

void changeColor() {
  while (true) {
    if (SerialBT.available()) {
      cmd = SerialBT.read();
    }
    if (cmd != cmdOld && cmd != 10 && cmd != 13) {      ---------Same here----------
      Serial.println(cmd);
      cmdOld = cmd;
    }
    if (cmd == 2) {
      break;
    }
  }
}

I have marked 2 situations in the code.
I think its because of the software that i use on my phone allways send an \n with the message but i dont know how to remove that.

The app that I am using

Thanks for your time in advance

regards
Wilgelmy

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