Checking battery level in Symbian Web Runtime
Article Metadata
Code Example
Tested with
Compatibility
Article
Contents |
Overview
This code snippet demonstrates how to obtain the charge level and charger connection state of a device by using the SystemInfo Service API of the Web Runtime (WRT). 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: 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.

