Using Sensors on the Android Platform Andreas Terzis Android N00b
Hardware-oriented Features Feature Camera Sensor SensorManager SensorEventListener SensorEvent GeoMagneticField 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 data. 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 sensors Get an instance of this class by calling Context.getSystemService() with the argument SENSOR_SERVICE Methods (partial list) List<Sensor> getsensorlist(int type) Static float[] getorientation(float []R,float []values) registerlistener(sensoreventlistener listener, Sensor sensor, int rate) 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) { super.oncreate(savedinstancestate); // get reference to SensorManager sm = (SensorManager) getsystemservice(sensor_service); protected void onresume() { super.onresume(); // register as a listener for the accel. sensors sm.registerlistener(this, Sensor.TYPE_ACCELEROMETER, SensorManager.SENSOR_DELAY_NORMAL); protected void onstop() { // unregister listener sm.unregisterlistener(this); super.onstop();
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
Fall Detection Algorithm :Total acceleration of phone body The acceleration of absolute vertical direction 1. For total acceleration: 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 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
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. @Override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); // use the LocationManager class to obtain GPS locations lm = (LocationManager) getsystemservice(context.location_service); locationlistener = new MyLocationListener(); lm.requestlocationupdates( LocationManager.GPS_PROVIDER, 0, 0, locationlistener);
Example GPS (II): Add MyLocationListener private class MyLocationListener implements LocationListener { @Override public void onlocationchanged(location loc) { if (loc!= null) { Toast.makeText(getBaseContext(), "Location changed : Lat: " + loc.getlatitude() + " Lng: " + loc.getlongitude(), Toast.LENGTH_SHORT).show(); @Override public void onproviderdisabled(string provider) { // TODO Auto-generated method stub @Override public void onproviderenabled(string provider) { // TODO Auto-generated method stub @Override public void onstatuschanged(string provider, int status, Bundle extras) { // TODO Auto-generated method stub