Please note that as of October 24, 2014, the Nokia Developer Wiki will no longer be accepting user contributions, including new entries, edits and comments, as we begin transitioning to our new home, in the Windows Phone Development Wiki. We plan to move over the majority of the existing entries. Thanks for all your past and future contributions.

How to use sensors in Java ME

From Wiki
Jump to: navigation, search
Article Metadata
Code ExampleTested with
Devices(s): Nokia N97
S60 5th Edition
Created: jarmlaht (22 May 2009)
Last edited: hamishwillee (22 Jul 2013)

This article provides examples showing how to use the Java ME Mobile Sensor API (JSR-256) on S60 5th Edition



S60 5th Edition devices support Mobile Sensor API (JSR-256). Nokia N97 is the first device having it as a built-in feature. The S60 5th Edition (and most of the S60 3rd Edition FP1 and the S60 3rd Edition FP2) devices has an accelerometer, which gives values based on the device position and movement. For example, when the device screen is turned to landscape position, also the screen is updated to landscape position. In N97, in addition to the accelerometer sensor there are three other sensors: battery charge level sensor, charger state sensor and network field intensity sensor.

As shown in the How to get information about sensors in Java ME, in Nokia N97 Mobile Sensor API finds five different sensors. This article shows, how to get values from the battery charge level sensor, charger state sensor and network field intensity sensors. There is also an article, which shows, how to get values from the accelerators in a MIDlet.

Steps in using a sensor is as follows:

  • find a sensor of desired type (quantity) in the device by using SensorManager.findSensors() method
    • for example in N97 the quantities of the sensors are "acceleration", "battery_charge", "charger_state" and "network_field_intensity"
  • select the sensor from the SensorInfo array
    • get the URL to the sensor by using SensorInfo.getUrl() method
  • open SensorConnection to the sensor
  • implement the dataReceived() method for using the sensor data

Code samples

The code sample below shows, how to search a sensor of desired quantity. The method also gets the sensor URL and returns the correct SensorConnection to it.

* Searches sensors of desired quantity and if found, returns a
* SensorConnection opened to it.
* @param quantity the sensor quantity, for example "acceleration", "battery_charge",
* "charger_state" and "network_field_intensity"
* @return SensorConnection, which has been opened to a sensor matching the criteria

private SensorConnection openSensor(String quantity) {
infos = SensorManager.findSensors(quantity, null);
if (infos.length==0) return null;
String sensor_url = infos[0].getUrl();
try {
return (SensorConnection);
}catch (IOException ioe) {
return null;

The following method shows, how to set DataListener for listening sensor values from the three sensors:

* Initializes (opens) the sensor connections and sets the DataListener.
* Takes also care of removing the DataListeners and closing the connections.

private synchronized void initSensors() {
batterySensor = openSensor(BATTERY);
if (batterySensor == null) return;
chargeSensor = openSensor(CHARGER);
if (chargeSensor == null) return;
networkSensor = openSensor(NETWORK);
if (networkSensor == null) return;
try {
batterySensor.setDataListener(this, BUFFER_SIZE);
chargeSensor.setDataListener(this, BUFFER_SIZE);
networkSensor.setDataListener(this, BUFFER_SIZE);
}catch(InterruptedException ie){}
}catch (IllegalMonitorStateException imse) {
}catch (IllegalArgumentException iae) {
try {
} catch(IOException ioe){
if (isStopped) {
batterySensor = null;
chargeSensor = null;
networkSensor = null;

Finally, the dataReceived() method is implemented for getting the sensor values. The method creates the Strings for showing the sensor values properly formatted.

* Notification of the received sensor data.
* @param sensor - SensorConnection, the origin of the received data
* @param data - the received sensor data
* @param isDataLost - true if some data has been lost

public void dataReceived(SensorConnection sensor, Data[] data, boolean isDataLost) {
sensorString = sensor.getSensorInfo().getQuantity();
int values[] = data[0].getIntValues();
if (sensorString.equals(BATTERY)) {
batteryString = "" + values[0] + "%";
else if (sensorString.equals(CHARGER)) {
int value = values[0];
if (value == 0) chargeString = "not plugged in";
else if (value == 1) chargeString = "plugged in";
else if (sensorString.equals(NETWORK)) {
networkString = "" + values[0] + "%";

There are also the .jad and .jar files available here. The MIDlet shows the sensor values on the screen, once the "Start" command is selected.

Example application

  • containing SensorTest2.jad, SensorTest2.jar and the sources

See also

This page was last modified on 22 July 2013, at 22:27.
97 page views in the last 30 days.