You Are Here:

Community: Wiki

This page was last modified on 23 September 2009, at 16:07.

How to use sensors in Java ME

From Forum Nokia Wiki

Reviewer Approved   

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

See also

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fNFCX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxNFCE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d03X qfnZuserE5ftagQSx62121X qfnZuserE5ftagQSxnfcX qfnZuserE5ftagQSxpushregistryX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ