Sensors & Motion Sensors in Android platform Minh H Dang CS286 Spring 2013
Sensors The Android platform supports three categories of sensors: Motion sensors: measure acceleration forces and rotational forces along three axes (in most devices) accelerometers, gravity sensors, gyroscopes, and rotational vector sensors
Sensors Position sensors: measure the physical position of a device orientation sensors and magnetometers Environmental sensors: measure environmental parameters (temperature and pressure, illumination, and humidity) (depend on device) Barometers, photometers, and thermometers.
Monitoring Sensor Events Implement 2 callback methods: - onaccuracychanged() - onsensorchanged() of interface SensorEventListener Example of Light sensor
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); } @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. } @Override protected void onresume() { super.onresume(); msensormanager.registerlistener(this, mlight, SensorManager.SENSOR_DELAY_NORMAL); } @Override protected void onpause() { super.onpause(); msensormanager.unregisterlistener(this);
Best Practices for Accessing and Using Sensors Unregister sensor listeners acquire data and use battery resources until unregister Don't test your code on the emulator Don't block the onsensorchanged() method avoid to have a lot of calculation in this Avoid using deprecated methods or sensor types & Choose sensor delay carefully Verify sensors before you use them detect sensors at runtime if (mlight == null)...
Motion Sensors Monitor the motion of a device such as tilt, shake, rotation, or swing Hardware based: Accelerometer Gyroscope Hardware-based or software-based: Gravity Linear acceleration Rotation vector sensors
Accelerometer Sensor Measures the acceleration applied to the device, including the force of gravity Accelerometers use the standard sensor coordinate system Uses about 10 times less power than the other motion sensors msensormanager = (SensorManager)getSystemService(Context.SENSOR_SERVICE); macceleromater = msensormanager.getdefaultsensor(sensor.type_accelerometer);
Gyroscope Sensor Measures the rate or rotation in rad/s around a device's x, y, and z axis Rotation is positive in the counter-clockwise direction mgyroscope = msensormanager.getdefaultsensor(sensor.type_gyroscope);
Gravity Sensor Provides a three dimensional vector indicating the direction and magnitude of gravity mgravity = msensormanager.getdefaultsensor(sensor.type_gravity); When a device is at rest, the output of the gravity sensor should be identical to that of the accelerometer.
Linear Accelemeter Sensor Provides a three-dimensional vector representing acceleration along each device axis, excluding gravity msensor = msensormanager.getdefaultsensor( Sensor.TYPE_LINEAR_ACCELEMETER); The sensor coordinate system is the same as the one used by the acceleration sensor, as are the units of measure (m/s 2 ).
Rotation Vector Sensor Represents the orientation of the device as a combination of an angle and an axis msensor = msensormanager.getdefaultsensor( Sensor.TYPE_ROTATION_VECTOR);
References http://developer.android.com/guide/topics/ sensors/sensors_overview.html http://developer.android.com/guide/topics/ sensors/sensors_motion.html
Lab Draw a ball Move a ball by tilting a device