Send text via bluetooth

Hello everyone!

I'm doing some tests sending and receiving data via bluetooth on ESP32.

When I send a certain string value, the null value is sent with it. So I get "page1ok " instead of "page1ok". Is there a possibility for me to send without the null value? Or just dealing with the receiver's side of the message?

//By Evandro Copercini - 2018
//
//This example creates a bridge between Serial and Classical Bluetooth (SPP)
//and also demonstrate that SerialBT have the same functionalities of a normal Serial

#include "BluetoothSerial.h"

#if !defined(CONFIG_BT_ENABLED) || !defined(CONFIG_BLUEDROID_ENABLED)
#error Bluetooth is not enabled! Please run `make menuconfig` to and enable it
#endif

BluetoothSerial SerialBT;

String msgA;
String msgB;

int tela;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
  tela = 0;
}

void loop() {
  switch (tela)
  {
    case 0:
      Serial.println("Pagina 0");
      if (SerialBT.available() > 0) {
        msgB = "";
        while (SerialBT.available() > 0) {
          char ch = SerialBT.read();
          msgB += ch;
          delay(5);
        }
        Serial.println("Mensagem:");
        Serial.println(msgB);
        Serial.println("Verificando tela...");
        verificaTela();
      }
      break;

    case 1:
      Serial.println("Pagina 1");
      if (SerialBT.available() > 0) {
        msgA = "";
        while (SerialBT.available() > 0) {
          char ch = SerialBT.read();
          msgA += ch;
          delay(5);
        }
        Serial.println("Mensagem:");
        Serial.println(msgA);
        if (msgA.startsWith("page")) {
        msgB = msgA;
        Serial.println("Verificando tela...");
          verificaTela();
        }
      }
      break;
  }
  delay(20);
}



void verificaTela()
{
  Serial.println("TELA:");
  Serial.println(msgB);
  if (msgB == "page1") {
    //sendMessage("page1ok");
    String a = "page1ok";
    SerialBT.println(a);
    tela = 1;
  }
  else if (msgB == "page0") {
    tela = 0;
  }
}


void bluetooth(String a)
{
  if (SerialBT.available() > 0) {
    a = "";
    while (SerialBT.available() > 0) {
      char ch = SerialBT.read();
      a += ch;
      delay(5);
    }
    Serial.println("Mensagem:");
    Serial.println(a);
    Serial.println("Verificando tela...");
    verificaTela();
  }
  delay(20);
}


Thanks!

Does it help if you use print instead of println?

I tried. But if I use print, the message is not received...

Try this to strip the null terminator

String a = "page1ok";
char buf[a.length()];
memcpy(buf, a.c_str(), a.length());
SerialBT.write(buf, a.length());

I use print, the message is not received...

You may need to send a '\n' or some other character which the receiver is expecting.

Returns me the following error:

invalid conversion from 'char*' to 'const uint8_t* {aka const unsigned char*}' [-fpermissive]

So I changed to:

  SerialBT.write((uint8_t *)buf, a.length());

But it still didn't work, the message is not sent...

I can not confirm what you say.

I run this sketch on an ESP32

#include "BluetoothSerial.h"

BluetoothSerial SerialBT;

void setup() {
  Serial.begin(115200);
  SerialBT.begin("ESP32test"); //Bluetooth device name
  Serial.println("The device started, now you can pair it with bluetooth!");
}

void loop() {  
  String a = "page1ok";
  uint8_t buf[a.length()];
  memcpy(buf,a.c_str(),a.length());
  SerialBT.write(buf,a.length());
  SerialBT.println(); 
  delay(1000);
}

The compiler doesn't complain when the buffer is declared as uint8_t.

This is what I see on the screen of the Serial Bluetooth Terminal App is use.

What is receiving the message?

1 Like

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