Код ниже вызывается в mainThread, я хотел обновить MPAndroidChart LineChart из отдельного потока с данными из модуля Bluetooth Arduino HC-06, но я не знаю, что происходит (потому что график все еще заполнен фиктивными данными) когда он входит в поток и почему журнал ничего не печатает, последняя информация о журнале, которая появляется, находится «внутри работающего объекта».
Имейте в виду, я новичок в 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();
}