0% found this document useful (0 votes)
69 views

Integrate Google Maps Android

Uploaded by

Robert Le
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Integrate Google Maps Android

Uploaded by

Robert Le
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 18

FPT Software

ANDROID TRAINING
LESSON 13
Version 0.1

© Copyright 2011 FPT Software 1


Agenda

• Android Sensors
• Sensor Coordinate System
• Sensor Manager
• Monitoring Sensor Events
• Accelerometers
• Orientation

2/11/2012 Android Insights - 3 2/18


© Copyright 2011 FPT Software 2
Android Sensors

• Android provides means of accessing its hardware


through specific APIs such as a multimedia playback
library, and specific sensors library for monitoring
phone’s orientation for example.
• This module will describe how to monitor hardware
sensors to determine the device acceleration and
orientation.
• A sensor is defined as “a device that measures a
physical quantity and converts it into a signal which
can be read by an observer or by an instrument”
(Wikipedia).

© Copyright 2011 FPT Software 3


Sensor Coordinate System

• In general, the sensor framework uses a


standard 3-axis coordinate system to express
data values. For most sensors, the
coordinate system is defined relative to the
device's screen when the device is held in its
default orientation (see figure). 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.

© Copyright 2011 FPT Software 4


Sensor Coordinate System

• The most important point to understand


about this coordinate system is that the axes
are not swapped when the device's screen
orientation changes—that is, the sensor's
coordinate system never changes as the
device moves.

© Copyright 2011 FPT Software 5


Sensor Framework

• API (I.)
– Package: android.hardware
– Classes:
• SensorManager – android service
• Sensor – specific sensor
• SensorEvent – specific event of the sensor = data

© Copyright 2011 FPT Software 6


Sensor Manager

• The Sensor Manager is used to manage the


sensor hardware available on an Android
device. Use getSystemService to get a
reference to the Sensor Service as shown in
the code snippet below:
String service_name = Context.SENSOR_SERVICE;
SensorManager sensorManager =
(SensorManager)getSystemService(service_name);

© Copyright 2011 FPT Software 7


Monitoring Sensor Events

• The SensorListener interface is used to listen for


Sensor value and accuracy changes.
SensorListener mySensorListener = new SensorListener() {
public void onSensorChanged(SensorEvent event) {
// TODO Deal with sensor value changes
}
public void onAccuracyChanged(Sensor sensor, int
accuracy) {
// TODO Auto-generated method stub
}
};

© Copyright 2011 FPT Software 8


Monitoring Sensor Events

• Implement the onSensorChanged method to react to value changes.


– event.sensor.getType() identifies the sensor that triggered the event
– event.values contains the new values detected by that sensor.
• Implement onAccuracyChanged to react to changes in a sensor’s accuracy.
– The accuracy parameter indicates the new accuracy of that sensor using one
of the constants:
• SensorManager.SENSOR_STATUS_ACCURACY_HIGH Indicates that the sensor is
reporting with the highest possible accuracy.
• SensorManager.SENSOR_STATUS_ACCURACY_LOW Indicates that the sensor is
reporting with low accuracy and needs to be calibrated.
• SensorManager.SENSOR_STATUS_ACCURACY_MEDIUM Indicates that the sensor
data is of average accuracy, and that calibration might improve the readings.
• SensorManager.SENSOR_STATUS_UNRELIABLE Indicates that the sensor data is
unreliable, meaning that either calibration is required or readings are not currently
possible.

© Copyright 2011 FPT Software 9


Monitoring Sensor Events

• The Sensor Manager includes constants to help identify the sensor triggering the
change event. The following list includes the sensors for which constants are
currently defined. Some or all of these sensors will be available to your
applications depending on the hardware available on the host device:
– SensorManager.SENSOR_ACCELEROMETER Is an accelerometer sensor that returns the
current acceleration along three axes in meters per second squared (m/s2). The
accelerometer is explored in greater detail later in this chapter.
– SensorManager.SENSOR_ORIENTATION Is an orientation sensor that returns the current
orientation on three axes in degrees. The orientation sensor is explored in greater detail
later in this module.
– SensorManager.SENSOR_LIGHT Is an ambient-light sensor that returns a single value
describing the ambient illumination in lux.
– SensorManager.SENSOR_MAGNETIC_FIELD Is a sensor used to determine the current
magnetic field measured in microteslas (μT) along three axes.
– SensorManager.SENSOR_PROXIMITY Is a proximity sensor that returns a single value
describing the distance between the device and the target object in meters (m).
– SensorManager.SENSOR_TEMPERATURE Is a thermometer sensor that returns the
ambient temperature in degrees Celsius (°C).

© Copyright 2011 FPT Software 10


Monitoring Sensor Events

• register Listener with the Sensor Manager


specifying the sensor that should trigger the
Listener and the rate at which the sensor
should update, as shown in the following code
snippet:
sensorManager.registerListener(mySensorListener,
sensorMgr.getDefaultSensor
(Sensor.TYPE_ACCELEROMETER) ,
SensorManager.SENSOR_DELAY_FASTEST);

© Copyright 2011 FPT Software 11


Monitoring Sensor Events

• The Sensor Manager includes the following constants (shown in


descending order of responsiveness) to let you select a suitable update
rate:
– SensorManager.SENSOR_DELAY_FASTEST Specifies the fastest possible sensor
update rate.
– SensorManager.SENSOR_DELAY_GAME Selects an update rate suitable for use
in controlling games.
– SensorManager.SENSOR_DELAY_NORMAL Specifies the default update rate.
– SensorManager.SENSOR_DELAY_UI Specifies a rate suitable for updating UI
features.
• The rate you select is not binding; the Sensor Manager may return results
faster or slower than you specify, though it will tend to be faster. To
minimize the associated resource cost of using the sensor in your
application you should try to select the slowest suitable rate.

© Copyright 2011 FPT Software 12


Accelerometers

• Accelerometers are used to measure acceleration.


It’s important to note that accelerometers do not
measure velocity, instead, it measures changes in
acceleration over time.
• Accelerometers are unable to differentiate between
acceleration due to movement and gravity. As a
result, an accelerometer detecting acceleration on
the Z-axis (up/down) will read –9.8 m/s2 (this value
is available as the
SensorManager.STANDARD_GRAVITY constant).

© Copyright 2011 FPT Software 13


Accelerometers

• Acceleration can be measured along three directional axes:


forward–backward (longitudinal), left–right (lateral), and up–
down (vertical):
– Vertical Upward or downward, where positive represents upward
movement such as the device being lifted up.
– Longitudinal Forward or backward acceleration, where forward
acceleration is positive. This represents a device fl at on its back, facing
up, and in portrait orientation being moved along the desk in the
direction of the top of the device.
– Lateral Sideways (left or right) acceleration, where positive values
represent movement toward the right of the device, and negative
values show movement toward the left. In the same configuration as
described in longitudinal movement, positive lateral movement would
be created by moving the device along the surface to your right.

© Copyright 2011 FPT Software 14


Orientation

• The orientation sensors are a combination of a


built-in compass that provides the yaw
(heading) and the accelerometers that help
determine pitch and roll.

© Copyright 2011 FPT Software 15


Orientation

• The orientation sensor derives its data by using a device's geomagnetic


field sensor in combination with a device's accelerometer. Using these two
hardware sensors, an orientation sensor provides data for the following
three dimensions:
– Azimuth (degrees of rotation around the z axis). This is the angle between
magnetic north and the device's y axis. For example, if the device's y axis is
aligned with magnetic north this value is 0, and if the device's y axis is pointing
south this value is 180. Likewise, when the y axis is pointing east this value is
90 and when it is pointing west this value is 270.
– Pitch (degrees of rotation around the x axis). This value is positive when the
positive z axis rotates toward the positive y axis, and it is negative when the
positive z axis rotates toward the negative y axis. The range of values is 180
degrees to -180 degrees.
– Roll (degrees of rotation around the y axis). This value is positive when the
positive z axis rotates toward the positive x axis, and it is negative when the
positive z axis rotates toward the negative x axis. The range of values is 90
degrees to -90 degrees.

© Copyright 2011 FPT Software 16


Homework

• Apply Accelerometers to detect shake motion


for closing application

© Copyright 2011 FPT Software 17


Thank you!

© Copyright 2011 FPT Software 18

You might also like