bluetooth comunications

Hi all,
I’m new on forum and i want to know something more about bluetooth comunications.

I have HC-05 module. I want to send and recive data via android phone. But i don’t have base
knowledge about bluetooth protocol and I seaching informations about this.

  1. If i set Serial to 9600 arduino send 9600 bit per second to mobile phone and mobilephone and on the other side the same (mobilephone–>>arduino) Yes ?

  2. When i want to send message “Hello” from arduino to phone how aplication on android knows where is the begin and the end of this message if arduino still sending the other data 9600 bit per second.

3)If i send some informations to arduino and i want feedback message to phone i must use some delay between sending and reciving command in android program ?

This is my android and arduino code :

void loop() {
if(Serial1.available() > 0){
state = Serial1.read();
if (state == ‘1’) {
Serial1.print(49);
}

else if (state == ‘2’) {
Serial1.print(50);

}
}

state=0;
}

Without delay between sending and reciving command i must click same button twice to change value 50<->49. When I added delay this issuse gone but when i wait ~5, 10 sec and i press button, first i have nothing in text label, by next click i have 5050 or 5049 or 4950 value . Why ? I know this is problem with understanding the bluetooth protocol and in the same time phone recive two message. But i want to understand this. Maybe you have some links with tutorials, instructions, anything where i can understand this ;]

Thanks for help.

  1. If i set Serial to 9600 arduino send 9600 bit per second to mobile phone and mobilephone and on the other side the same (mobilephone–>>arduino) Yes ?

Yes. Keep in mind that the 9600 bits includes start and stop bits between bytes, so 9600 baud is 960 bytes per second (assuming one start and one stop bit, which is the default).

  1. When i want to send message “Hello” from arduino to phone how aplication on android knows where is the begin and the end of this message if arduino still sending the other data 9600 bit per second.

If all you send is “hello”, the phone doesn’t know. You need to send data in packets, with start and end markers, so the phone can tell where a packet starts and ends.

If you are sending temperature data, humidity data, and useless messages, you might send "<T:20.7> <H:93.0> “<U:Hey, cutie>”. The < and > delimit the packets. The T, H, and U define the kind of data being sent. The data is, obviously, the rest of the packet.

3)If i send some informations to arduino and i want feedback message to phone i must use some delay between sending and reciving command in android program ?

No. Serial communication is asynchronous. Trying to force it into a synchronous mold, using delay, is just an exercise in frustration. Understand that communication is asynchronous, and design the packages being exchanged so each end knows what is a request and what is a reply. Design the reply messages so that it is clear what kind of request might have generated such a reply.

For instance, “<What time is it?>” is clearly a request. “<The temperature is 76 degrees!>” is clearly a response, and you have a pretty good idea what the question was. (If not, here’s a hint. The response is not a reply to the example request.)

Without delay between sending and reciving command i must click same button twice to change value 50<->49.

No. There is something wrong with the phone code if it needs a delay.

jarossis:
I have HC-05 module. I want to send and recive data via android phone.

Do you really need to do all this stuff? If you were doing something very sophisticated, there is a faint possibility that you do, but it appears that you aren't -you are just wanting to send and receive plain-vanilla data and text. You may be getting deeply involved for the intellectual exercise, but it looks like you are trying to re-invent the wheel.

The picture is unreadable, but that probably doesn't matter.

If you want to send data, you can print it to serial, just like you would to the serial monitor - no change in code required. If you want a command from Android, you just get Arduino to listen for it.

You might find the following background notes useful

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

Maybe your problem is really an Android problem but the above might at least get you started with bluetooth and may be all you need. if you have an Android problem after that, well, it's an Android problem.

Guys. Thanks for answears. Could You send me a short code with “<” and “>” including sending and reciving packets?

I don’t know what you mean by “<” and “>” or by packets. Maybe you are talking Android here, not Arduino.

jarossis:
Guys. Thanks for answears. Could You send me a short code with “<” and “>” including sending and reciving packets?

Have you seen https://forum.arduino.cc/index.php?topic=288234.0? If not, it’s time that you did.