Arduino --> Android bluetooth communication (receive text with App Inventor)

user2756724 picture user2756724 · May 1, 2015 · Viewed 15.7k times · Source

I'm creating an Arduino based drone that can be controlled through an Android application. In order to improve the user experience, I'd like to show the accelerometer/compass sensor's values on the application, so I need to send them from Arduino to Android, via Bluetooth. The values are simple integer number between 0 and 180.

The best solution I thought is to concatenate all the values (separated with a comma) in one string, and send it to the app, that will separate the single values (the string will be sent only when the app require it, in this case when a 'z' byte is received by Arduino).

if (Serial.available() > 0) {
    if (Serial.read()=='z'){
        Serial.println(String((int)sensor1) + ',' + String((int)sensor2) + ',' + String((int)sensor3));
    }  
 }

Here are the App Inventor blocks: App Inventor pseudo-code

It seems that the values are being received quite well, but there is a critical issue: somethimes the string is not received well, and that cause a lot of errors. Sometimes the received string is (for example) 10,10,10, but somethimes it is 10,10,1010 or just 10,10 ecc...

I also tried to send the values one by one, but the result was nearly the same. I even tried to set 'numberOfBytes' to -1, using a delimiter byte, but this also was not succesful unfortunately.

I getting quite mad, so I hope there is another way to send thoose integers to Android, or to fix the system I'm already using.

Answer

Andy Droid picture Andy Droid · Oct 13, 2018

I used Serial.print to send each result and then used Serial.write('>'); as the end marker.

In appinventor designer window set the Delimiter byte for Bluetooth client to 62 (the ASCII value for the > character ).

In the blocks window, use Bluetooth cliant1.Receive text and set number of bytes to -1

App invented will then read until a delimiter is found.

However it will cause the app to hang if it doesn't find one.