//
you're reading...
Android, How-to's

Android Graph of published sensor data

Screenshot_2014-05-05-09-33-30

You can use the Android library GraphView  to display a real time graph of published sensor data. It needs a LinearLayout with an id as part of your layout.

Here is an Android app that creates the bar chart shown above:

package org.lejos.ev3.android.sample.graphsubscribe;

import java.io.IOException;
import java.util.Collection;

import lejos.robotics.SampleProvider;
import lejos.robotics.filter.PublishedSource;
import lejos.robotics.filter.SubscribedProvider;
import lejos.utility.Delay;

import com.jjoe64.graphview.BarGraphView;
import com.jjoe64.graphview.GraphView;
import com.jjoe64.graphview.GraphViewSeries;
import com.jjoe64.graphview.GraphView.GraphViewData;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.LinearLayout;
import android.widget.Toast;

public class MainActivity extends Activity {
  GraphViewSeries rangeSeries;
  GraphView graphView;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    rangeSeries = new GraphViewSeries(new GraphViewData[] {});

    graphView = new BarGraphView(this, "Range readings");
    graphView.addSeries(rangeSeries);

    LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
    layout.addView(graphView);

    graphView.setScrollable(true);

    new Control().execute(rangeSeries);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  private class Control extends AsyncTask {
    private int numSamples = 0;

    protected Long doInBackground(final GraphViewSeries... series) {
      Collection sources = PublishedSource.getSources();

      for (PublishedSource source : sources) {
        if (source.getName().contains("IR")) {
          try {
            SubscribedProvider sp = source.connect();
            final float[] sample = new float[sp.sampleSize()];
            for (numSamples = 0; sp.isActive(); numSamples++) {
              sp.fetchSample(sample, 0);
              runOnUiThread(new Runnable() {
                public void run() {
                  series[0].appendData(new GraphViewData(numSamples + 1,
                      sample[0]), false, 50);
                  graphView.redrawAll();
                }
              });
              Delay.msDelay((long) (1000 / source.getFrequency()));
            }
          } catch (IOException e) {
            // ignore
          }
          try {
            source.close();
          } catch (IOException e) {
          }
          break;
        }
      }
      return (long) numSamples;
    }

    protected void onPostExecute(Long result) {
      Toast.makeText(MainActivity.this, "Number of samples: " + result,
          Toast.LENGTH_LONG).show();
    }
  }

}
Advertisements

Discussion

No comments yet.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

About leJOS News

leJOS News keeps you up-to-date with leJOS. It features latest news, explains cool features, shows advanced techniques and highlights amazing projects. Be sure to subscribe to leJOS News and never miss an article again. Best of all, subscription is free!
Follow leJOS News on WordPress.com
%d bloggers like this: