Finding and identifying sensors

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:

Table: Common ChannelInfo search criteria

Criterion

Description

Values

Data type

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.

Unit

The unit in which data values are presented. These are usually derived from the Unit class.

See table SI units in Unit class description.

Measurement range

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 classMeasurementRange

Table: Common SensorInfo search criteria

Criterion

Description

Values

Context

The context type of the sensor. Context divides sensor types into four categories depending on the environment where they are used.

  • USER

  • DEVICE

  • AMBIENT

  • VEHICLE

Model

The model of the sensor specified by vendor specific.

Returns the sensor model as a String

URL

The URL of the sensor. It starts with the sensor: prefix and contains at least the quantity, model, and context type of the sensor.

Returns the URL as a String

For a full list of searchable criteria, see ChannelInfo and SensorInfo.

Example: identifying data types

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;
        }
    }
}