Mobile Application Development
Mobile Application Development
DEVELOPMENT
WEEK 12
OSAMA ILTAF
CONTENT
• Sensors
• Location sensor
• Motion sensor
• Environment sensor
SENSOR
• Most Android-powered devices have built-in sensors that measure motion, orientation, and
various environmental conditions. These sensors are capable of providing raw data with high
precision and accuracy and are useful if you want to monitor three-dimensional device
movement or positioning, or you want to monitor changes in the ambient environment near a
device. For example, a game might track readings from a device's gravity sensor to infer
complex user gestures and motions, such as tilt, shake, rotation, or swing. Likewise, a weather
application might use a device’s temperature sensor and humidity sensor to calculate and report
the dewpoint, or a travel application might use the geomagnetic field sensor and accelerometer to
report a compass bearing.
TYPES
• Motion sensor:
The Android platform provides several sensors that let you monitor the motion of a
device. Motion sensors measure acceleration forces and rotational forces along three axes. This category
includes accelerometers, gravity sensors, gyroscopes, and rotational vector sensors.
• Position sensor:
These sensors measure the physical position of a device. This category includes
orientation sensors and magnetometers.
TYPES
• Environmental sensor:
These sensors measure various environmental parameters, such as ambient
air temperature and pressure, illumination, and humidity. This category includes barometers,
photometers, and thermometers.
ANDROID SENSOR FRAMEWORK
You can access sensors available on the device and acquire raw sensor data by using the Android
sensor framework. The sensor framework provides several classes and interfaces that help you
perform a wide variety of sensor-related tasks. For example, you can use the sensor framework to
do the following:
• Determine which sensors are available on a device.
• Determine an individual sensor's capabilities, such as its maximum range, manufacturer, power
requirements, and resolution.
• Acquire raw sensor data and define the minimum rate at which you acquire sensor data.
INTRODUCTION SENSOR
The Android sensor framework lets you access many types of sensors. Some of these sensors are
hardware-based and some are software-based. Hardware-based sensors are physical components
built into a handset or tablet device. They derive their data by directly measuring specific
environmental properties, such as acceleration, geomagnetic field strength, or angular change.
Software-based sensors are not physical devices, although they mimic hardware-based sensors.
Software-based sensors derive their data from one or more of the hardware-based sensors and are
sometimes called virtual sensors or synthetic sensors. The linear acceleration sensor and the gravity
sensor are examples of software-based sensors.
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:
• 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.
SENSOR FRAMEWORK
• 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.
• 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.
SENSOR FRAMEWORK
• 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.
SENSOR AVAILABILITY
While sensor availability varies from device to device, it can also vary between Android versions.
This is because the Android sensors have been introduced over the course of several platform
releases.
For example, many sensors were introduced in Android 1.5 (API Level 3), but some were not
implemented and were not available for use until Android 2.3 (API Level 9). Likewise, several
sensors were introduced in Android 2.3 (API Level 9) and Android 4.0 (API Level 14). Two
sensors have been deprecated and replaced by newer, better sensors.
IDENTIFYING SENSORS
The Android sensor framework provides several methods that make it easy for you to determine at
runtime which sensors are on a device. The API also provides methods that let you determine the
capabilities of each sensor, such as its maximum range, its resolution, and its power requirements.
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 thegetSystemService() method
and passing in the SENSOR_SERVICE argument. For example:
private SensorManager sensorManager;
...
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
SPECIFIC SENSOR
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.
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. For
example, the following code checks whether there's a magnetometer on a device:
private SensorManager sensorManager;
sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
if (sensorManager.getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD) != null){ // Success! There's a
magnetometer.
} else {// Failure! No magnetometer.
}
SENSOR CAPABILITIES
• In addition to listing the sensors that are on a device, you can use the public methods of the
Sensor class to determine the capabilities and attributes of individual sensors. This is useful if
you want your application to behave differently based on which sensors or sensor capabilities are
available on a device. For example, you can use the getResolution() and getMaximumRange()
methods to obtain a sensor’s resolution and maximum range of measurement. You can also use
the getPower() method to obtain a sensor's power requirements.
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:
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.
MONITORING SENSOR EVENTS
Location user drivers allow your app to publish updates to the device’s physical location through the
Android location services. The API supports constellations of the Global Navigation Satellite System
(GNSS), such as the Global Positioning System (GPS).
GNSS modules are receive-only devices that triangulate signals from remote satellites in order to
determine an accurate physical location. Once the module collects enough satellite data to calculate an
accurate position, it has a valid location (a fix ) that it can report.
Receiver modules typically connect to the host system via UART(Universal Asynchronous
Receiver/Transmitter, but may use other forms of Peripheral I/O. For example, they may contain
additional GPIO pins to control power management or report when the module has gained or lost a fix.
HOW TO GET LOCATION OF USER?
• Adding the required permission:
Add the required permission for the user driver to your app's manifest file:
<uses-permission
android:name="com.google.android.things.permission.MANAGE_GNSS_DRIVERS" />
• Creating the driver:
The driver implementation is responsible for communicating with the connected hardware, monitoring for valid
location changes, and reporting those changes to the framework. To create a driver:
1) Create a new GnssDriver instance.
2) Register the driver with the UserDriverManager:
import com.google.android.things.userdriver.location.GnssDriver;
import com.google.android.things.userdriver.UserDriverManager;
HOW TO GET LOCATION OF USER?
The following table describes the Location attributes that a driver can report to the framework.
Attributes marked required must be included or the framework will reject the location update:
Required Attributes Optional Attributes
Accuracy Altitude
Timestamp Bearing
Latitude Speed
Longitude
CONVERTING GPS DATA
GPS is a commonly used constellation of GNSS satellites. GPS hardware typically reports location
information as ASCII strings in the NMEA standard format. Each line of data is a comma-separated
list of data values known as a sentence. While each GPS module may choose to report different
portions of the NMEA protocol, most devices send one or more of the following sentences:
• GPGGA (Fix Information): Includes position fix, altitude, timestamp, and satellite metadata.
• GPGLL (Geographic Latitude/Longitude): Includes position fix and timestamp.
• GPRMC (Recommended Minimum Navigation): Includes position fix, speed, timestamp, and
navigation metadata.
EXAMPLE
public class SensorActivity extends Activity implements SensorEventListener {
private SensorManager sensorManager;
private Sensor mLight;
@Override
public final void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
@Override
public final void onAccuracyChanged(Sensor sensor, int accuracy) {
// Do something here if sensor accuracy changes.
}
EXAMPLE
@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();
sensorManager.registerListener(this, mLight, SensorManager.SENSOR_DELAY_NORMAL);
}
@Override
protected void onPause() {
super.onPause();
sensorManager.unregisterListener(this);
}
}