Here is your most recently posted code section of relevance:
void loop()
{
//sends distance reading to mobile via BT
getdistance();
Serial.println(distance,0);
//test relay UP
state = Serial.read(); // Reads the data from the serial port
if ( state== '0')
{
digitalWrite(relay1Pin, HIGH);
delay(500);
}
else if (state == '1')
{
...
My C is weak. Could you tell us what that ,0 in Serial.println(distance,0); is for?
It is to trim fractions off distance, or is it to pad your output messages?
(If in doubt, rip it out)
Did you see the tool tip for that block yet?
Since you have 10 (New Line) as your Delimiter, that will return exactly one message (or wait until there is one, which you don't want but that's okay since your request is guarded by that test to insure there are bytes available so you will never wait here.)
By The Way, your Clock Timer is way too slow, and will result in a growing backlog:
To avoid a message backlog piling up in the AI2 BT buffer, ask for data twice as often as it will arrive. I suggest maybe 75 ms since you delay(150) in your .ino file. Don't worry about running the Timer too fast, since it's okay for it to come up empty. As a precaution, show the BytesAvailable number in a Label, to warn you if you get a backlog.
First, make sure it is clean (no commentary from the sending device, just exactly one number or something you can parse easily like a JSON tag:value pair.)
AI2 uses Duck Typing, so don't worry about text to number conversion as long as you use a math (blue) comparison block and the incoming text reads like a number (No Hello World garbage)
Here is an updated blocks sample illustrating these ideas ...
BlueTooth_delimiter_sample.aia (3.4 KB)