Android Sensor Programming
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
Android Sensors Overview
•Android Sensors:
– MIC – Camera – Temperature – Location (GPS or Network) – Orientation – Accelerometer – Proximity – Pressure – LightSensors
• hardware-based 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
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
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
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.
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,
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