Android Sensor Programming Weihong Yu
Sensors Overview The Android platform is ideal for creating innovative applications through the use of sensors. These built-in sensors measure motion, orientation, and various environmental conditions. These sensors are capable of providing raw data with high precision and accuracy, and are useful if you want to monitor three-dimensional device movement or positioning, or you want to monitor changes in the ambient environment near a device.
Android Sensors Overview Android Sensors: MIC Camera Temperature Location (GPS or Network) Orientation Accelerometer Proximity Pressure Light
hardware-based sensors Sensors physical components built into a handset or tablet device They derive their data by directly measuring specific environmental properties, such as acceleration, geomagnetic field strength, or angular change software-based sensors not physical devices, although they mimic hardware-based sensors Software-based sensors derive their data from one or more of the hardware-based sensors and are sometimes called virtual sensors or synthetic sensors Examples: the linear acceleration sensor
categories of sensors supported by Android platform Motion sensors :measure acceleration forces and rotational forces along three axes accelerometers, gravity sensors, gyroscopes, etc. Environmental sensors: measure various environmental parameters, such as ambient air temperature and pressure, illumination, and humidity barometers, photometers, and thermometers. Position sensors: measure the physical position of a device orientation sensors and magnetometers.
Sensor Coordinate System When a device is held in its default orientation the X axis is horizontal and points to the right the Y axis is vertical and points up and the Z axis points toward the outside of the screen face
Android sensor framework The sensor framework provides several classes and interfaces that help you perform a wide variety of sensor-related tasks Determine which sensors are available on a device. Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power requirements, and resolution. Acquire raw sensor data and define the minimum rate at which you acquire sensor data. Register and unregister sensor event listeners that monitor sensor changes.
Sensor Framework-sensor Manager You can use this class to create an instance of the sensor service This class provides various methods for accessing and listing sensors, registering and unregistering sensor event listeners, and acquiring orientation information. This class also provides several sensor constants that are used to report sensor accuracy, set data acquisition rates, and calibrate sensors.
Sensor Framework---Sensor You can use this class to create an instance of a specific sensor. This class provides various methods that let you determine a sensor's capabilities.
Sensor Framework---SensorEvent The system uses this class to create a sensor event object, which provides information about a sensor event. A sensor event object includes the following information: the raw sensor data the type of sensor that generated the event the accuracy of the data the timestamp for the event
Sensor Framework---SensorEventListener You can use this interface to create two callback methods that receive notifications (sensor events) when sensor values change or when sensor accuracy changes. onaccuracychanged(sensor sensor, int accuracy) :Called when the accuracy of a sensor has changed. onsensorchanged(sensorevent event) :Called when sensor values have changed.
Basic Steps for Programming Step 1--access a SensorManager via getsystemservice(sensor_service). Android supports several sensors via the SensorManager Step 2--access the Sensor via the sensormanager.getdefaultsensor() method Step 3--Once you acquired a sensor, you can register a SensorEventListener object on it. This listener will get informed, if the sensor data changes. Step 4--To avoid the unnecessary usage of battery you register your listener in the onresume() method and de-register it in the onpause() method.
Android Activity lifecycle
Example 1:List all sensors Step 1--- get a reference to the sensor service private SensorManager msensormanager; msensormanager = (SensorManager) getsystemservice(context.sensor_servi CE); Step2---get a list of all sensors on a device List<Sensor> devicesensors = msensormanager.getsensorlist(sensor.typ E_ALL);
Example 2:Use Accelerometer Step 1---get an instance of SensorManager and Sensor msensormanager = (SensorManager) getsystemservice(context.sensor_servic E); maccelerometer = msensormanager.getdefaultsensor(sensor.t YPE_ACCELEROMETER);
Example 2:Use Accelerometer Step 2---register a sensor event listener msensormanager.registerlistener(this, maccelerometer, SensorManager.SENSOR_DELAY_GAME));
Example 2:Use Accelerometer Step 3---override onsensorchanged(sensorevent event) and use event.values[] to get the co-ordinates @Override public void onsensorchanged(sensorevent event) { float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; }
Codes for Using Accelerometer Get the Sensor Service and Sensor public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_acc_main); mytx=(textview)this.findviewbyid(r.id.tx); myty=(textview)this.findviewbyid(r.id.ty); mytz=(textview)this.findviewbyid(r.id.tz); sm=(sensormanager)this.getsystemservice(context.se NSOR_SERVICE); mysensor=sm.getdefaultsensor(sensor.type_accele ROMETER); }
Codes for Using Accelerometer Define an instance of SensorEventListener private final SensorEventListener mysensorlistenr=new SensorEventListener(){ public void onaccuracychanged(sensor sensor, int accuracy) { // TODO Auto-generated method stub } public void onsensorchanged(sensorevent event) { // TODO Auto-generated method stub mytx.settext(string.valueof(event.values[0])); myty.settext(string.valueof(event.values[1])); mytz.settext(string.valueof(event.values[2])); } };
Codes for Using Accelerometer Register the object of SensorEventListener protected void onresume() { // TODO Auto-generated method stub super.onresume(); sm.registerlistener(mysensorlistenr, mysensor, SensorManager.SENSOR_DELAY_NORMAL); }
Codes for Using Accelerometer De-Register the object of SensorEventListener protected void onpause() { // TODO Auto-generated method stub super.onpause(); sm.unregisterlistener(mysensorlistenr); }
Running result on the Emulator But you currently can't test all sensor code on the emulator because the emulator cannot emulate all sensors. Most of the time,you must test your sensor code on a physical device.