I am working on an application in android. And I am trying to update data to UI from the background thread. However, it doesn't work for some reasons. Can any one splot the error or suggest any better solution? I used Thread to read data from the bluetooth socket and update the UI with RunOntheUI. Here is the code:
socket = belt.createRfcommSocketToServiceRecord(UUID_SECURE);
socket.connect();
stream = socket.getInputStream();
// final int intch;
Thread timer2 = new Thread() { // Threads - do multiple things
public void run() {
try {
// read data from input stream if the end has not been reached
while ((intch = stream.read()) != -1) {
byte ch = (byte) intch;
b1.append(ByteToHexString(ch) +"/");
decoder.Decode(b1.toString());
runOnUiThread(new Runnable() {
public void run()
{
// update the uI
hRMonitor.SetHeartRateValue((int) decoder.GetHeartRate());
}
});
Thread.sleep(1000);
}
} catch (Exception e) {
e.printStackTrace();
}
}};
timer2.start();