Top Banner
Sensors in android
26

Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Jan 29, 2016

Download

Documents

Brendan Hampton
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Sensors in android

Page 2: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

App being more applicable

• Keeping track of your heart beat while jogging.• Pointing the phone camera towards the night sky to know the name

of the constellation in focus.• Tilting the device to control the car wheel in a racing game.• Checking the current altitude while hiking.

All these suggest that most likely the mobile app here are using

sensors to provide you with a seventh sense.

Page 3: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

What are sensors?Sensors are not just one that make a device smarter

• Sensors are components of a device that are capable of detecting and measuring changes in & around the device.• Various smartphone sensors Global Positioning System(GPS) gyroscope proximity sensors Near Field Communication(NFC) gravity sensors electronic compass

Page 4: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Look through

• Various sensors supported by the Android platform

• Explore the android sensor framework that let us access these sensors.

Page 5: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• Android platform classifies sensors into motion, position & environment sensors

AccelerometerGyroscope

Rotation Vector sensorGravity sensor

Proximity sensorOrientation sensor

Magnetometer

Light sensorsAmbient temperature

sensorPressure sensorHumidity sensor

Motion sensors Position sensors Environment sensors

Page 6: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• Motion sensors-detect device movement by measuring acceleration & rotational forces along the three dimensions.

• Position sensors-measure the physical position of the device.

• Environment sensors-detect changes in the environmental parameters such as temperature, humidity & pressure.

Page 7: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• Sensors such as gyroscope & temperature have a physical component plugged in the device circuitry.so called hardware sensors.

• Gravity & orientation rely on hardware sensors for raw data & process this data to provide some unique sensory information. They are known as software sensors.

Page 8: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Let us briefly discuss common device sensors• Accelerometer: motion sensor;measure acceleration in any of the 3

dimensions.used to detect events such as shake & move.• Gyroscope: motion sensor; angular acceleration.used to detect event

such as tilt & rotate.so more useful for gaming app where there is a need for high-precision tracking of rotation,roll.• Proximity sensors: position sensor ;use to determine how close the

device is to users face.commonly used to turn off the screen when user answers a call.thus ensures longer battery life & prevent any accidental inputs.

Page 9: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• Magnetometer: position sensor; detect device orientation with respect to the earth magnetic field.• Light sensors: environment sensor used to provide appropriate screen

brightness by optimizing it to the external environment. Saves power & a soothing experience to the user’s eyes.

Page 10: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Sensor Framework

• You can access these sensors and acquire raw sensor data by using the Android sensor framework. • The sensor framework is part of the android.hardware package and

includes the following classes and interfaces:

Page 11: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

SensorManager

• 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.

Page 12: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

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.

Page 13: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

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, and the timestamp for the event.

Page 14: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

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.

Page 15: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

In a typical application you use these sensor-related APIs to perform two basic tasks:• Identifying sensors and sensor capabilities.• Monitor sensor events

Page 16: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Identifying Sensors and Sensor Capabilities• The Android sensor framework provides several methods that make it

easy for you to determine at runtime which sensors are on a device. • To identify the sensors that are on a device you first need to get a

reference to the sensor service. • To do this, you create an instance of the SensorManager class by

calling the getSystemService() method and passing in the SENSOR_SERVICE argument.

Page 17: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• Next, you can get a listing of every sensor on a device by calling the getSensorList() method and using the TYPE_ALL constant.

If you want to list all of the sensors of a given type, you could use another constant instead of TYPE_ALL such as TYPE_GYROSCOPE, TYPE_LINEAR_ACCELERATION, or TYPE_GRAVITY.

Page 18: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• You can also determine whether a specific type of sensor exists on a device by using the getDefaultSensor() method and passing in the type constant for a specific sensor. • If a device has more than one sensor of a given type, one of the

sensors must be designated as the default sensor. • If a default sensor does not exist for a given type of sensor, the

method call returns null, which means the device does not have that type of sensor.

Page 19: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

The following code checks whether there's a magnetometer on a device:

Page 20: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Monitoring Sensor Events

• To monitor raw sensor data you need to implement two callback methods that are exposed through the SensorEventListener interface: onAccuracyChanged() and onSensorChanged().• The Android system calls these methods whenever the following

occurs:A sensor's accuracy changes. A sensor reports a new value.

Page 21: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

A sensor's accuracy changes.

• In this case the system invokes the onAccuracyChanged() method, providing you with a reference to the Sensor object that changed and the new accuracy of the sensor. Accuracy is represented by one of four status constants: SENSOR_STATUS_ACCURACY_LOW, SENSOR_STATUS_ACCURACY_MEDIUM, SENSOR_STATUS_ACCURACY_HIGH, or SENSOR_STATUS_UNRELIABLE.

Page 22: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

A sensor reports a new value.

• In this case the system invokes the onSensorChanged() method, providing you with a SensorEvent object. • A SensorEvent object contains information about the new sensor

data, including: the accuracy of the data, the sensor that generated the data, the timestamp at which the data was generated, and the new data that the sensor recorded.

Page 23: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

Accelerometer

• An acceleration sensor measures the acceleration applied to the device, including the force of gravity.• to get an instance of the default acceleration sensor:

Page 24: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• An acceleration sensor determines the acceleration that is applied to a device (Ad) by measuring the forces that are applied to the sensor itself (Fs) using the following relationship:

• Ad = - ∑Fs / mass• The force of gravity is always influencing the measured acceleration

according to the following relationship:

• Ad = -g - ∑F / mass

Page 25: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.

• when the device is sitting on a table (and not accelerating), the accelerometer reads a magnitude of g = 9.81 m/s2. • when the device is in free fall and therefore rapidly accelerating

toward the ground at 9.81 m/s2, its accelerometer reads a magnitude of g = 0 m/s2. • To measure the real acceleration of the device, the contribution of the

force of gravity must be removed from the accelerometer data. • This can be achieved by applying a high-pass filter. • Conversely, a low-pass filter can be used to isolate the force of gravity.

Page 26: Sensors in android. App being more applicable Keeping track of your heart beat while jogging. Pointing the phone camera towards the night sky to know.