Arduino-Android Bluetooth handshake


this is my first post, so If I violate any forum rules please forgive me :slight_smile:

  • I’ve developed the app on Android that sends an array of bytes (around 500 elements) over bluetooth to arduino pro micro.
  • Android: User can specify how often send this data. The data is sent in different thread. Also it takes some time (I need to measure that!) to calculate the data before sending it.
  • Arduino: my loop part is divided into two parts by IF clause that checks if Serial is available. First part receives the data and store in arduino array, second part checks if serial is not available (i.e. nothing is being sent) and if the data received is 500 elements - if yes then the data is shown on LCD screen - so not a lot of calculations

I’ve got two problems here.
First one is the minor one: sometimes I need to try couple times to connect with the bluetooth (HC-05). I am not sure here if it is a matter of the app on Android or maybe the quality of the bluetooth device is not so perfect. Any experience here?

Second: sometimes it happens that the received data is shifted, i.e. it looks that arduino/bluetooth stores some old data (from previous sending) and when it has counted that it has received 500 elements then in fact for instance first 100 elements are from the old pack and 400 from new pack.
Maybe small visualisation, instead of receiving {1,2,3,4,5,6} it gets {5,6,1,2,3,4}

It happens when I send the data too often. I dont control if the data has been already sent before starting again the new thread with sending data (it stubbornly sends data for instance every 2 seconds). Also I don’t have any handshake, i.e. implementation in arduino: “If you get all the data and show on the LCD then send back over BT that it is finished, so another pack can be send”.

I guess that my arduino code could be enhanced by sending back that calculations are over (after writing the post I think also that android could control if the previous sending has been over…:)), but any other ideas how to solve that?
I can share my arduino code (but not from this computer so later:)), because I guess this one could cause problems - it looks really simple (IMHO even too simple): one big If… so maybe there is some better option to collect data from bluetooth. Interruptions and another thread? possible to do that?

I have a similar problem. I think a buffer is being overran.
In my case it manifests as Android streaming data through bluetooth at normal pace but after a few seconds Android starts to run slower (I implemented a blinking dot in android as watchdog), and it reflects in Arduino (LCD printing the received data starts changing slower, but apparently data is not lost).
I transmit 7 bytes together, then wait 200ms (more than enough at 9600 bps), then tx again.
On the arduino side, I collect data immediately, identifying packets by “<” and “>” as delimiters, and print them in 16x2 LCD display. I know that “>” is the last character transmitted in every pack because the code is very simple. I also know that arduino code pulls it from the RX FIFO because it identifies de end of packet. That means there’s no garbage accumulated in the buffer. So what’s clogging the streaming after a few packets?

Did you solve your problem?

LCD_BT_Servo.ino (3.12 KB)