Android Sensors CPRE 388 Fall 2015 Iowa State University
What are sensors? Sense and measure physical and ambient conditions of the device and/or environment Measure motion, touch pressure, orientation, environmental conditions, gravity Example Motion measurement (gestures) Tilt, rotate, toss, swing Can use for interactive games to change animation characteristics in response to type of motion Example Accelerometer and geomagnetic field sensor to infer direction in a compass or travel app
Sensors in Android Three major categories Motion Sensors Measures acceleration and rotation along three axes Example accelerometers, gravity sensors, gyroscopes, and rotational vector sensors Environmental sensors Measures environmental parameters ambient air temperature and pressure, illumination, and humidity. Position sensors Measures the physical position of a device orientation sensors and magnetometers
Sensors in Android Two types Hardware sensors Physical components built into the device Data derived by direct measurement Accelerometer, Ambient temperature Software or Virtual sensors Not physical devices but mimic hardware based sensors Data derived from one or more hardware sensors Linear acceleration sensor, gravity sensor
Sensor Type Description Common Uses TYPE_ACCELEROMETER TYPE_AMBIENT_TEMPER ATURE TYPE_GRAVITY TYPE_GYROSCOPE TYPE_LIGHT Hardware Hardware Software or Hardware Hardware Hardware Measures the acceleration force in m/s 2 that is applied to a device on all three physical axes (x, y, and z), including the force of gravity. Measures the ambient room temperature in degrees Celsius ( C). See note below. Measures the force of gravity in m/s 2 that is applied to a device on all three physical axes (x, y, z). Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z). Measures the ambient light level (illumination) in lx. Motion detection (shake, tilt, etc.). Monitoring air temperatures. Motion detection (shake, tilt, etc.). Rotation detection (spin, turn, etc.). Controlling screen brightness.
TYPE_LINEAR_ACCELERAT ION Software or Hardware Measures the acceleration force in m/s 2 that is applied to a device on all three physical axes (x, y, and z), excluding the force of gravity. Monitoring acceleration along a single axis. TYPE_MAGNETIC_FIELD Hardware Measures the ambient geomagnetic field for all three physical axes (x, y, z) in μt. Creating a compass. TYPE_ORIENTATION Software Measures degrees of rotation that a device makes around all three physical axes (x, y, z). As of API level 3 you can obtain the inclination matrix and rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the getrotationmatrix() method. Determining device position.
Android Sensor Framework Get raw data from sensors Change sensor parameters Sampling frequency Determine which sensors are available Determine sensor's specs Manufacturer, resolution, power needs Set up sensor event listeners to monitor changes
Android sensor framework Part of android.hardware package Classes and interfaces to access sensors
Android sensor framework classes SensorManager class to create an instance of the sensor service methods for accessing and listing sensors registering and unregistering sensor event listeners Determine sensor accuracy, set data acquisition rates, and calibrate sensors Sensor create an instance of a specific sensor determine a sensor's capabilities
Android sensor framework classes SensorEvent create a sensor event object provide the raw sensor data, the type of sensor that generated the event, the accuracy of the data, and the timestamp for the event SensorEventListener Two callback methods methods called when receiving notifications (sensor events) when sensor values change or when sensor accuracy changes
Primary API functionality Identifying sensors and sensor capabilities identify all of the sensors that are present on a device disable app features which rely on unavailable sensors Monitor sensor events acquire raw sensor data sensor event occurs every time a sensor detects a change in the parameters it is measuring Four parameters per event name of the sensor that triggered the event timestamp for the event accuracy of the event raw sensor data that triggered the event.
What sensors are in my device? Identify sensors get a reference to the system sensor service List all the sensors Instance of sensor service private SensorManager msensormanager; msensormanager = (SensorManager) getsystemservice(context.sensor_service); List<Sensor> devicesensors = msensormanager.getsensorlist(sensor.type_all); List all the sensors in the device
What sensors are in my device? is there a default sensor of the given type? If there is more than one sensor of a type, one of them is designated as default. if (msensormanager.getdefaultsensor(sensor.type_magnetic_field)!= null){ // Success! There's a magnetometer. TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION, or TYPE_GRAVITY
Sensor Capabilities & Attributes Sensor class public methods to determine the capabilities and attributes of individual sensors. Useful if you want your application to behave differently based on which sensors or sensor capabilities are available on a device. getresolution(), getmaximumrange(), getpower()
msensormanager = (SensorManager) getsystemservice(context.sensor_service); if (msensormanager.getdefaultsensor(sensor.type_gravity)!= null){ List<Sensor> gravsensors = msensormanager.getsensorlist(sensor.type_gravity); for(int i=0; i<gravsensors.size(); i++) { if ((gravsensors.get(i).getvendor().contains("google Inc.")) && (gravsensors.get(i).getversion() == 3)){ // Use the version 3 gravity sensor. msensor = gravsensors.get(i);
else{ // Use the accelerometer. if (msensormanager.getdefaultsensor(sensor.type_accelerometer)!= null){ msensor = msensormanager.getdefaultsensor(sensor.type_accelerometer); else{ // Sorry, there are no accelerometers on your device. // You can't play this game.
Monitor sensor events implement two callback methods onaccuracychanged() and onsensorchanged() exposed through the SensorEventListener interface sensor's accuracy changes system invokes the onaccuracychanged() method reference to the Sensor object that changed the new accuracy of the sensor Accuracy is represented by one of four status constants: SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_ME DIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE sensor reports a new value system invokes the onsensorchanged() method reference to SensorEvent object contains information about the new sensor data
public class SensorActivity extends Activity implements SensorEventListener { private SensorManager msensormanager; private Sensor mlight; @Override public final void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); msensormanager = (SensorManager) getsystemservice(context.sensor_service); mlight = msensormanager.getdefaultsensor(sensor.type_light);
Monitor sensor events @Override public final void onaccuracychanged(sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. @Override public final void onsensorchanged(sensorevent event) { // The light sensor returns a single value. // Many sensors return 3 values, one for each axis. float lux = event.values[0]; // Do something with this sensor value. Reference to Sensor object Reference to Sensor event object raw data, time stamp, accuracy, type of sensor
@Override protected void onresume() { super.onresume(); msensormanager.registerlistener(this, mlight, SensorManager.SENSOR_DELAY_NORMAL); @Override protected void onpause() { super.onpause(); msensormanager.unregisterlistener(this); int SENSOR_DELAY_FASTEST int SENSOR_DELAY_GAME int SENSOR_DELAY_NORMAL int SENSOR_DELAY_UI get sensor data as fast as possible (0 µs) rate suitable for games (20000 µs) rate (default) suitable for screen orientation changes (200000 µs) rate suitable for the user interface (60000 µs)
Detecting Sensor at Run Time private SensorManager msensormanager;... msensormanager = (SensorManager) getsystemservice(context.sensor_service); if (msensormanager.getdefaultsensor(sensor.type_pressure)!= null){ // Success! There's a pressure sensor. else { // Failure! No pressure sensor. Disable the UI that depends on pressure sensor
Uses Feature For google play, use uses feature in manifest <uses feature android:name="android.hardware.sensor.accelerometer" android:required="true" />
Sensor Coordinate System sensor framework uses a standard 3 axis coordinate system coordinate system is defined relative to the device's screen device is held in its default orientation default orientation the X axis is horizontal and points to the right the Y axis is vertical and points up the Z axis points toward the outside of the screen face. ve Z coordinates behind the screen the sensor's coordinate system never changes as the device moves Accelerometer, Gyroscope, gravity sensor, linear acceleration sensor, geomagnetic field sensor
Putting it all together Accelerometer public class sensorexample extends Activity implements SensorEventListener { private SensorManager sensensormanager; private Sensor senaccelerometer; @Override protected void oncreate(bundle savedinstancestate) { Reference to system sensor service Reference to default accelerometer Need to check if it exists super.oncreate(savedinstancestate); setcontentview(r.layout.activity_sensorexample); sensensormanager = (SensorManager) getsystemservice(context.sensor_service); senaccelerometer = sensensormanager.getdefaultsensor(sensor.type_accelerometer); sensensormanager.registerlistener(this, senaccelerometer, SensorManager.SENSOR_DELAY_NORMAL); Register the listener to call back when sensor event occurs
Called when accelerometer registers a new event such as motion along any axis @Override public void onsensorchanged(sensorevent sensorevent) { Sensor mysensor = sensorevent.sensor; if (mysensor.gettype() == Sensor.TYPE_ACCELEROMETER) { float x = sensorevent.values[0]; Acceleration values along float y = sensorevent.values[1]; the three axis float z = sensorevent.values[2]; @Override public void onaccuracychanged(sensor sensor, int accuracy) {
protected void onpause() { super.onpause(); sensensormanager.unregisterlistener(this); Important Sensors drain battery resource. Sensors keep acquiring data during onpause if not unregistered Re register Sensorevent listener to continue acquiring data during onresume protected void onresume() { super.onresume(); sensensormanager.registerlistener(this, senaccelerometer, SensorManager.SENSOR_DELAY_NORMAL);