The Android Accelerometer

December 7, 2010 by · 2 Comments 

I had occasion to play with the Android accelerometer recently.  It’s amazing how well instrumented smart phone’s are these days.  Compared to the GPS, the accelerometer inputs are remarkably easy to use. I thought that was pretty cool so I decided a brief post may be in order to return to technical topics…and I was not lazy and actually used syntax highlighting this time. No need to thank me.

First, make a private SensorManager member of your Activity class.  While you’re at it, make a Sensor and a List<Sensor> member which you’ll later use to ask the system what sensors it has (more on this later).

private SensorManager sensorManager;
private List sensors;
private Sensor accSensor;

Moving along, in the onCreate() method for your activity, go ahead and initialize it by calling getSystemService().  Once you do so you’ll call getSensorList() and check to see if you get anything.  If so, grab the first element in that list.  That’s your accelerometer!

// set the sensor manager to read the accelerometers
sensorManager = (SensorManager)getSystemService(Context.SENSOR_SERVICE);
sensors = sensorManager.getSensorList(Sensor.TYPE_ACCELEROMETER);
if(sensors.size() > 0) {
    accelerometerSensor = sensors.get(0);
} else {
    // TODO - Houston we have a problem!  No accelerometers!
}

Ok, now the fun starts. as with most things in the Java/Android world you’ll now need to implement a listener to get the sensor updates.

 private final SensorEventListener mySensorListener = new SensorEventListener() {
    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        float x = event.values[0];
        float y = event.values[1];
        float z = event.values[2];
    }
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }
};

As soon as you declare the function it will offer to implement the required overrides. Just take the results, display them on a text view and you’re all set. The units are, conveniently, m/s^2. So, if you put the device on a stable flat surface you should be seeing 9.8 m/s^2 in the vertical direction. This assumes, of course, that you are on planet Earth.

Last, but not least, it’s good practice to resume and dismiss your listener when the application state changes like so.

    @Override
    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(mySensorListener, accSensor, SensorManager.SENSOR_DELAY_GAME);
    }

    @Override
    protected void onStop() {
        sensorManager.unregisterListener(mySensorListener);
        super.onStop();
    }

Note the SENSOR_DELAY_GAME. The API allows you to tune your accelerometer response to an appropriate level for your app, so check out the API documentation.

That’s it!

About dave@kiwiluv.com

Comments

2 Responses to “The Android Accelerometer”
  1. Dirk Mass says:

    Please let me know if you’re looking for a article author for your site. You have some really good posts and I believe I would be a good asset. If you ever want to take some of the load off, I’d absolutely love to write some articles for your blog in exchange for a link back to mine. Please blast me an e-mail if interested. Many thanks!

  2. Hi Dirk,

    If you’d like to contribute, contact me at dave@kiwiluv.com. As long as the content is relavent, the more the merrier!

    Dave

Speak Your Mind

Tell us what you're thinking...
and oh, if you want a pic to show with your comment, go get a gravatar!