0

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();
}
st4hoo
  • 2,196
  • 17
  • 25
RABI Hamza
  • 106
  • 8
  • How are you filtering these logs? They all have different tags... – TheWanderer Sep 20 '18 at 19:47
  • 1
    Format the code correctly, please – Josh Sep 20 '18 at 19:49
  • As I don't know the implementation of Log, does it have something like a flush() method to write immediatly to a file ? – JFPicard Sep 20 '18 at 19:59
  • ok, is it readable now? – RABI Hamza Sep 20 '18 at 20:29
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Sep 21 '18 at 08:16
  • @RabieHamza it is worthless to remove your Log statements that you refer to in you question. Readable code means the indentation of the lines is correct. Furthermore the Log statement should say something "gggggg" is odd for everybody. Plus you should remove any unnecessary code. Reduce the code to a minimal version that has the same effect. – Christian Sep 21 '18 at 08:40
  • the reason why It has tags like "ggggg" is because i wrote the code under seriously urgent conditions so I didn't want to waste time organizing the whole code though it seems like this is the real problem because it's hard to debug such code, I am sorry for any inappropriate statements i have made that are mainly a language problem, thanks for your help. – RABI Hamza Sep 22 '18 at 00:34
  • Also I deleted these log instructions because after I had calmed down I figured out that It was embarrassing to write such pieces of code. – RABI Hamza Sep 22 '18 at 00:43

0 Answers0