I can't send two datas at the same time via Bluetooth

Hello guys. I am trying to make an app that can command the movement of a car and receive the distance via Bluetooth from Arduino. Its commands works properly when I use the app only to command the movement. Could you help me with this problem, please?



1 Like

We will need to see your source code on the other device.
Also see FAQ Section: Bluetooth < version 4 (BlueTooth Client) and Arduino

1 Like




1 Like

Please see the Delimiter article in FAQ

Be sure to set the Delimiter attribute of the BlueTooth Client component to 10 in the Designer
or at connect time.

Be sure to use println() at the end of each message to send from the sending device, to signal end of message. Do not rely on timing for this, which is unreliable.

Also, return data is not immediately available after sending a request,
you have to start a Clock Timer repeating and watch for its arrival in the Clock Timer event. The repeat rate of the Clock Timer should be faster than the transmission rate in the sending device, to not flood the AI2 buffers.

In your Clock Timer, you should check
Is the BlueTooth Client still Connected?
Is Bytes Available > 0?
IF Bytes Available > 0 THEN
set message var to BT.ReceiveText(-1)

This takes advantage of a special case in the ReceiveText block:

ReceiveText(numberOfBytes)
Receive text from the connected Bluetooth device. If numberOfBytes is less than 0, read until a delimiter byte value is received.

1 Like

You could experiment with two BT Clients, one to send, one to receive. I have never done this myself but it wouldn’t harm to try.

1 Like

Hi there,

I had a look at the Arduino Code, I think there are a number of changes required:

  • at the begin of the loop “Serial” is checked and read instead of BTSerial (BTSerial.available() and BTSerial.read() )
  • I am not clear what the idea of the last two commands in the “L” - part is: (delay(1500); & state=3;)
  • what is the idea of the variable “flag” ?
1 Like

Tip: Do not use Delay() - everything is delayed. Instead, use milliseconds elapsed.
A basic example:
// include the library code:
#include <SoftwareSerial.h>

//vars
unsigned int igUpdateTime;

void setup()
{
Serial.begin(9600);
igUpdateTime = millis();
}

void loop()
{
if(millis() - igUpdateTime > 2000) //Loop approx every 2 seconds
{
igUpdateTime = millis();

                     if (btSerial.connected())
                     {
                               //Bluetooth to App
                               Serial.print("Hello");
                               Serial.print("|");
                               Serial.print("World");
                               Serial.print("|");
                               Serial.println();        //This last line tells App "End of Data" = Ascii LineFeed Char Number 10
                     }
           }

}

2 Likes

I found a cod and it was including flag , state. However I deleted them. I replaced Serial with BTserial and it didn’t work. However, thank you for the support.

Did you get my basic Sketch to work? Sorry, in the If statement it should read:

if (Serial.connected())