×
Namespaces

Variants
Actions
Revision as of 10:29, 6 May 2013 by hamishwillee (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Checking battery level in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search

This code example shows how to obtain the charge level and charger connection state of a device on Symbian Web Runtime.

Article Metadata
Code ExampleTested with
Devices(s): Nokia E90 Communicator (v.210.34.75 and later), Nokia 5800 XpressMusic
Compatibility
Platform(s): S60 3rd Edition, FP1, S60 3rd Edition, FP2, S60 5th Edition
S60 5th Edition
S60 3rd Edition FP2
S60 3rd Edition FP1
Article
Keywords: x-systeminfo-widget, SystemInfo.chargelevel, SystemInfo.chargerconnected, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo()
Created: dekuykin (10 Dec 2008)
Last edited: hamishwillee (06 May 2013)

Contents

Overview

The example uses the SystemInfo Service API . There are two versions of this particular API, one for WRT 1.0 and one for WRT 1.1. Both versions are demonstrated in this snippet.

Note.pngNote: The SystemInfo Service API for WRT 1.1 is only available from S60 5th Edition onwards. The API for WRT 1.0 is supported from S60 3rd Edition, Feature Pack 2 onwards and also in select S60 3rd Edition, Feature Pack 1 devices or their newest firmware versions (for example, Nokia E90 Communicator, from v.210.34.75 onwards).

Source (WRT 1.0)

Embed the SystemInfo widget into the document:

<body>
<embed type="application/x-systeminfo-widget" hidden="yes" />
</body>

Put also two buttons onto the HTML page:

<input type="button" onclick="getChargeLevel();"
value="Display charge level" />
<input type="button" onclick="getChargingStatus();"
value="Display charging status" />

After that, the information can be obtained:

var sysInfo = null;
 
window.onload = init;
 
function init() {
// Obtain the SystemInfo object
try {
sysInfo = document.embeds[0];
} catch (ex) {
alert("SystemInfo object cannot be found.");
return;
}
}
 
function getChargeLevel() {
if (sysInfo == undefined) {
alert("SystemInfo object is undefined.");
return;
}
 
alert("Battery strength: " + sysInfo.chargelevel + " %");
}
 
function getChargingStatus() {
if (sysInfo == undefined) {
alert("SystemInfo object is undefined.");
return;
}
 
if (sysInfo.chargerconnected) {
alert("The charger is connected.");
} else {
alert("The charger is not connected.");
}
}

Source (WRT 1.1)

var serviceObj = null;
 
window.onload = init;
 
function init() {
// Obtain the SystemInfo service object
try {
serviceObj = device.getServiceObject("Service.SysInfo", "ISysInfo");
} catch (ex) {
alert("Service object cannot be found.");
return;
}
 
getBatteryStrength();
getChargingStatus();
}
 
function getBatteryStrength() {
// Initialize the criteria for the service object and obtain the
// information
var criteria = new Object();
criteria.Entity = "Battery";
criteria.Key = "BatteryStrength";
try {
var result = serviceObj.ISysInfo.GetInfo(criteria,
displayBatteryStrength);
} catch (ex) {
alert(ex);
return;
}
}
 
function displayBatteryStrength(transId, eventCode, result) {
// On error situation, display the error message
if (eventCode == 4) {
alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
return;
}
 
alert("Battery strength: " + result.ReturnValue.Status + " %");
}
 
function getChargingStatus() {
// Initialize the criteria for the service object and obtain the
// information
var criteria = new Object();
criteria.Entity = "Battery";
criteria.Key = "ChargingStatus";
try {
// Only synchronous version of the GetInfo function can be used here
var result = serviceObj.ISysInfo.GetInfo(criteria);
if (result.ReturnValue.Status == 1) {
alert("The charger is connected.");
} else {
alert("The charger is not connected.");
}
} catch (ex) {
alert(ex);
return;
}
}

Postconditions

Battery strength and charging status are obtained.

Supplementary material

For a complete example that makes use of the SystemInfo Service API, see Media:Checking battery level in WRT.zip.

This page was last modified on 6 May 2013, at 10:29.
62 page views in the last 30 days.