The code below is called in the mainThread, I wanted to update an MPAndroidChart LineChart from a separate thread with data from Arduino HC-06 Bluetooth module but I don't know what happens(because the plot is still populated with mock data) when it enters the thread and why the log doesn't print anything, the last log info that appears is "inside the runnable object".
Keep in mind I am a beginner in Android.
void receiveData(final InputStream inputStream)
{
Log.e("listener", "beginListenForData: Starting listener...." );
handler = new Handler();
stopThread = false;
//buffer = new byte[1024];
new Thread(new Runnable()
{
public void run()
{
int byteCount = 0;
Log.e("runnable", "run: inside the runnable object" );
try {
byteCount = inputStream.available();
} catch (IOException e) {
Log.e("hhhh", "run: no input Stream .................." );
}
if(byteCount > 0)
{
byte[] rawBytes = new byte[byteCount];
try {
int result =inputStream.read(rawBytes);
Log.e("hyyhhy", "run: input Stream contains "+ result+ " bytes." );
} catch (IOException e) {
Log.e("hyyhhy54", "run: input Stream is empty" );
}
String string=null;
try {
string = new String(rawBytes,"UTF-8");
Log.e("ggggggggg", "run: "+ string );
} catch (UnsupportedEncodingException e) {
Log.e("gggggggg11g", "run: String is epty " );
}
if(string!=null){
final StringBuilder builder=new StringBuilder(string);
handler.post(new Runnable() {
public void run()
{
Log.e("gg", "run: run method is called ");
j++;
humDataSet.addEntry(new Entry(j,Float.valueOf(builder.substring(1, 3))));
humDataSet.removeFirst();
tempDataSet.addEntry(new Entry(j,Float.valueOf(builder.substring(1, 3))));
tempDataSet.removeFirst();
tempChart.notifyDataSetChanged(); // let the chart know it's data changed
tempChart.invalidate();
humChart.notifyDataSetChanged();
humChart.invalidate();
Log.e("toctoc", "run: Receiving.." );
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
else
{
Log.e("jhbkl", "run: no data ");
}
}
}
}).start();
}