Contents |
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:
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.
No related wiki articles found