Android Sensors PDF
Android Sensors PDF
Android Sensors PDF
Devices
CS 495/595 - Fall 2012
Lec #5: Android Sensors
Tamer Nadeem Dept. of Computer Science
Page 2
Objective
• Working in Background
• Sensor Manager • Sensor Types
• Examples
CS 495/595 - App Development for Smart Devices Fall 2013
Page 3
What is a Sensor
• A converter that measures a physical quantity
and converts it into a signal which can be read
by an observer or by an instrument ...
• Sensors have been used in cellphones since
they were invented ...
• Microphone, number keys
• Instead of carrying around 10 separate devices,
now you just need 1
CS 495/595 - App Development for Smart Devices Fall 2013
Page 4
Android Sensors
• MIC
• Camera
• Temperature
• Location (GPS or Network)
• Orientation
• Accelerometer
• Proximity
• Pressure
• Light
CS 495/595 - App Development for Smart Devices Fall 2013
Page 5
Android.hardware Package
Support for Hardware classes with some
interfaces
• Camera: used to set image capture settings, start/stop
preview, snap pictures, and retrieve frames for encoding
for video.
• Camera.CameraInfo: Information about a camera
• Camera.Parameters: Camera service settings.
• Camera.Size: Image size (width and height dimensions).
• GeomagneticField: Estimate magnetic field at a given
point on Earth and compute the magnetic declination from
true north.
• Sensor: Class representing a sensor.
• SensorEvent: Represents a Sensor event and holds
information such as sensor's type, time-stamp, accuracy
and sensor's data.
• SensorManager: SensorManager lets you access the
device's sensors.
http://developer.android.com/reference/android/hardware/packa
ge-summary.html
CS 495/595 - App Development for Smart Devices Fall 2013
Page 6
Async Callbacks
• Android’s sensors are controlled by external
services and only send events when they choose to
• An app must register a callback to be notified of
a sensor event
• Each sensor has a related XXXListener interface
that your callback must implement
• E.g. LocationListener
CS 495/595 - App Development for Smart Devices Fall 2013
SensorManager Your App
Register Callback
Sensor Event
Sensor Event
Sensor Event
Page 18
SensorManager’s Methods
• Sensor getDefaultSensor(int type) Use this
method to get the default sensor for a given type
• List<Sensor> getSensorList(int type) Use this
method to get the list of available sensors of a
certain type
• boolean
registerListener(SensorEventListener
listener, Sensor sensor, int rate) Registers a
SensorEventListener for the given sensor.
• void
unregisterListener(SensorEventListener
listener, Sensor sensor) Unregisters a listener
for the sensors with which it is registered.
http://developer.android.com/reference/android/hardware/SensorM
anager.html
CS 495/595 - App Development for Smart Devices Fall 2013
Page 21
Sensor’s Methods
• public float getMaximumRange () -
maximum range of the sensor in the sensor's
unit.
• public int getMinDelay () - the minimum
delay allowed between two events in
microsecond or zero if this sensor only
returns a value when the data it's measuring
changes.
• public String getName () - name string of
the sensor.
• public float getPower () - the power in mA
used by this sensor while in use.
• public float getResolution () - resolution of
the sensor in the sensor's unit.
CS 495/595 - App Development for Smart Devices Fall 2013
Page 22
getPower() Methods
• The device’s battery has a 1500 mA
• Under normal use, the battery lasts
10hours.
• If we use orientation, rotation vector, &
magnetic field sensors
• How long would it last now?
CS 495/595 - App Development for Smart Devices Fall 2013
Page 23
• Accuracy:
– SensorManager.SENSOR_STATUS_ACCURACY_LOW –
SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM
– SensorManager.SENSOR_STATUS_ACCURACY_HIGH –
SensorManager.SENSOR_STATUS_ACCURACY_UNRELIA
BL
CS 495/595 - App Development for Smart Devices Fall 2013
SensorEvent
• SensorEvent parameter in the
onSensorChanged method includes four
properties used to describe a Sensor event:
• sensor: The sensor that triggered the event.
• accuracy: The accuracy of the Sensor when the event
occurred.
• values: A float array that contains the new value(s)
detected.
• timestamp: The time in nanosecond at which the event
occurred.
Page 25
CS 495/595 - App Development for Smart Devices Fall 2013
Page 26
Sensor Values
CS 495/595 - App Development for Smart Devices Fall 2013
Page 27
Register
// Usually in onResume Sensor sensor =
sensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
sensorManager.registerListener(mySensorEventListener, sensor,
SensorManager.SENSOR_DELAY_NORMAL);
// Usually in onPause
sensorManager.unregisterListener(mySensorEventListener);
• Update Rate:
– SensorManager.SENSOR_DELAY_FASTEST –
SensorManager.SENSOR_DELAY_GAME –
SensorManager.SENSOR_DELAY_NORMAL –
SensorManager.SENSOR_DELAY_UI
CS 495/595 - App Development for Smart Devices Fall 2013
Accelerometer, Compass, &
Orientation
• Allow you to:
< Determine the current device orientation < Monitor and
track changes in orientation < Know which direction the
user is facing < Monitor acceleration—changes in
movement rate—in any direction
• Open possibilities for your applications:
➤Use these with a map, camera, and location-based
services to create augmented reality interfaces. ➤Create
user interface that adjust dynamically to suit device
orientation. ➤Monitor rapid acceleration to detect if a
device is dropped or thrown. ➤Measure movement or
vibration (e.g., locking application). ➤User interface
controls that use physical gestures and movement.
Page 28
CS 495/595 - App Development for Smart Devices Fall 2013
Page 29
Accelerometer Data
CS 495/595 - App Development for Smart Devices Fall 2013
Collecting Sensor Data
How we have collected the data
Walking Experiment
●
Gyroscope Reading
Detected Stride
Walking Experiment
●
Controlling Vibration
• Vibration is an excellent way to provide haptic
user feedback.
• Applications needs the VIBRATE permission
in application manifest:
<uses-permission android:name="android.permission.VIBRATE"/>
• Example:
String vibratorService = Context.VIBRATOR_SERVICE; Vibrator
vibrator = (Vibrator)getSystemService(vibratorService);
long[] pattern = {1000, 2000, 4000, 8000, 16000 };
vibrator.vibrate(pattern, 0); // Execute vibration pattern.
vibrator.vibrate(1000); // Vibrate for 1 second.
CS 495/595 - App Development for Smart Devices Fall 2013
Page 36
Questions?
CS 495/595 - App Development for Smart Devices Fall 2013
To DO
• Example 1 (in slides)
• Example 2 (in slides)
• Example 3 (in slides)
• Assignment #3: Assignment Tracker App
v2.0
Page 37
CS 495/595 - App Development for Smart Devices Fall 2013
Example 1. Displaying Accelerometer and Orientation
Data
• Create an activity with accelerometer and orientation data.
package com.exercise.AndroidSensorList;
import android.app.ListActivity; import android.content.Context; import
android.hardware.Sensor; import android.hardware.SensorManager; import
android.os.Bundle; import android.widget.ArrayAdapter;
public class SensorTest extends Activity implements SensorEventListener {
SensorManager sensorManager = null;
//for accelerometer values TextView outputX; TextView outputY; TextView
outputZ;
//for orientation values TextView outputX2; TextView outputY2; TextView
outputZ2;
Page 38
CS 495/595 - App Development for Smart Devices Fall 2013
Page 39