I am using a bluetooth serial connection to send data from Arduino to Android. This is the code that I use in my arduino to send data:
char toSend = (char)Serial.read();
bluetooth.print(toSend);
and this code on my Android application to read this data:
void inputthread() {
final Handler handler = new Handler();
stopWorker = false;
workerThread = new Thread(new Runnable()
{
public void run()
{
while(!Thread.currentThread().isInterrupted() && !stopWorker)
{
try
{
int bytesAvailable = mmInputStream.available();
if(bytesAvailable > 0)
{
byte[] packetBytes = new byte[bytesAvailable];
mmInputStream.read(packetBytes);
final String s = new String(packetBytes);
final String t = new String("s");
if (s==t)
{
handler.post(new Runnable()
{
public void run()
{
try {
closeBT();
}
catch (IOException ex) { }
}
});
}
handler.post(new Runnable()
{
public void run()
{
// txtaggiorna.setText(s);
}
});
}
} catch (IOException ex)
{
stopWorker = true;
}
}
}
});
When I send "Ciao" from Arduino serial monitor, Android shows "C" first and then "iao", but I don't know why.