Hello!
I was working on a project with a ESP32 Lolin32 lite where I'm using a phone connected to the esp by bt to send some data and then based on what the ESP32 received it will send other data to another BT phone. I couldn't find many info online so I was wondering if it's even possible.
Thank you
After hours of research I found this code here:
#include <BluetoothSerial.h>
BluetoothSerial SerialBT;
char receivedChars[256]; // Array to store received data
boolean newData = false; // Flag to indicate new data received
void setup() {
Serial.begin(115200);
SerialBT.begin("ESP32_BT"); // Set Bluetooth name
Serial.println("Waiting for Bluetooth connection...");
}
void loop() {
if (SerialBT.available()) {
char c = SerialBT.read();
if (c == '\n') { // End of transmission character
receivedChars[SerialBT.available()] = '\0'; // Add null terminator
newData = true; // Set flag to indicate new data received
} else {
// Append character to receivedChars array
strncat(receivedChars, &c, 1);
}
}
if (newData) {
Serial.println("Received data: " + String(receivedChars));
// Process the received data if needed
// Send the received data to another phone via Bluetooth
sendBluetoothData(receivedChars);
// Reset the receivedChars array and flag for new data
memset(receivedChars, 0, sizeof(receivedChars));
newData = false;
}
}
void sendBluetoothData(const char* data) {
// Connect to the other phone via Bluetooth
BluetoothSerial bt;
bt.begin("OTHER_PHONE_BT"); // Set Bluetooth name of the other phone
// Send the data to the other phone
bt.println(data);
// Disconnect from the other phone
bt.end();
}
But I noticed that at the end it disconnects from the second BT device. Is it a problem if I let it connected? Because this is a moving robot and the second device will be on it so that when I receive the data from the esp, using a text to speech tool I can hear the answer, so if I can't let it connected I'll have to take the tablet, reconnect it again and put it back on the robot which is not the best thing in the world
This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.