| ID | CS001247 | Creation date | December 18, 2008 |
| Platform | S60 3rd Edition, FP1, S60 3rd Edition, FP2, S60 5th Edition | Tested on devices | Nokia E90 Communicator (v.210.34.75 and later), Nokia 5800 XpressMusic |
| Category | Web Runtime (WRT) | Subcategory | System information |
| Keywords (APIs, classes, methods, functions): x-systeminfo-widget, SystemInfo.chargelevel, SystemInfo.chargerconnected, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo() |
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).
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.");
}
}
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;
}
}
Battery strength and charging status are obtained.
For a complete example that makes use of the SystemInfo Service API, see Media:Checking_battery_level_in_WRT.zip.
No related wiki articles found