I have Arduino connected to a HC-06 bluetooth module.
I wan't to send data from some Android-Application I wrote to the Arduino and let Arduino do stuff depending on the received data. So far I'm Sending Integer values between 0 and 1024.
Unfortunately mySerial.read() won't come to an end here. It reads the Data as one big string.
My Modules RX and TX pin are connected to my Arduinos RX- and TX pin. (The console didn't show any data when I connected them to Port 10 and 11, so I thought this is how SoftwareSerial defines the RX and TX Port of Arduino…)
My Sketch:
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
char character;
void setup()
{
// set the data rate for the SoftwareSerial port
mySerial.begin(9600);
}
void loop() // run over and over
{
String Data = "";
while(mySerial.available()) {
character = mySerial.read();
Data.concat(character);
// Serial.println(Data);
Serial.println("foo");
}
This will output:
154888143201270341421501588671750825906975101110241005969922864792711629540451369290213140600
I can't read this data properly and I can't parse the data to some function to handle it, because mySerial.read() won't come to an end. There must be a better way to receive or send this Data.
My Bluetooth Output Stream Source:
public void sendPWM(int progress) {
OutputStream tmpOut = null;
try {
tmpOut = mmSocket.getOutputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("error", "NO OUTPUT STREAM", e);
e.printStackTrace();
return;
}
try {
// byte[] buffer = stringToBytesUTFCustom("1");
byte[] buffer = String.valueOf(progress).getBytes();
tmpOut.write(buffer);
// tmpOut.flush(); /*I have turned this ON and OFF, to see if it
// makes any difference*/
} catch (IOException e) {
// TODO Auto-generated catch block
Log.e("error", "EXCEPTION DURING WRITE", e);
e.printStackTrace();
}
}
I don't close the outputStream, because this will also cancel the connection with the HC-06 and I have to send a lot of data. I can't let the user reconnect to the module each time he sends data. Can you help me out here?
Best Answer