How to use sensors in Java ME
Article Metadata
Code Example
Tested with
Compatibility
Article
This article provides examples showing how to use the Java ME Mobile Sensor API (JSR-256) on S60 5th Edition
Contents |
Overview
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)Connector.open(sensor_url);
}catch (IOException ioe) {
ioe.printStackTrace();
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);
while(!isStopped){
try{
wait();
}catch(InterruptedException ie){}
}
batterySensor.removeDataListener();
chargeSensor.removeDataListener();
networkSensor.removeDataListener();
}catch (IllegalMonitorStateException imse) {
imse.printStackTrace();
}catch (IllegalArgumentException iae) {
iae.printStackTrace();
}
try {
batterySensor.close();
chargeSensor.close();
networkSensor.close();
} catch(IOException ioe){
ioe.printStackTrace();
}
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] + "%";
}
repaint();
}
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
- SensorTest2.zip containing SensorTest2.jad, SensorTest2.jar and the sources


23 Sep
2009
This article discusses the use of the JSR-256 Mobile Sensor API. This API has only recently been available for use on Nokia phones and devices such as the Nokia 5800 and Nokia N97 were among the first to support its use. The API provides access to a wide range of sensors, depending on the implementation of the API and the sensors which each device provides.
The article splits the process of accessing and displaying sensor data into its constituent steps, namely finding the sensor (sensors can be searched for by name), selecting the sensor from the returned list, connecting to the sensor and implementing the dataReceived method. These steps are nicely illustrated using a simple code example. The code example illustrates how to check for a battery level sensor, charger sensor (is the charger plugged in or not) and network intensity sensor. A connection is then opened to these sensors (if found) and the dataReceived method is implemented. The values of the different sensors are then displayed on the screen.
The code example is nicely documented as serves as a useful introduction to the use of the Mobile Sensor API in Java ME.