RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Augmented Reality on Android: Prepping the Camera and Compass : Page 3

Learn how to implement the first two elements of an Augmented Reality engine (the camera and the Compass) on Android.


Asking the Phone for Direction(s)

The final piece of the puzzle is getting directional information from the magnetic compass. Be aware that these compasses tend to freak out when near large metal objects such as...well...computers. With that caveat in mind, you can begin using the compass.

First, you need to extend or to create inline a SensorEventListener. Here's how to do it inline:

SensorEventListener listener = new SensorEventListener(){

   public void onAccuracyChanged(Sensor arg0, int 

   public void onSensorChanged(SensorEvent evt)
      float vals[] = evt.values;   
      direction = vals[0];
This code simply modifies a direction integer whenever the onSensorChanged method is invoked. You can get away with this because you're planning on registering only for orientation sensor updates. If you're listening to both the compass and the GPS, this method will get called whenever either sensor changes. Be sure to check the type of the SensorEvent before modifying any values.

With the compass, the values array should contain a single float: a number representing the bearing or degrees from north at which your device is pointed. A zero value (0) would be due north, 180 would be due south.

Now that you have a listener, you need to register it with the Sensor Manager. You create and register with a SensorManager object like this:

public static SensorManager sensorMan;
sensorMan = (SensorManager)ctx.getSystemService(Context.SENSOR_SERVICE);
Apologies for the horrid line breaking. These functions are long, so be sure to paste the code out and delete the line breaks. As you can see (when you've deciphered the code), you're simply getting the Sensor Manager from the getSystemService call on the context. This returns a sensor manager with which you can register a listener.

Compile this mess, run it on your device, and watch the camera render while the compass updates your orientation. Remember, the compass updates will not come in on the UI thread. Android veterans know that you can change views only on the UI or Main thread, so plan accordingly.

Tying on a Bow

As you can see, when you break the process of Augmented Reality down, it becomes a series of straightforward concrete steps. The next article will reveal how to get and normalize the GPS and accelerometer information. In the meantime, this article should serve as a tutorial for using both the camera preview and the compass in whatever application you wish.

Chris Haseman is an independent software engineer specializing in wireless development. He can be found riding his bike between coffee shops in San Francisco. He's the author of the book Android Essentials (published by Apress). In his spare time, he's a resident DJ at xtcradio.com and a martial arts instructor.
Email AuthorEmail Author
Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date