0

I am trying to plot data received from Bluetooth with real-time with MPAndroidChart library. I believe there is problem with thread deadlocks but I cannot figure it out exactly.

Here is how the code goes: After connection is established, when "Read" button is pressed.

public boolean onOptionsItemSelected(MenuItem item) {
    Intent serverIntent;
    if (dtToggle.onOptionsItemSelected(item)) {         return true;     }
    switch (item.getItemId())
    {  ......
     case R.id.menu_read:
            String message;
            if (oper_state) {

                oper_state = false;
                put_thread = new PutTask();
                put_thread.start();
                timer = new Timer();
                timer.schedule(new MyTask(),10, 100);
                //feedMultiple();
                //put_thread.start();

                message = CMD_READ + "";
                sendMessage(message);

            } else {
                if(timer != null)
                {

                    message = CMD_STOP + "";
                    sendMessage(message);
                    put_thread.interrupt();
                    timer.cancel();
                    timer.purge();
                    timer = null;
                    oper_state = true;

                    //put_thread.interrupt();
                    try{
                        if(fos != null)
                            fos.close();
                    }
                    catch(Exception e)
                    {

                    }

                }
            }
            return true;

PutThread is where I add received data from bluetooth to data queue:

class PutTask extends Thread {

    public void run(){

        int channel_idx;
        customParsePackets mBluetoothService;

        while(true)
        {

            mBluetoothService = mChestPatchService;

            for(int i=0;i<num_siganl_list;i++)
            {
                if(!mBluetoothService.isEmpty(i))
                {
                    int read_data = mBluetoothService.poll_data(i);
                    put_data(i, read_data);
                    sample_incr(i);
                }
            }

        }
    }
}

Put_data function:

 public synchronized void put_data(int i, int int_data)
{
    int tmp_sample = 0;
    int tmp_data, filtered_data;

    double tmp_diff_val=0;

    tmp_sample = sample_get_value(i);


    tmp_data = int_data;
    if(mECGSignCheck.isChecked())
        tmp_data = -tmp_data;

    if(mFilterCheck.isChecked() == true)
        ecg_data[i] = ECG_bpf[i].getOutputSample(tmp_data);
    else

        ecg_data[i] = tmp_data;
    if(i==1) {
        tmp_diff_val = mRpeak.put_ecg(tmp_sample, tmp_data);
        peak_point = mRpeak.Apply_ecg();
        Log.d("PEAK", "Data pushed" );
    }

    synchronized (chartQueue_Lock){
        if(tmp_sample % 4 == 0)
            **chartQueue[i].offer(new Point(tmp_sample, (int)ecg_data[i]));**
    }

}

And this is where I update my UI:

class MyTask extends TimerTask {

    public void run(){

        runOnUiThread(new Runnable(){
            @Override
            public void run(){
                //updateui();
                dataseries_add_new((int)(Math.random() * 7),0, Math.random() * 500);
            }
        });

    }
}

The above code works perfectly fine for GraphView Library. But I want to implement it for MPAndroidChart. For MPAndroidChart when I start do dataseries_add_new((int)(Math.random() * 7),0, Math.random() * 500); in the above class it works well. But my goal is to add the data from the data queue to the graphs. So in order to do so, in the above code sample instead of dataseries_add_new((int)(Math.random() * 7),0, Math.random() * 500); I call updateui() from where I poll the data from the queue and call dataseries_add_new((int)(Math.random() * 7),0, Math.random() * 500);:

public void updateui()
{
    long starttime, endtime;

    int tmp_sample = sample_get_value(APW_SIGNAL);
    Point qPoint = new Point();

    for(int i=0; i<num_siganl_list; i++)
    {

        int poll_num = 0;

        while(!chartQueue[i].isEmpty())
        {
            if(poll_num != 0)
            {
                if(qPoint.x % 4 == 0) {
                    dataseries_add_new(i, qPoint.x, qPoint.y);
                }
            }

            synchronized (chartQueue_Lock)
            {
                qPoint = chartQueue[i].poll();
            }

            poll_num = 1;
        }

Where the graph update code is taken from MPAndroidChart sample app for Realtime Chart.

When "Read" button is presed app screen goes black for some time and then gives an error. Could you please provide some insight about how to solve the problem?

Alex Bailo
  • 197
  • 2
  • 15
  • You can use an [AsyncTask](http://developer.android.com/reference/android/os/AsyncTask.html) for this, and use the [publishProgress(...)](http://developer.android.com/reference/android/os/AsyncTask.html#publishProgress(Progress...)) to update the UI – Titus Jul 21 '15 at 07:22
  • @Titus AsyncTask for graph update part, or for getting data from the queue and subsequently updating the graph together? – Alex Bailo Jul 21 '15 at 07:39
  • AsyncTask runs operations on a new thread and `publishProgress(...)` it is used to periodically update the UI with the results of those operations. – Titus Jul 21 '15 at 08:04

0 Answers0