How a character is converted to a string. How to read data such as Esp32, bluetooth low energy Serial.readString ()

class MyCallbacks: public BLECharacteristicCallbacks {
void onWrite(BLECharacteristic *pCharacteristic) {
std::string rxValue = pCharacteristic->getValue();
if (rxValue.length() > 0) {

    for (int i = 0; i < rxValue.length(); i++) {

    if (mystring == "on") { 
      Serial.println("Turning ON!");

Based on what you have posted I don't know what you are asking.

For advice on asking a good question, one likely to get a helpful answer read: How to get the best out of this forum

For advice on dealing with serial communications read: Serial Input Basics - updated

The data example I sent from the phone is "role on". I read this data from esp32.

n ".
I want to convert this data to String data
namely "role on"

only the last character comes from the loop.
I want to save all the characters of their data in a string variable

mystring += rxValue[i];

Your code assumes that all your data is received in one go; that is more than likely not the case. To make debugging easier, you should not print anonymously. Add additional stuff so you see what is being printed

  for (int i = 0; i < rxValue.length(); i++) {
      Serial.print("rValue["); Serial.print(i); Serial.print("] = '"); Serial.print(rxValue[i]); Serial.println("'");
      mystring += rxValue[i];
  Serial.print("mystring = '"); Serial.print(mystring); Serial.println("'");

This will show you in a better way what is happening; also note the use of single ticks around the printing so you can identify empty Strings, whitespaces and so on.

Code not tested.

Thanks a lot for your help it worked :smiley:

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