October 2018
Intermediate to advanced
464 pages
15h 17m
English
Since we've already covered reading sensor data in the Reading sensor data – using the Android Sensor Framework section (in the previous chapter), we won't repeat explaining the sensor framework, and, instead, jump right to the calculateCompassDirection() method.
We call this method directly from the onSensorChanged() callback. Since we used the same class to handle the sensor callbacks for both the Magnetometer and Accelerometer, we first check which sensor is being reported in SensorEvent. Then, we call SensorManager.getRotationMatrix(), passing in the last sensor data. If the calculation is successful, it returns RotationMatrix, which we use to call the SensorManager.getOrientation() method. Note that getOrientation()