Module 0268: Accessing sensors from an Android App

Tak Auyeung, Ph.D.

February 27, 2017

1 About this module

2 Introduction to Sensors

An Android sensor is a device that measures some environmental parameter and convert the physical quantity of the parameter into a number that can be read from an app. Typical examples include the following:

For more details, visit the Android Developer’s page on sensors.

In Linux, the underlying OS of Android, reading a hardware device is an involved process because Linux and Unix systems tend to see everything as a “file”. A bit of work is needed to gain access to special devices like sensors. The Android platform, on the other hand, simplifies access to sensors from a file-based operation to an event-based operation.

Because an app is, for the most part, an event-driven program, event-driven sensor access makes it easier to develop code to access sensors. In general, the following steps allow an app access a sensor:

3 A sample program

A sample program is written to demonstrate how to make use of sensors. This program has a single button to trigger the listing of all available sensors. In the list view, clicking an item displays the value of the chosen sensor. Value changes are updated dynamically. It is a simple app in many ways, but it illustrates coding techniques of using sensors as well as using the ListView class and its associated classes.