toniux
toniux

Reputation: 31

Why am I seeing unexpected data at the beginning of the Android app when sending Arduino data via Bluetooth?

I am working on a group project where we are sending serial data over Bluetooth from Arduino to Android. We are all fairly new at both Arduino and Android.

Hardware used include Arduino Uno R3 and HC-05 bluetooth module.

I am sending dummy data for a 3 axis accelerometer packet and successfully read the packet data from Android.

However, we have this blob of data (about 50+ bytes usually and has ranged up to 512 bytes) that always gets sent to the app in the beginning. Its a randomly sized chunk of bytes, which we can't interpret because it doesn't seem to match the packet format we set up for our data. We managed to avoid looking at this byte chunk by checking to see if the packet size is small enough. But this adds a lot of overhead (4 - 5 seconds), so we'd like to figure out what this blob of data is. So, does the HC-05 send some proprietary Bluetooth related data first or is there some thing wrong with my script that's causing the unexpected data to be sent?

This is the Arduino code.

#include <SoftwareSerial.h>
SoftwareSerial bluetooth(10,11);

void setup(){
  bluetooth.begin(9600);
}

void loop() {
  int x = random(360);
  int y = random(360);
  int z = random(360);
  formAccelerometerPacket(x, y, z); 
  delay(5000); // wait 5 sec
}

void formAccelerometerPacket(int xVal, int yVal, int zVal) {
  printSensorVal('A', xVal); 
  printSensorVal(':', yVal); 
  printSensorVal(':', zVal); 
}

void printSensorVal(char flag, int sensorVal) {
  bluetooth.print(flag);
  bluetooth.print(sensorVal);
}

I've looked at it with a Bluetooth terminal app but nothing looks wrong from there. Its LogCat from the app that shows this content received from the app, but I can't interpret it as I said earlier, which is what I need to solve.

I've tried to look at other SO questions but none others could help me.

I don't have the code for the Android app as it is with another teammate, but I know that they followed the BluetoothChat example closely.

The only thought I had was that since Arduino loops the data, if the app starts after the Arduino starts, it might start reading some data part way from what was going on in the serial port before. But it doesn't explain the size difference in the blob of bytes.


Edit on 08/21/2014 at 10:33AM PST

Here is a screenshot of the LogCat. What we did was ran the Android app first and then I started the Arduino to make sure the board didn't have old data. Looking at this makes me think it might be a pairing issue as some one suggested. I am working on trying that fix.

logCat Output

Upvotes: 3

Views: 976

Answers (2)

Riccardo Pretolesi
Riccardo Pretolesi

Reputation: 23

I am not sure that is your case but may be it's usefull. When you send a data from HC-05(FC-114) to a Slave(HC-06) the first byte(or the first three/four) is sent immediatly and the rest with a delay of 5/10ms. I don't know why, but i see it using oscilloscope. If well managed, you can fix the problem when receive the packet of byte waiting for a while, otherwise, you can get crazy for understand what is happening.

Upvotes: 0

Theoneil Steenkamp
Theoneil Steenkamp

Reputation: 21

Try Bluetooth SPP on Google Play, then connect to the HC-05. Check the output and then once you get clean data reset the arduino and see what happens. That's how I usually go about checking the output from my HC-05. And no there is nothing sent by the HC-05 when it starts up. I couldn't comment so had to post an answer, sorry.

Upvotes: 1

Related Questions