Android Sensors With Examples

In this tutorial, we’ll be digging deep into the world of Android Sensors. Our smartphones are much more than just speed, UI and animations.

Android Sensors

Android Sensors contains properties to detect changes in the environment such as light, proximity, rotation, movements, magnetic fields and much more.

Broadly speaking, the Android Sensors fall under the following categories:

  • Environment Sensors
  • Motion Sensors
  • Orientation and Position Sensors

To access the various Sensors in Android, you must use the SensorManager class.

Following code shows how to initialize a SensorManager:

Next, you can use the Sensor class to instantiate the specific Sensor.

Next, you can register the sensor using the following code:

It’s recommended to register the listener in the onResume() method and unregister it in the onPause method in order to save battery power.

In order to listen to sensor events, you can implement the SensorEventListener interface in the activity.

You need to override the following methods for that:

To get a list of all the available sensors on the device, use the following code:

In the next section, we’ll discuss some of the common types of Sensors.

Android Sensors Types

  • Photometer is used to sense and control the brightness. Besides, there are sensors for pressure, humidity, and temperature.
  • For movements, Accelerometer is used to detect shakes/tilt gestures.
  • Proximity Sensors are used to detect how close the object is to the device. It’s commonly present in Call Applications. As you bring the phone close to the ear, the screen goes black, thanks to this sensor. Though the maximum range of proximity is 5 cms.
  • Gyroscope is used to measure rotation/spin. Gravity sensors are used to measure the force of gravity.
  • Magneto Meter is used to get the device position.
  • Pedometer is used to detect the number of steps the user takes.

Following image depicts a few sensor types and their event data values with the respective formats.

android-sensors-environment

 

Android Sensors Types

How to Check if Android Sensor is Available?

Some devices don’t support certain Sensors. So you can simply add the permissions in the Manifest file.
Google ensures that the Application on the Play store would not be visible to users with unsupported devices.

Accelerometer is not available on Android Emulators.

In the next section, we’ll implement certain sensors in our Android Application.

Android Sensors Example Project Structure

android-sensors-environment

 

Android Sensor Example Project Structure

Android Sensors Example Code

The code for the activity_main.xml is given below:

The code for the MainActivity.java is given below:

For the accelerometer, we need to get the position of all the three coordinates from each of the three axes. The accelerometer is so sensitive that it keeps updating these points.

In order to detect whether it’s a shake or not, we take the difference of the points within a given time frame to detect how quickly they moved.

The gyroscope detects whether the phone is rotated anticlockwise or clockwise in the z plane.

Light sensor’s hardware is located at the top of the phone to the right of the front camera lens.

The output of the above application in action is given below. Since we cannot show a live demo, we’ve just displayed the sensor value for brightness.

Android Sensors App Output

Android Sensors App Output

That brings an end to this android sensors tutorial. You can download the project from the link below.

By admin

Leave a Reply

%d bloggers like this: