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!
