Using Sensors on the Android Platform

baroohspottyMobile - Wireless

Jul 19, 2012 (5 years and 10 months ago)


Using Sensors on the
Android Platform
Andreas Terzis
Android N00b
Hardware-oriented Features
Feature Description
A class that enables your application to interact with the
camera to snap a photo, acquire images for a preview
screen, and modify parameters used to govern how the
camera operates.
Class representing a sensor. Use getSensorList(int) to get the
list of available Sensors.
A class that permits access to the sensors available within
the Android platform.
An interface used for receiving notifications from the
SensorManager when sensor values have changed. An
application implements this interface to monitor one or more
sensors available in the hardware.
This class represents a sensor event and holds information
such as the sensor type (e.g., accelerometer, orientation,
etc.), the time-stamp, accuracy and of course the sensor's
This class is used to estimated estimate magnetic field at a
given point on Earth, and in particular, to compute the
magnetic declination from true north.
SensorManager Class
SensorManager lets you access the device's
 Get an instance of this class by calling
Context.getSystemService() with the argument
Methods (partial list)
 List<Sensor> getSensorList(int type)
 Static float[] getOrientation(float []R,float []values)
 registerListener(SensorEventListener listener, Sensor sensor, int
 Rate is only a hint to the system: Fastest, game, normal, user interface
Sensor Class
Class representing a sensor
Sensor type
 Accelerometer, gravity, gyroscope, pressure, light,
magnetic field, proximity, temperature, etc.
Methods (partial list)
 public string getName()
 public float getPower()

public float getMaximumRange()
SensorEvent Class
• This class represents a Sensor event and holds
information such as the sensor's type, the
time-stamp, accuracy and of course the
sensor's data.
• Fields:
– public int accuracy
– public Sensor sensor
– public long timestamp

public final float[] values
SensorEventListener Interface
• To interact with a sensor, an application must
register to listen for activity related to one or
more sensors. Registering takes place with the
registerListener method of the
SensorManager class.
• Two required methods
– onSensorChanged(SensorEvent event): method is
invoked whenever a sensor value has changed

onAccuracyChanged(Sensor sensor, int
accuracy): method is invoked when the accuracy of a
sensor has been changed
Registering to receive sensor events
public class SensorReader extends Activity implements SensorEventListener {
SensorManager sm= null;
public void onCreate(Bundle savedInstanceState) {
// get reference to SensorManager
sm= (SensorManager) getSystemService(SENSOR_SERVICE);
protected void onResume() {
// register as a listener for the accel. sensors
sm.registerListener(this, Sensor.TYPE_ACCELEROMETER,
protected void onStop() {
// unregister listener
Receiving events
public void onSensorChanged(SensorEvent event) {
synchronized (this) {
if (event.sensor == Sensor.SENSOR_ACCELEROMETER) {
xViewA.setText("Accel X: " + event.values[0]);
yViewA.setText("Accel Y: " + event.values[1]);
zViewA.setText("Accel Z: " + event.values[2]);
public void onAccuracyChanged(Sensor sensor, int accuracy) {
Log.d(tag,"onAccuracyChanged: " + sensor.getName() + ",
accuracy: " + accuracy);
Fall Detection Application
• Use a combination of acceleration and
orientation sensor to detect people falling
:Total acceleration of phone body
The acceleration of absolute vertical direction
If (diff between within a time window ) >
Check the difference between the max and min following in next time window
If this difference is larger than some threshold, a fall is potentially detected
1. For total acceleration:1. For total acceleration:
2. For absolute vertical direction:
The same algorithm as 1, but different threshold values
- If 1 and 2 say yes, algorithm reports a fall
Dai et. al., PerFallD: A Pervasive Fall Detection System Using Mobile Phones
Fall Detection Algorithm
Code Walkthrough
Example: GPS (I)
Add LocationManager to get Updates
public class GPSSimulator extends Activity
private LocationManager lm;
private LocationListener locationListener;
// Called when the activity is first created.
public void onCreate(Bundle savedInstanceState) {
// use the LocationManager class to obtain GPS locations
lm = (LocationManager)
locationListener = new MyLocationListener();
LocationManager.GPS_PROVIDER, 0, 0, locationListener);
Example GPS (II): Add
private class MyLocationListener implements LocationListener {
public void onLocationChanged(Location loc) {
if (loc != null) {
"Location changed : Lat: " + loc.getLatitude() +
" Lng: " + loc.getLongitude(),
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub