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:
First, we create the infrastructure to get the values from the orientation, accelerometer, and magnetometer sensors. Inside the
onCreate()
method ofCompassActivity
, we initialize all the three sensors usingSensorManager
. We also initiate a layout file for the activity, which consists of a compass image that has North, South, East, and West marked on it. The compass image will be rotated to align its north to point toward the Earth's magnetic north. We also created four float arrays to store...