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 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:

Table: Common ChannelInfo search criteria

Criterion

Description

Value

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.

Possible values:

Measurement range

All measurement ranges for this channel. Measurement ranges are the resolution and the possible largest and smallest values of the sensor.

See the MeasurementRange class specification.

Unit

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

See table SI units in the Unit class specification.

Table: Common SensorInfo search criteria

Criterion

Description

Value

Context type

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

Possible values:

  • AMBIENT

  • DEVICE

  • USER

  • VEHICLE

Model

The model of the sensor specified by the vendor.

Sensor model as a string

URL

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

URL as a string

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

Example: Identifying data types

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