Example: Using the accelerometer

This example shows you how to use sensors in a MIDlet. It describes the basics of sensor operation, such as opening a sensor connection, listening to sensor values, and closing a sensor connection.

The example MIDlet receives sensor data from the accelerometer sensor and translates it to key press events and a visual indicator. The accelerometer-to-key-press functionality can be replicated in other MIDlets to control an object on the screen, for example. Overall, incorporating features such as the orientation detection described in this example to legacy MIDlets is a straightforward task since the sensors function only as data sources from the perspective of a MIDlet.

An accelerometer returns the state of the mobile device's rotation in three axes: x, y, z. The example MIDlet uses these return values to determine the orientation and rotation of the device.

Figure: Three axes of a device

For an tutorial on creating the example ArrowKeys MIDlet, see the following sections:

For more examples, see Appendix D of the Mobile Sensor API specification. Appendix E contains a useful definition for the acceleration sensor.