Sensors in Android Types of Sensors Motion Sensor

  • Slides: 14
Download presentation
Sensors in Android

Sensors in Android

Types of Sensors Motion Sensor Accelerometers Gravity sensors Gyroscopes Rotational vector sensors Environmental Sensors

Types of Sensors Motion Sensor Accelerometers Gravity sensors Gyroscopes Rotational vector sensors Environmental Sensors Barometers Photometers Thermometers Position sensors Orientation sensors Magnetometers

Sensors in Android Sensor TYPE_ ACCELEROMETER Type Description Hardware Measures the acceleration force in

Sensors in Android Sensor TYPE_ ACCELEROMETER Type Description Hardware Measures the acceleration force in m/s 2 that is Motion detection (shake, tilt, applied to a device on all three physical axes (x, etc. ). y, and z), including the force of gravity. TYPE_ Hardware AMBIENT_TEMPERATURE TYPE_ GRAVITY Measures the ambient room temperature in degrees Celsius (°C). Common Uses Monitoring air temperatures. Measures the force of gravity in m/s 2 that is Software or Motion detection (shake, tilt, applied to a device on all three physical axes (x, Hardware etc. ). y, z). TYPE_ GYROSCOPE Hardware Measures a device's rate of rotation in rad/s around each of the three physical axes (x, y, and z). TYPE_ LIGHT Hardware Measures the ambient light level (illumination) Controlling screen brightness. in lx. Rotation detection (spin, turn, etc. ). Measures the acceleration force in m/s 2 that is TYPE_ Software or Monitoring acceleration along applied to a device on all three physical axes (x, LINEAR_ACCELERATION Hardware a single axis. y, and z), excluding the force of gravity. TYPE_ MAGNETIC_FIELD Hardware Measures the ambient geomagnetic field for all Creating a compass. three physical axes (x, y, z) in μT.

Sensors in Android TYPE_ ORIENTATION Software Measures degrees of rotation that a device makes

Sensors in Android 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 Determining device position. rotation matrix for a device by using the gravity sensor and the geomagnetic field sensor in conjunction with the get. Rotation. Matrix() method. TYPE_ PRESSURE Hardware Measures the ambient air pressure in h. Pa or mbar. Hardware Measures the proximity of an object in cm relative to the view screen of a device. This sensor is typically Phone position during a call. used to determine whether a handset is being held up to a person's ear. TYPE_ PROXIMITY TYPE_ Hardware RELATIVE_HUMIDITY Measures the relative ambient humidity in percent (%). TYPE_ Software or Measures the orientation of a device by providing ROTATION_VECTOR Hardware three elements of the device's rotation vector. TYPE_ TEMPERATURE Hardware Measures the temperature of the device in degrees Celsius (°C). This sensor implementation varies across devices and this sensor was replaced with the TYPE_AMBIENT_TEMPERATURE sensor in API Level 14 Monitoring air pressure changes. Monitoring dewpoint, absolute, and relative humidity. Motion detection and rotation detection.

Android Sensor Framework Determining what sensors are available Determining the sensor’s capability Defining min

Android Sensor Framework Determining what sensors are available Determining the sensor’s capability Defining min sampling rate Acquiring raw sensor data Registering sensor event listeners

Sensor Framework Sensor. Manager Accessing/listing sensors Registering/unregistering sensor event listeners Acquiring orientation information Sensor

Sensor Framework Sensor. Manager Accessing/listing sensors Registering/unregistering sensor event listeners Acquiring orientation information Sensor Determining the capability of sensors Sensor. Event Providing information about sensor events Including raw sensor data, sensor, the accuracy, and timestamp. Sensor. Event. Listener Containing callbacks to receiving notifications of sensor events

Identifying Sensors 1. Get a Sensor. Manager m. SM = (Sensor. Manager)get. System. Service(Context.

Identifying Sensors 1. Get a Sensor. Manager m. SM = (Sensor. Manager)get. System. Service(Context. SENSOR_SERVICE); 2. Get a list of all sensors available on device List<Sensor> sensors = m. SM. get. Sensor. List(Sensor. TYPE_ALL); 3. Get a list of all sensors of a given type List<Sensor> sensors = m. SM. get. Sensor. List(Sensor. TYPE_GRAVITY); 4. Get the default sensor of a given type Multiple sensors of the same type One must be assigned as default If (m. SM. get. Default. Sensor(Sensor. TYPE_GRAVITY) != null) { // sensor available } else { // no gravity sensor available }

Getting Sensor Information Sensor class offers methods to get sensor info get. Resolution() get.

Getting Sensor Information Sensor class offers methods to get sensor info get. Resolution() get. Maximum. Range() get. Vendor() get. Version() get. Min. Delay(): determines the max rate of sampling > 0: streaming sensor = 0: reporting data only when there is a change

Monitoring Sensor Events Sensor. Event. Listener on. Accuracy. Changed(Sensor s, int accuracy) called when

Monitoring Sensor Events Sensor. Event. Listener on. Accuracy. Changed(Sensor s, int accuracy) called when the accuracy of the sensor has changed SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_MEDIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE. on. Sensor. Changed(Sensor. Event se) Called with a Sensor. Event object for new sensor data The accuracy of the data, The sensor, the timestamp of the change, and new data

Registering Listeners -1 public class Sensor. Activity extends Activity implements Sensor. Event. Listener {

Registering Listeners -1 public class Sensor. Activity extends Activity implements Sensor. Event. Listener { private Sensor. Manager m. Sensor. Manager; private Sensor m. Light; public final void on. Create(Bundle saved. Instance. State) { super. on. Create(saved. Instance. State); set. Content. View(R. layout. main); m. Sensor. Manager = (Sensor. Manager) get. System. Service(Context. SENSOR_SERVICE); m. Light = m. Sensor. Manager. get. Default. Sensor(Sensor. TYPE_LIGHT); } public final void on. Accuracy. Changed(Sensor sensor, int accuracy) { // Do something here if sensor accuracy changes. } public final void on. Sensor. Changed(Sensor. Event 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. }

Registering Listeners - 2 public class Sensor. Activity extends Activity implements Sensor. Event. Listener

Registering Listeners - 2 public class Sensor. Activity extends Activity implements Sensor. Event. Listener { … protected void on. Resume() { super. on. Resume(); m. Sensor. Manager. register. Listener(this, m. Light, Sensor. Manager. SENSOR_DELAY_NORMAL); // sample rate } protected void on. Pause() { super. on. Pause(); // if not unregistered, the sensor will continue polling data // which would drain the battery m. Sensor. Manager. unregister. Listener(this); } }

Sensor. Event public int accuracy: The accuracy of this event. public Sensor sensor: The

Sensor. Event public int accuracy: The accuracy of this event. public Sensor sensor: The sensor that generated this event. public long timestamp: The time in nanosecond at which the event happened public final float[] values: raw values of the sensor data. The length and contents of the values are sensor dependent

Sensor Coordinate System Default orientation X axis – horizontal pointing right Y axis –

Sensor Coordinate System Default orientation X axis – horizontal pointing right Y axis – vertical pointing up Z axis – pointing toward the outside of the screen face. Coordinate system does not change when orientation changes You can use get. Orientation() to determine screen rotation and use remap. Coordinate. System() to map sensor coordinates to screen coordinates.

Best Practices Unregister sensor listeners Unregistering listners in on. Pause(). Otherwise the sensor continues

Best Practices Unregister sensor listeners Unregistering listners in on. Pause(). Otherwise the sensor continues acquiring data Don’t test your code on the emulator Emulator cannot accurately emulate sensors Don’t block the on. Sensor. Changed() When sensor data changes often, this method is called often. Keep it as short as possible. Avoid using deprecated methods/sensors Verify sensor’s availability before using them Not all devices provide all sensors Choose sensor delays carefully Specify the largest delay that is fast enough for your app.