ESP32 BluetoothSerial cannot read string

I try to read string from ESP32 bluetooth like this code.

  if (SerialBT.available()) {

    Serial.println(SerialBT.readString());
    String pass = SerialBT.readString();
    
    if(pass == "1234"){
      SerialBT.println("Password correct!");
    }else{
      SerialBT.println("Password wrong!");
      Serial.println(pass);
    }
  }

In serial monitor this line can show output is 1234.

Serial.println(SerialBT.readString());

In bluetooth terminal app it show

Password wrong!

And this line not show anything.

Serial.println(pass);

How to read string from bluetooth ?

Serial.println(SerialBT.readString());
String pass = SerialBT.readString();

You can not read the serial buffer twice and expect to see the same data. The buffer is emptied after the first read.

Try

String pass = SerialBT.readString();
Serial.println(pass);

It's work. Thank you very much.

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