Receiving data from HC-06 on Android

I am building an android application that opens a thread that receives data on a regular rate (every second) and can send commands back to the module.

I have programmed my application so far that I am able to send strings to the module and they arrive correctly, as ASCII strings.

However, sending data to my application through the HC-06 module and receiving it correctly in the application seems to be a problem. I write the data that I want to send to the module, and once it arrives in my application, the data seems to be corrupt.

I am wondering if the HC-06 maybe encodes the data, which I then have to decode in my application.

Any help would be appreciated,
Thanks :smiley:

My Arduino code:

#include <ArduinoJson.h>
#include <Adafruit_BMP085.h>
#include <SoftwareSerial.h>
#include "TotalRGB.h"
#include <Servo.h>

Adafruit_BMP085 bmp;
CTotalRGB rgbControl(13, 12, 11);
//Servo servo;

SoftwareSerial mySerial(0, 1); // RX, TX

void setup() {
  rgbControl.SetColor(PURPLE);
  bmp.begin();  
  
  pinMode(10, OUTPUT);
  // servo.attach(9);
}

void loop() {
  CheckSensors();

  if (mySerial.available()) {
    rgbControl.SetColor(BLUE);
    mySerial.read();
    //TODO: If a certain character comes in, start the launch sequence
  }

  SerialFlush();  
  SendData("EngineTemperature", bmp.readTemperature(), 0);
  mySerial.print("\r\n");
  delay(1000);
}

void SerialFlush(){
  while(mySerial.available() > 0) {
    char t = mySerial.read();
  }
}

void SendData(const String type, const float value, const long collectionTime){
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data  = jsonBuffer.createObject();
  data["type"] = type;
  data["value"] = value;
  data["collectionTime"] = collectionTime;
  data.printTo(mySerial);
}

void CheckSensors(){
  while(!bmp.begin()){
    rgbControl.Blink(BLACK, RED, 100);
    digitalWrite(10, HIGH); // send high signal to buzzer 
  }
  digitalWrite(10, LOW);
  rgbControl.SetColor(GREEN);
}

Android thread snippet:

if(_dataInputStream != null){
                    int bytesAvailable = _dataInputStream.available();
                    if(bytesAvailable > 0)
                    {
                        byte[] packetBytes = new byte[bytesAvailable];
                        _dataInputStream.read(packetBytes);
                        for(int i = 0; i < packetBytes.length; i++)
                        {
                            byte byteData = packetBytes[i];
                            if(readBufferPosition + 1 > readBuffer.length){
                                break;
                            }

                            readBuffer[readBufferPosition++] = byteData;
                        }
                        _rocketDataCallback.onRocketDataReceived(new String(readBuffer, "ASCII"));
                    }
                }

felix-seip:
I am wondering if the HC-06 maybe encodes the data,

No, it doesn't. It just receives, and passes along, data the Arduino sends to the COM port.

The most obvious problems you have is that you are attempting to use software serial on the hardware serial pins 0,1 which is usually fatal. If you just use serial, delete all reference to software serial, and leave bluetooth where it is, whatever you are doing stands a chance of working. Just ensure bluetooth is disconnected when you upload the edited programme.

You have also omitted the serial.begin command, which means you had no chance of it working, irrespective of the type of serial you used.

It is a fair bet that there are other errors and omissions and so you might find the following background notes useful.

http://homepages.ihug.com.au/~npyner/Arduino/GUIDE_2BT.pdf
http://homepages.ihug.com.au/~npyner/Arduino/BT_2_WAY.ino

So it would look something like this?

#include <ArduinoJson.h>
#include <Adafruit_BMP085.h>
#include "TotalRGB.h"
#include <Servo.h>

Adafruit_BMP085 bmp;
CTotalRGB rgbControl(13, 12, 11);
//Servo servo;

void setup() {
  rgbControl.SetColor(PURPLE);
  bmp.begin();  
  Serial.begin(9600);
  pinMode(10, OUTPUT);
  // servo.attach(9);
}

void loop() {
  CheckSensors();

  if (Serial.available()) {
    rgbControl.SetColor(BLUE);
    Serial.read();
    //TODO: If a certain character comes in, start the launch sequence
  }

  SerialFlush();  
  //SendData("EngineTemperature", bmp.readTemperature(), 0);
  
  String message = "Hello World";
  Serial.write(message);

  delay(1000);
}

void SerialFlush(){
  while(Serial.available() > 0) {
    char t = Serial.read();
  }
}

void SendData(const String type, const float value, const long collectionTime){
  StaticJsonBuffer<200> jsonBuffer;
  JsonObject& data  = jsonBuffer.createObject();
  data["type"] = type;
  data["value"] = value;
  data["collectionTime"] = collectionTime;
  
  data.printTo(Serial);
}

void CheckSensors(){
  while(!bmp.begin()){
    rgbControl.Blink(BLACK, RED, 100);
    digitalWrite(10, HIGH); // send high signal to buzzer 
  }
  digitalWrite(10, LOW);
  rgbControl.SetColor(GREEN);
}

I have no idea about what you are doing, but at least you have

..........
  Serial.begin(9600);
.........

which is essential. The serial.flush is probably harmless, but very probably redundant.
I suggest you just get familiar with basic luetooth communications before you get onto all that other stuff. Using bluetooth is just as much a matter of procedure as it is of programming.