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 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 |
Values |
---|---|---|
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. |
||
The unit in which data values are presented. These are usually derived
from the |
See table SI units in |
|
All measurement ranges for this channel. Measurement ranges are the resolution and the possible largest and smallest values of the sensor. |
For more information see class |
Criterion |
Description |
Values |
---|---|---|
The context type of the sensor. Context divides sensor types into four categories depending on the environment where they are used. |
|
|
The model of the sensor specified by vendor specific. |
Returns the sensor model as a String |
|
The URL of the sensor. It starts with the |
Returns the URL as a String |
For a full list of searchable criteria, see ChannelInfo
and SensorInfo
.
The following code example determines the amount of available channels
in the sensor and handles them according to data type. The example uses the DataListener
interface to retrieve the sensor data. See section Using
data listeners for more information.
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; } } }