To start using the Mobile Sensor API, you need to find the available
sensors to be used. This is done with the SensorManager.findSensors
method. You
can find the sensor by specifying quantity or context, or by entering
a definite sensor URL describing the desired sensor properties. These
are described in the following example:
// Find all sensors SensorInfo[] sensorInfos = SensorManager.findSensors(null, null); // Find all acceleration sensors, the contextType is left undefined SensorInfo[] sensorInfos = SensorManager.findSensors("acceleration", null); // Or with URL SensorInfo[] sensorInfos = SensorManager.findSensors("sensor:acceleration");
Depending on the search parameters, findSensors
can return several matching SensorInfo
objects. To find the correct
sensor, you may need to refine your search further by some of the
following criteria:
Criterion |
Description |
Value |
---|---|---|
The data type of the channel. This is one of the most useful criteria as it is very common for channels to use different data types. |
Possible values: |
|
All measurement ranges for this channel. Measurement ranges are the resolution and the possible largest and smallest values of the sensor. |
See the |
|
The unit in which data values are presented. These are usually
derived from the |
See table SI units in the |
Criterion |
Description |
Value |
---|---|---|
The context type of the sensor. Context divides sensor types into four categories depending on the environment where they are used. |
Possible values:
|
|
The model of the sensor specified by the vendor. |
Sensor model as a string |
|
The URL of the sensor. The URL starts with the |
URL as a string |
For a full list of searchable criteria, see ChannelInfo
and SensorInfo
.
The following code snippet determines
the number of available channels in the sensor and handles them according
to data type. The example uses the DataListener
interface to retrieve the
sensor data. For more information, see section Using data listeners.
public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost ) { ChannelInfo[] channelinfo = sensor.getSensorInfo().getChannelInfos(); for(int i = 0; i < channelinfo.length; i++) { switch (channelinfo[i].getDataType()) { case ChannelInfo.TYPE_INT: displayData(data[i].getIntValues()); break; case ChannelInfo.TYPE_DOUBLE: displayData(data[i].getDoubleValues()); break; case ChannelInfo.TYPE_OBJECT: displayData(data[i].getObjectValues()); break; } } }