Time for action – the compass using orientation sensor and orientation APIs

In our example, we will use the orientation sensor to develop the compass, and since it is a deprecated sensor, we will also develop it using alternative APIs. From the orientation sensor, we will directly use its azimuth values to feed into the compass, while for alternative APIs, we will use the raw accelerometer and magnetometer sensor values to compute the azimuth values. Let's look at both the implementations in detail:

  1. First, we create the infrastructure to get the values from the orientation, accelerometer, and magnetometer sensors. Inside the onCreate() method of CompassActivity, we initialize all the three sensors using SensorManager. We also initiate a layout ...

Get Android Sensor Programming By Example now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.