I try to create a chat application for android by using a bluetooth conection!For that 2 smartphone 2 arduino AT mega 2560 and on every board i have a bluetooth device!My question is how i store word or sentence in a variable i’ve try with string but it doesnt work i get an error.So to communicate I send a message from my application by bluetooth to the 1st arduino board.Then from the first arduino board i sent it to the 2nd board by a serial communication and from the second board i sent it to the other phone by bluetooth.It only work if i sent number from the smarphone i can receive the same number on the other smartphone but it doesnt work when i sent word or setence.I also upload an image of my application you can download the picture.“Envoyer” means send in english and the message i receive should appear below the send bouton.Ty and sorry for my bad english
byte serialA; //comunication btw the phone and the arduino board
byte serialB; //comunication btw the 2 arduino board
void setup()
{
Serial1.begin(9600); //connection btw the 2 board
Serial.begin(9600); //connection btw arduino and the bluetooth device
}
void loop()
{
serialA = Serial.read(); // read the value sented by the phone
serialB = Serial1.read(); //read the value sented by the arduino board to the 2nd arduino board
Serial1.write(serialA); //Sent to the 2nd arduino the value which i receive from the phone
Serial.write(serialB); //Sent to the phone the value which i receive from the 2nd arduino board
}
You have to either use .available() to see if there is a character in the buffer before reading Serial OR you have to store the value in an 'int' and see if you received -1 which indicates that there were no characters in the buffer:
byte serialA; //comunication btw the phone and the arduino board
byte serialB; //comunication btw the 2 arduino board
void setup() {
Serial1.begin(9600); //connection btw the 2 board
Serial.begin(9600); //connection btw arduino and the bluetooth device
}
void loop() {
if (Serial.available()) {
serialA = Serial.read(); // read the value sented by the phone
Serial1.write(serialA); //Sent to the 2nd arduino the value which i receive from the phone
}
if (Serial1.available()) {
serialB = Serial1.read(); //read the value sented by the arduino board to the 2nd arduino board
Serial.write(serialB); //Sent to the phone the value which i receive from the 2nd arduino board
}
}
OR
// NOTE: These must be 'int' to tell -1 from 'ÿ'
int serialA; //comunication btw the phone and the arduino board
int serialB; //comunication btw the 2 arduino board
void setup() {
Serial1.begin(9600); //connection btw the 2 board
Serial.begin(9600); //connection btw arduino and the bluetooth device
}
void loop() {
serialA = Serial.read(); // read the value sented by the phone
serialB = Serial1.read(); //read the value sented by the arduino board to the 2nd arduino board
if (serialA != -1)
Serial1.write((char)serialA); //Sent to the 2nd arduino the value which i receive from the phone
if (serialB != -1)
Serial.write((char)serialB); //Sent to the phone the value which i receive from the 2nd arduino board
}