-
Notifications
You must be signed in to change notification settings - Fork 5
sensor
##Sensors Overview Android devices have a wide variety of sensors that can capture measurements about the device and its environment. There are three broad categories of sensors supported by the Android platform:
- Motion sensors: These sensors measure acceleration forces and rotational forces along three axes. This category includes accelerometers, gravity sensors and gyroscope sensors
- Environmental sensors: These sensors measure various environmental parameters, such as pressure and illumination.
- Position sensors: These sensors measure the physical position of a device. This category includes orientation sensors and magnetometers.
To know more about the sensortypes supported by the Android platform click on this link. This tutorial is about how to acquire raw sensor data from the physical sensors in our applications.
To integrate sensors in our application, we require three classes and one interface from the android.hardware package:
-
SensorManager: It's the system service that manages sensors. Applications get a reference to the sensor manager by calling
getSystemServicemethod by passing the argumentCONTEXT.SENSOR_SERVICE. For example, -
Sensor: This class creates an instance of a specific sensor. It provides various methods that determine a sensor's capabilities like
getResolution(),getMaximumRange(),getPower(), getVendor(), getVersion().These methods are generally used to determine the data interpretation algorithms that are applied on the raw sensor data. -
SensorEvent: Sensor readings are represented as instances of the
SensorEventclass. The data this class holds depends on the specific kind of sensor that generated the reading but will include the sensor type, time stamp, accuracy, measurement data associated with that new reading. -
SensorEventListener: If an Application wants to receive information from a sensor, then it will have to implement a sensor event listener. This interface defines Call back methods:
OnAccuracyChangedmethod andonSensorChangedmethod that are invoked when accuracy of a sensor changes or when the sensor has a new reading resp.
In order for an application to use sensors, it needs to follow certain sequence of steps:
-
First, the application needs to refer SensorManager by calling
getSystemServicemethod and passing in the argument CONTEXT.SENSOR_SERVICE. For example
sm =(SensorManager) getSystemService(SENSOR_SERVICE); -
Using that SensorManager class, find out whether the required sensor exists or not. One can do this by using the
getDefaultSensormethod inSensorManagerclass . For example `` if (sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) != null){ ` // Success! There is ACCELEROMETER. // get reference to Accelerometer sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); } else{ // failure! There's no ACCELEROMETER finish(); }` -
If that type of sensor exists, get a instance of that sensor by using
Sensorclass. For example,
sensor = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); -
Then register a listener for that sensor which waits for the SensorEvent to happen and callsback two methods:
onSensorChangedmethod passing in theSensorEventclass corresponding to the new reading andOnAccuracyChangedmethod passing in the sensor that changed and the new accuracy
-
To register a listener, first implement the main class of the application with
sensoreventlistenerinterface. For example
public class MainActivity extends ActionBarActivity implements SensorEventListener { -
Then register a listener by calling the
registerListenermethod. This registration is generally done in theonResumemethod rather than inonCreatemethod to save power.
@Override
protected void onResume() {
super.onResume();
sm.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
} -
Then unregister the listener in
onPauseandonStopmethods
protected void onPause() {
super.onPause();
sm.unregisterListener(this);
}
- In the two callback methods:
onSensorChangedandOnAccuracyChangedmethods, first verify whether the event is from the associated sensor or not.
-
If yes, then use Event class that holds the raw data from the sensor along with other information like timestamp for the data interpretation algorithms or you can directly use the raw data depending on the purpose of the application.
For example,
@Override
public void onAccuracyChanged (Sensor arg0, int arg1) {
// Inflate the menu; this adds items to the action bar if it is present.
}
@Override
public void onSensorChanged (SensorEvent event) {`// first we need to check the event corresponds to accelerometer reading`if(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER){`displayReading.setText(" X: "+event.values[0]+"\n Y: " +event.values[1]+"\n Z: "+event.values[2]); ` `}`
- For applications to view list of sensors available on a device
- For applications to read raw accelerometer readings
- To know about the data interpretation algorithms applied on the raw sensor data, go through the 'professional_android_sensor_programming' book written by google developers
- http://developer.android.com/guide/topics/sensors/sensors_overview.html
- For a sample code, http://davidcrowley.me/?p=356
- For applying low pass and high pass filters on accelerometer readings, https://www.youtube.com/watch?v=tY-NImDTOz4