Book Image

Android Sensor Programming By Example

By : Varun Nagpal
Book Image

Android Sensor Programming By Example

By: Varun Nagpal

Overview of this book

Android phones available in today’s market have a wide variety of powerful and highly precise sensors. Interesting applications can be built with them such as a local weather app using weather sensors, analyzing risky driving behavior using motion sensors, a fitness tracker using step-counter sensors, and so on. Sensors in external devices such as Android Watch, Body Analyzer & Weight Machine, Running Speed Cell, and so on can also be connected and used from your Android app running on your phone. Moving further, this book will provide the skills required to use sensors in your Android applications. It will walk you through all the fundamentals of sensors and will provide a thorough understanding of the Android Sensor Framework. You will also get to learn how to write code for the supportive infrastructure such as background services, scheduled and long running background threads, and databases for saving sensor data. Additionally, you will learn how to connect and use sensors in external devices from your Android app using the Google Fit platform. By the end of the book, you will be well versed in the use of Android sensors and programming to build interactive applications.
Table of Contents (13 chapters)
Android Sensor Programming By Example
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface

Motion, position, and environmental sensors


The Android platform supports mainly three broad categories of sensors: the motion, position, and environment-based sensors. This categorization is done based on the type of physical quantity detected and measured by the sensors.

Motion sensors

Motion sensors are responsible for measuring any kind of force that could potentially create motion in the xy, and z axes of the phone. The motion could be either a linear or angular movement in any direction. This category includes accelerometers, gravity, gyroscope, and rotational vector sensors. Most of these sensors will have values in the x, y, and z axes, and the rotational vector will especially have extra value in the fourth axis, which is the scalar component of the rotation vector.

The following table summarizes the motion sensor usage, types, and power consumption:

Sensor

Type

Value

Underlying Sensors

Description

Common Usage

Power Consumption

Accelerometer

Physical

Raw

Accelerometer

This measures the acceleration force along the xy, and z axes (including gravity). Unit: m/s2

It can be used to detect motion such as shakes, swings, tilt, and physical forces applied on the phone.

Low

Gravity

Synthetic

Fused

Accelerometer, Gyroscope

This measures the force of gravity along the xy, and z axes. Unit: m/s2

It can be used to detect when the phone is in free fall.

Medium

Linear Acceleration

Synthetic

Fused

Accelerometer, Gyroscope

It measures the acceleration force along the xy, and z axes (excluding gravity). Unit: m/s2

It can be used to detect motion such as shakes, swings, tilt, and physical forces applied on phone.

Medium

Gyroscope

Physical

Raw, Calibrated

Gyroscope

This measures the rate of rotation of the device along the xy, and z axes. Unit: rad/s

It can be used to detect rotation motions such as spin, turn, and any angular movement of the phone.

Medium

Step Detector

Synthetic

Calibrated

Accelerometer

This detects walking steps.

It can be used to detect when a user starts walking.

Low

Step Counter

Synthetic

Calibrated

Accelerometer

It measures the number of steps taken by the user since the last reboot while the sensor was activated

It keeps track of the steps taken by the user per day.

Low

Significant Motion

Synthetic

Calibrated

Accelerometer

It detects when there is significant motion on the phone because of walking, running, or driving.

It detects a significant motion event.

Low

Rotation Vector

Synthetic

Fused

Accelerometer, Gyroscope, Magnetometer

This measures the rotation vector component along the x axis (x * sin(θ/2)), y axis (y * sin(θ/2)), and z axis (z * sin(θ/2)). Scalar component of the rotation vector ((cos(θ/2)). Unitless.

It can be used in 3D games based on phone direction.

High

Position sensors

Position sensors are used to measure the physical position of the phone in the world's frame of reference. For example, you can use the geomagnetic field sensor in combination with the accelerometer to determine a device's position relative to the magnetic North Pole. You can use the orientation sensor to determine the device's position in your application's frame of reference. Position sensors also support values in the x,y, and z axes.

The following table summarizes the position sensor's usage, types, and power consumption:

Sensor

Type

Value

Underlying Sensors

Description

Common Usage

Power Consumption

Magnetometer

Physical

Raw, Calibrated

Magnetometer

This measures the geomagnetic field strength along the xy, and z axes. Unit: μT

It can be used to create a compass and calculate true north.

Medium

Orientation (Deprecated)

Synthetic

Fused

Accelerometer, Gyroscope, Magnetometer

This measures the Azimuth (the angle around the z axis), Pitch (the angle around the x axis), and Roll (the angle around the y axis). Unit: Degrees

It can be used to detect the device's position and orientation.

Medium

Proximity

Physical

Raw

Proximity

This measures the distance of an object relative to the view screen of a device. Unit: cm

It can be used to determine whether a handset is being held up to a person's ear.

Low

Game Rotation Vector

Synthetic

Fused

Accelerometer, Gyroscope

This measures the rotation vector component along the x axis (x * sin(θ/2)), y axis (y * sin(θ/2)), and z axis (z * sin(θ/2)). It is the scalar component of the rotation vector (cos(θ/2)). Unitless. It is based only on the Gyroscope and Accelerometer and does not use the Magnetometer.

It can be used in 3D games based on phone direction.

Medium

Geomagnetic Rotation Vector

Synthetic

Fused

Accelerometer, Magnetometer

This measures the rotation vector component along the x axis (x * sin(θ/2)), y axis (y * sin(θ/2)), and z axis (z * sin(θ/2)). It is the scalar component of the rotation vector (cos(θ/2)). Unit less. * It is based only on the Magnetometer and Accelerometer and does not use the Gyroscope.

It can be used in augmented reality apps, which are based on the phone and compass direction.

Medium

Environmental sensors

Environment sensors are responsible for measuring environmental properties, such as temperature, relative humidity, light, and air pressure near the phone. Unlike motion and position sensors, which give sensor values multi-dimensional arrays, the environment sensors report single sensor values.

The following table summarizes the environment sensor's usage, types, and power consumption:

Sensor

Type

Value

Underlying Sensors

Description

Common Usage

Power Consumption

Ambient Temperature

Physical

Raw

Thermometer

This measures the ambient air temperature. Unit: Degrees Celsius

It is used for monitoring temperatures.

Medium

Light

Physical

Raw

Photometer

This measures the ambient light level (illumination). Unit: lx

It can be used to dim the screen brightness of the phone.

Low

Barometer

Physical

Raw

Barometer

This measures the ambient air pressure. Unit: mPa or mbar

It can be used to measure height relative to sea level.

Medium

Relative Humidity

Physical

Raw

Relative Humidity

This measures the relative ambient humidity in percentage. Unit: %

It can be used for calculating the dew point, and absolute and relative humidity.

Medium