Using the Mobile Sensor API

To receive data measurements in a MIDlet, you need to identify the correct sensor and open a connection to that sensor. Sensors are identified by their URL that starts with the sensor: prefix. A sensor connection is opened with the javax.microedition.io.Connector.open() method.

Figure: Opening a sensor connection

An opened sensor can function in two modes.

  • Synchronous mode - the MIDlet polls the sensor with the SensorConnection.getData() method to get the requested data in necessary intervals.

  • Asynchronous mode - the MIDlet listens to DataListener.dataReceived() notifications from the sensor. Listening may start when the sensor connection is opened or only when preset conditions have been met.

After using a sensor, you need to close its sensor connection.

Figure: Lifecycle for an asynchronous sensor connection

For more details, see the following sections: