Motion Sensors and Orientation in Android

An Activity can listen for changes in a device’s motion sensors by implementing the OrientationEventListener or SensorEventListener. The Listener’s onSensorChanged function will return units of measure differently depending on the axis of rotation. Each of the axes returned from the SensorEventListener are best interpretted by considering their roots in flight dynamics and how they would relate to a plane’s joystick in flight, where the android device would be the joystick.
Download the SensorEventListener Cheatsheet >>

X represents the ‘Azimuth’ which is the angle in current reference to Magnetic North. The units of measure are within 0-360 degrees imagining a complete rotation divided by 360 equal divisions. This is easiest interpretted by considering the device from a birds eye view and considering that this is the same response as you would expect from a compass.

Y or ‘Pitch’ is the degree to which the device is tilted forwards or backwards. The measure of pitch is zero when the device is lying flat, -90 when standing upright and 180 when lying flat on the device face. Realising that -90 is the value of the device upstanding and 90 being the value when it is upside down is initially confusing until you think about the aviation analogy. If a plane’s nose is dipping and the plane is losing height a pilot would pull upwards. The upwards values represent the increase in height as the pilot pulled back on the joystick. Using this analogy, holding the device and pulling backwards would increase the height and pushing forwards would decrease the height.

Z or ‘Roll’ is the rotation of the device in relation to the bottom left hand corner of the screen. The units returned represent the degree of change in degrees from 0 – +/-90. Zero is when the device is upright standing and forward facing. Any movement left or right will then increase or decrease to 90 or -90 and on to 0 when going past these values. Each quadrant within the clock face of rotation will progress 0 – 90. During flight a plane’s wings would ideally be aligned with the horizon and the cockpit would be upright at 90 degrees.

Initially dealing with data in this format can be a bit dis-orientating so if you only want to get the angle of the phone moving from an initial upright standing position use the OrientationEventListener. Otherwise the values returned form the SensorEventListener are the Y & Z representing the 0 – 90 degrees of rotation while the angle is passing through a certain quadrant within a full clock face of rotation while X units are returned in relation to Magnetic North.

The OrientationEventListener

The OrientationEventListener provides a much easier solution to detecting the phone’s current angle when you only need the angle of change from upright standing to left/right and around a clock face of rotation. Implementing this interface will enable you to access orientation as a simple int representing the rotation in degrees.

Rather than have you feel the pain of trying to make sense of Android Sensor data from scratch we’ve made a quick printable cheat sheet, so puzzle no more!

SensorEventListener cheatsheet

Android SDK at time of writing was: android-sdk-mac_x86-1.5

Citations & Research

7 Comments