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

Android Graph of published sensor data


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;

  protected void onCreate(Bundle savedInstanceState) {

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

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

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


    new Control().execute(rangeSeries);

  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);
              Delay.msDelay((long) (1000 / source.getFrequency()));
          } catch (IOException e) {
            // ignore
          try {
          } catch (IOException e) {
      return (long) numSamples;

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



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 )

Google+ photo

You are commenting using your Google+ 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 )


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: