Finding Sensors

An Android device can include multiple implementations of a particular sensor-type. To find the default Sensor implementation for a particular type use the Sensor Manager's getDefaultSensor method, passing in the sensor-type required from the constants described in the previous section.

The following snippet returns the default gyroscope. If no default Sensor exists for the given type, the method returns null.

Sensor defaultGyroscope = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE);

Alternatively, use getSensorList to return a list of all the available Sensors of a given type, as shown in the following code, which returns all the available pressure sensor objects:

List<Sensor> pressureSensors = sensorManager.getSensorList(Sensor.TYPE_PRESSURE);

To find every Sensor available on the host platform use getSensorList, passing in Sensor.TYPE_ALL, as shown here:

List<Sensor> allSensors = sensorManager.getSensorList(Sensor.TYPE_ALL); This technique lets you determine which Sensors, and sensor-types, are available on the host platform.

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide

Post a comment