Developing Sensor Applications on Intel Atom Processor-Based Android Phones and Tablets

This guide provides application developers with an introduction to the Android Sensor framework and discusses how to use some of the sensors that are generally available on phones and tablets based on the Intel Atom processor.

Published Date
27 - Nov - 2013
| Last Updated
27 - Nov - 2013
Developing Sensor Applications on Intel Atom Processor-Based Andr...

Developing Sensor Applications on Intel® Atom™ Processor-Based Android* Phones and Tablets

This guide provides application developers with an introduction to the Android Sensor framework and discusses how to use some of the sensors that are generally available on phones and tablets based on the Intel® Atom™ processor. Among those discussed are the motion, position, and environment sensors. Even though GPS is not strictly categorized as a sensor in the Android framework, this guide discusses GPS-based location services as well. The discussion in this guide is based On Android 4.2, Jelly Bean.

Sensors on Intel® Atom™ Processor-Based Android Phones and Tablets

The Android phones and tablets based on Intel Atom processors can support a wide range of hardware sensors. These sensors are used to detect motion and position changes, and report the ambient environment parameters. The block diagram in Figure 1 shows a possible sensor configuration on a typical Intel Atom processor-based Android device.

Figure 1. Sensors on an Intel® Atom™–based Android system
Based on the data they report, we can categorize Android sensors into the classes and types shown in Table 1.
Motion SensorsAccelerometer
Measures a device’s accelerations in m/s2Motion detection
Measures a device’s rates of rotationRotation detection
Position SensorsMagnetometer
Measures the Earth’s geomagnetic field strengths in µTCompass
Measures the proximity of an object in cmNearby object detection
(not a type of android.hardware.Sensor)
Gets accurate geo-locations of the deviceAccurate geo-location detection
Environment SensorsALS
Measures the ambient light level in lxAutomatic screen brightness control
BarometerMeasures the ambient air pressure in mbarAltitude detection

Table 1. Sensor Types Supported by the Android Platform

Android Sensor Framework

The Android sensor framework provides mechanisms to access the sensors and sensor data, with the exception of the GPS, which is accessed through the Android location services. We will discuss this later in this paper. The sensor framework is part of the android.hardware package. Table 2 lists the main classes and interfaces of the sensor framework.
SensorManagerClassUsed to create an instance of the sensor service. Provides various methods for accessing sensors, registering and unregistering sensor event listeners, and so on.
SensorClassUsed to create an instance of a specific sensor.
SensorEventClassUsed by the system to publish sensor data. It includes the raw sensor data values, the sensor type, the data accuracy, and a timestamp.
SensorEventListenerInterfaceProvides callback methods to receive notifications from the SensorManager when the sensor data or the sensor accuracy has changed.

Table 2. The Android Platform Sensor Framework

Obtaining Sensor Configuration

Device manufacturers decide what sensors are available on the device. You must discover which sensors are available at runtime by invoking the sensor framework’s SensorManager getSensorList() method with a parameter “Sensor.TYPE_ALL”. Code Example 1 displays a list of available sensors and the vendor, power, and accuracy information of each sensor.
003import java.util.ArrayList;
004import java.util.HashMap;
005import java.util.List;
006import java.util.Map;
009import android.content.Context;
010import android.hardware.Sensor;
011import android.hardware.SensorManager;
012import android.os.Bundle;
013import android.view.LayoutInflater;
014import android.view.View;
015import android.view.ViewGroup;
016import android.widget.AdapterView;
017import android.widget.AdapterView.OnItemClickListener;
018import android.widget.ListView;
019import android.widget.SimpleAdapter;
021publicclassSensorInfoFragment extends Fragment {
023    privateView mContentView;
025    privateListView mSensorInfoList;  
026    SimpleAdapter mSensorInfoListAdapter;
028    privateList<Sensor> mSensorList;
030    privateSensorManager mSensorManager;
032    @Override
033    publicvoidonActivityCreated(Bundle savedInstanceState) {
034        super.onActivityCreated(savedInstanceState);
035    }
037    @Override
038    publicvoidonPause()
039    {
040        super.onPause();
041    }
043    @Override
044    publicvoidonResume()
045    {
046        super.onResume();
047    }
049    @Override
050    publicView onCreateView(LayoutInflater inflater, ViewGroup container,
051            Bundle savedInstanceState) {
052        mContentView = inflater.inflate(R.layout.content_sensorinfo_main, null);
053        mContentView.setDrawingCacheEnabled(false);
055        mSensorManager = (SensorManager)getActivity().getSystemService(Context.SENSOR_SERVICE);
057        mSensorInfoList = (ListView)mContentView.findViewById(;
059        mSensorInfoList.setOnItemClickListener( newOnItemClickListener() {
061            @Override
062            publicvoidonItemClick(AdapterView<?> arg0, View view, intindex, longarg3) {
064                // with the index, figure out what sensor was pressed
065                Sensor sensor = mSensorList.get(index);
067                // pass the sensor to the dialog.
068                SensorDialog dialog = newSensorDialog(getActivity(), sensor);
070                dialog.setContentView(R.layout.sensor_display);
071                dialog.setTitle("Sensor Data");
072      ;
073            }
074        });
076        returnmContentView;
077    }
079    voidupdateContent(intcategory, intposition) {
080        mSensorInfoListAdapter = newSimpleAdapter(getActivity(),
081        getData() , android.R.layout.simple_list_item_2,
082        newString[] {
083            "NAME",
084            "VALUE"
085        },
086        newint[] {, });
087    mSensorInfoList.setAdapter(mSensorInfoListAdapter);
088    }
091    protectedvoidaddItem(List<Map<String, String>> data, String name, String value)   {
092        Map<String, String> temp = newHashMap<String, String>();
093        temp.put("NAME", name);
094        temp.put("VALUE", value);
095        data.add(temp);
096    }
099    privateList<? extends Map<String, ?>> getData() {
100        List<Map<String, String>> myData = newArrayList<Map<String, String>>();
101        mSensorList = mSensorManager.getSensorList(Sensor.TYPE_ALL);
103        for(Sensor sensor : mSensorList ) {
104            addItem(myData, sensor.getName(),  "Vendor: " sensor.getVendor() ", min. delay: " sensor.getMinDelay() ", power while in use: " sensor.getPower() "mA, maximum range: " sensor.getMaximumRange() ", resolution: " sensor.getResolution());
105        }
106        returnmyData;
107    }


Code Example 1. A Fragment that Displays the List of Sensors**

Sensor Coordinate System

The sensor framework reports sensor data using a standard 3-axis coordinate system, where X, Y, and Z are represented by values[0], values[1], and values[2] in the SensorEvent object, respectively.
Some sensors, such as light, temperature, proximity, and pressure, return only single values. For these sensors only values[0] in the SensorEvent object are used.
Other sensors report data in the standard 3-axis sensor coordinate system. The following is a list of such sensors:
  • Accelerometer
  • Gravity sensor
  • Gyroscope
  • Geomagnetic field sensor
The 3-axis sensor coordinate system is defined relative to the screen of the device in its natural (default) orientation. For a phone, the default orientation is portrait; for a tablet, the natural orientation is landscape. When a device is held in its natural orientation, the x axis is horizontal and points to the right, the y axis is vertical and points up, and the z axis points outside of the screen (front) face. Figure 2 shows the sensor coordinate system for a phone, and Figure 3 for a tablet.

Figure 2The sensor coordinate system for a phone

Figure 3. The sensor coordinate system for a tablet
The most important point regarding the sensor coordinate system is that the sensor’s coordinate system never changes when the device moves or changes its orientation.

Monitoring Sensor Events

The sensor framework reports sensor data with the SensorEvent objects. A class can monitor a specific sensor’s data by implementing the SensorEventListener interface and registering with the SensorManager for the specific sensor. The sensor framework informs the class about the changes in the sensor states through the following two SensorEventListener callback methods implemented by the class:






Code Example 2 implements the SensorDialog used in the SensorInfoFragment example we discussed in the section “Obtaining Sensor Configuration.”




04import android.content.Context;
05import android.hardware.Sensor;
06import android.hardware.SensorEvent;
07import android.hardware.SensorEventListener;
08import android.hardware.SensorManager;
09import android.os.Bundle;
10import android.widget.TextView;
12publicclassSensorDialog extends Dialog implements SensorEventListener {
13    Sensor mSensor;
14    TextView mDataTxt;
15    privateSensorManager mSensorManager;
17    publicSensorDialog(Context ctx, Sensor sensor) {
18        this(ctx);
19        mSensor = sensor;
20    }
22    @Override
23    protectedvoidonCreate(Bundle savedInstanceState) {
24        super.onCreate(savedInstanceState);
25        mDataTxt = (TextView) findViewById(;
26        mDataTxt.setText("...");
27        setTitle(mSensor.getName());
28    }
30    @Override
31    protectedvoidonStart() {
32        super.onStart();
33        mSensorManager.registerListener(this, mSensor,  SensorManager.SENSOR_DELAY_FASTEST);
34    }
36    @Override
37    protectedvoidonStop() {
38        super.onStop();
39        mSensorManager.unregisterListener(this, mSensor);
40    }
42    @Override
43    publicvoidonAccuracyChanged(Sensor sensor, intaccuracy) {
44    }
46    @Override
47    publicvoidonSensorChanged(SensorEvent event) {
48        if(event.sensor.getType() != mSensor.getType()) {
49            return;
50        }
51        StringBuilder dataStrBuilder = newStringBuilder();
52        if((event.sensor.getType() == Sensor.TYPE_LIGHT)||
53            (event.sensor.getType() == Sensor.TYPE_TEMPERATURE)||
54            (event.sensor.getType() == Sensor.TYPE_PRESSURE)) {
55            dataStrBuilder.append(String.format("Data: %.3fn", event.values[0]));
56        }
57        else{        
58            dataStrBuilder.append(
59                String.format("Data: %.3f, %.3f, %.3fn",
60                event.values[0], event.values[1], event.values[2] ));
61        }
62        mDataTxt.setText(dataStrBuilder.toString());
63    }
Code Example 2. A Dialog that Shows the Sensor Values**

Motion Sensors

Motion sensors are used to monitor device movement, such as shake, rotate, swing, or tilt. The accelerometer and gyroscope are two motion sensors available on many tablet and phone devices.
Motion sensors report data using the sensor coordinate system, where the three values in the SensorEvent object, values[0], values[1], and values[2], represent the x-, y-, and z-axis values, respectively.
To understand the motion sensors and apply the data in an application, we need to apply some physics formulas related to force, mass, acceleration, Newton’s laws of motion, and the relationship between several of these entities in time. To learn more about these formulas and relationships, refer to your favorite physics textbooks or public domain sources.


The accelerometer measures the acceleration applied on the device, and its properties are summarized in Table 3.


Data (m/s2)
Acceleration along the x axis
Acceleration along the y axis
Acceleration along the z axis
Table 3. The Accelerometer
The concept for the accelerometer is derived from Newton’s second law of motion:

a = F/m

The acceleration of an object is the result of the net external force applied to the object. The external forces include one that applies to all objects on Earth, gravity. It is proportional to the net force F applied to the object and inversely proportional to the object’s mass m.

In our code, instead of directly using the above equation, we are more concerned about the result of the acceleration during a period of time on the device’s speed and position. The following equation describes the relationship of an object’s velocity v1, its original velocity v0, the acceleration a, and the time t:

v1 = v0 at

To calculate the object’s position displacement s, we use the following equation: s = v0t (1/2)at2

In many cases we start with the condition v0 equal to 0 (before the device starts moving), which simplifies the equation to: s = at2/2

Because of gravity, the gravitational acceleration, represented with the symbol g, is applied to all objects on Earth. Regardless of the object’s mass, gonly depends on the latitude of the object’s location with a value in the range of 9.78 to 9.82 (m/s2). We adopt a conventional standard value for g:g = 9.80665 (m/s2)

Because the accelerometer returns the values using a multidimensional device coordinate system, in our code we can calculate the distances along the x, y, and z axes using the following equations: Sx = AxT2/2


Where SxSy, and Sz are the displacements on the x axis, y axis, and z axis, respectively, and AxAy, and Az are the accelerations on the x axis, y axis, and z axis, respectively. T is the time of the measurement period.

01publicclassSensorDialog extends Dialog implements SensorEventListener {
02    …  
03    privateSensor mSensor;
04    privateSensorManager mSensorManager;
06    publicSensorDialog(Context context) {
07        super(context);
08        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
09        mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

Code Example 3Instantiation of an Accelerometer**

Sometimes we don’t use all three dimension data values. Other times we may also need to take the device’s orientation into consideration. For example, for a maze application, we only use the x-axis and y-axis gravitational acceleration to calculate the ball’s moving directions and distances based on the orientation of the device. The following code fragment (Code Example 4) outlines the logic.

02publicvoidonSensorChanged(SensorEvent event) {
03    if(event.sensor.getType() != Sensor.TYPE_ACCELEROMETER) {
04        return;
05    }
06floataccelX, accelY;
08//detect the current rotation currentRotation from its “natural orientation”
09//using the WindowManager
10    switch(currentRotation) {
11        caseSurface.ROTATION_0:
12            accelX = event.values[0];
13            accelY = event.values[1];
14            break;
15        caseSurface.ROTATION_90:
16            accelX = -event.values[0];
17            accelY = event.values[1];
18            break;
19        caseSurface.ROTATION_180:
20            accelX = -event.values[0];
21            accelY = -event.values[1];
22            break;
23        caseSurface.ROTATION_270:
24            accelX = event.values[0];
25            accelY = -event.values[1];
26            break;
27    }
28    //calculate the ball’s moving distances along x, and y using accelX, accelY and the time delta
30    }

Code Example 4. Considering the Device Orientation When Using the Accelerometer Data in a Maze Game**


The gyroscope (or simply gyro) measures the device’s rate of rotation around the x , y, and z axes, as shown in Table 4. The gyroscope data values can be positive or negative. By looking at the origin from a position along the positive half of the axis, if the rotation is counterclockwise around the axis, the value is positive; if the rotation around the axis is clockwise, the value is negative. We can also determine the direction of a gyroscope value using the “right-hand rule,” illustrated in Figure 4.

Figure 4. Using the “right-hand rule” to decide the positive rotation direction
Data (rad/s)
Rotation rate around the x axis
Rotation rate around the y axis
Rotation rate around the z axis
Table 4. The Gyroscope
Code Example 5 shows how to instantiate a gyroscope.

01publicclassSensorDialog extends Dialog implements SensorEventListener {
02    …  
03    privateSensor mGyro;
04    privateSensorManager mSensorManager;
06    publicSensorDialog(Context context) {
07        super(context);
08        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
09        mGyro = mSensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

Code Example 5. Instantiation of a Gyroscope**

Position Sensors
Many Android tablets support two position sensors: the magnetometer and the proximity sensors. The magnetometer measures the strengths of the Earth’s magnetic field along the x, y, and z axes, while the proximity sensor detects the distance of the device from another object.
The most important usage of the magnetometer (described in Table 5) in Android systems is to implement the compass.
Data (µT)
Earth magnetic field strength along the x axis
Earth magnetic field strength along the y axis
Earth magnetic field strength along the z axis
Table 5. The Magnetometer
Code Example 6 shows how to instantiate a magnetometer.
01publicclassSensorDialog extends Dialog implements SensorEventListener {
02    …  
03    privateSensor mMagnetometer;
04    privateSensorManager mSensorManager;
06    publicSensorDialog(Context context) {
07        super(context);
08        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
09        mMagnetometer = mSensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);

Code Example 6. Instantiation of a Magnetometer**



The proximity sensor provides the distance between the device and another object. The device can use it to detect if the device is being held close to the user (see Table 6), thus determining if the user is on a phone call and turning off the display during the phone call.
Table 6: The Proximity Sensor
ProximityTYPE_PROXIMITYvalues[0]Distance from an object in cm. Some proximity sensors only report a Boolean value to indicate if the object is close enough.
Code Example 7 shows how to instantiate a proximity sensor.
01publicclassSensorDialog extends Dialog implements SensorEventListener {
02    …  
03    privateSensor mProximity;
04    privateSensorManager mSensorManager;
06    publicSensorDialog(Context context) {
07        super(context);
08        mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
09        mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);

Code Example 7. Instantiation of a Proximity Sensor**


Environment Sensors 

The environment sensors detect and report the device’s ambient environment parameters, such as light, temperature, pressure, or humidity. The ambient light sensor (ALS) and the pressure sensor (barometer) are available on many Android tablets.

 Ambient Light Sensor (ALS)


The ambient light sensor, described in Table 7, is used by the system to detect the illumination of the surrounding environment and automatically adjust the screen brightness accordingly.
Table 7: The Ambient Light Sensor
Data (lx)
ALSTYPE_LIGHTvalues[0]The illumination around the device
Code Example 8 shows how to instantiate the ALS.
2privateSensor mALS;
3privateSensorManager mSensorManager;
6    mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
7    mALS = mSensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);

Code Example 8. Instantiation of an Ambient Light Sensor**


Applications can use the atmospheric pressure sensor (barometer), described in Table 8, to calculate the altitude of the device’s current location.
Table 8: The Atmosphere Pressure Sensor
Data (lx)
BarometerTYPE_PRESSUREvalues[0]The ambient air pressure in mbar
Code Example 9 shows how to instantiate the barometer.
2privateSensor mBarometer;
3privateSensorManager mSensorManager;
6    mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
7    mBarometer = mSensorManager.getDefaultSensor(Sensor.TYPE_PRESSURE);

Code Example 9. Instantiation of a barometer**

Sensor Performance and Optimization Guidelines

To use sensors in your applications, you should follow these best practices:
  • Always check the specific sensor’s availability before using it
    The Android platform does not require the inclusion or exclusion of a specific sensor on the device. Before using a sensor in your application, always first check to see if it is actually available.
  • Always unregister the sensor listeners
    If the activity that implements the sensor listener is becoming invisible, or the dialog is stopping, unregister the sensor listener. It can be done via the activity’s onPause() method, or in the dialog’s onStop() method. Otherwise, the sensor will continue acquiring data and as a result drain the battery.
  • Don’t block the onSensorChanged() method
    The onSensorChanged() method is frequently called by the system to report the sensor data. You should put as little logic inside this method as possible. Complicated calculations with the sensor data should be moved outside of this method.
  • Always test your sensor applications on real devices
    All sensors described in this section are hardware sensors. The Android Emulator may not be capable of simulating a particular sensor’s functions and performance.

GPS and Location

GPS (Global Positioning System) is a satellite-based system that provides accurate geo-location information around the world. GPS is available on many Android phones and tablets. In many perspectives GPS behaves like a position sensor. It can provide accurate location data for applications running on the device. On the Android platform, GPS is not directly managed by the sensor framework. Instead, the Android location service accesses and transfers GPS data to an application through the location listener callbacks.
This section only discusses the GPS and location services from a hardware sensor point of view. The complete location strategies offered by Android 4.2 and Intel Atom processor-based Android phones and tablets is a much larger topic and is outside of the scope of this section.


Android Location Services


Using GPS is not the only way to obtain location information on an Android device. The system can also use Wi-Fi*, cellular networks, or other wireless networks to get the device’s current location. GPS and wireless networks (including Wi-Fi and cellular networks) act as “location providers” for Android location services. Table 9 lists the main classes and interfaces used to access Android location services.
Table 9: The Android Platform Location Service
LocationManagerClassUsed to access location services. Provides various methods for requesting periodic location updates for an application, or sending proximity alerts
LocationProviderAbstract classThe abstract super class for location providers
LocationClassUsed by the location providers to encapsulate geographical data
LocationListenerInterfaceUsed to receive location notifications from the LocationManager

Obtaining GPS Location Updates

Similar to the mechanism of using the sensor framework to access sensor data, the application implements several callback methods defined in the LocationListener interface to receive GPS location updates. The LocationManager sends GPS update notifications to the application through these callbacks (the “Don’t call us, we’ll call you” rule).
To access GPS location data in the application, you need to request the fine location access permission in your Android manifest file (Code Example 10).
1<manifest …>
3    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

Code Example 10. Requesting the Fine Location Access Permission in the Manifest File**

Code Example 11 shows how to get GPS updates and display the latitude and longitude coordinates on a dialog text view.
04import android.content.Context;
05import android.location.Location;
06import android.location.LocationListener;
07import android.location.LocationManager;
08import android.os.Bundle;
09import android.widget.TextView;
11publicclassGpsDialog extends Dialog implements LocationListener {
12    TextView mDataTxt;
13    privateLocationManager mLocationManager;
15    publicGpsDialog(Context context) {
16        super(context);
17        mLocationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
18    }
20    @Override
21    protectedvoidonCreate(Bundle savedInstanceState) {
22        super.onCreate(savedInstanceState);
23           mDataTxt = (TextView) findViewById(;
24          mDataTxt.setText("...");
26        setTitle("Gps Data");
27    }
29    @Override
30    protectedvoidonStart() {
31        super.onStart();
32        mLocationManager.requestLocationUpdates(
33            LocationManager.GPS_PROVIDER, 0, 0, this);
34    }
36    @Override
37    protectedvoidonStop() {
38        super.onStop();
39        mLocationManager.removeUpdates(this);
40    }
42    @Override
43    publicvoidonStatusChanged(String provider, intstatus,
44        Bundle extras) {
45    }
47    @Override
48    publicvoidonProviderEnabled(String provider) {
49    }
51    @Override
52    publicvoidonProviderDisabled(String provider) {
53    }
55    @Override
56    publicvoidonLocationChanged(Location location) {
57        StringBuilder dataStrBuilder = newStringBuilder();
58        dataStrBuilder.append(String.format("Latitude: %.3f,   Logitude%.3fn", location.getLatitude(), location.getLongitude()));
59        mDataTxt.setText(dataStrBuilder.toString());
61    }

Code Example 11. A dialog that Displays the GPS Location Data**

GPS and Location Performance and Optimization Guidelines

GPS provides the most accurate location information on the device. On the other hand, as a hardware feature, it consumes extra energy. It also takes time for the GPS to get the first location fix. Here are some guidelines you should follow when developing GPS and location-aware applications:
  • Consider all available location providers
    In addition to the GPS_PROVIDER, there is NETWORK_PROVIDER. If your applications only need coarse location data, you may consider using the NETWORK_PROVIDER.
  • Use the cached locations
    It takes time for the GPS to get the first location fix. When your application is waiting for the GPS to get an accurate location update, you can first use the locations provided by the LocationManager’s getlastKnownLocation() method to perform part of the work.
  • Minimize the frequency and duration of location update requests
    You should request the location update only when needed and promptly de-register from the location manager once you no longer need location updates.

For more information please visit:



Team DigitTeam Digit

All of us are better than one of us.