Checking network state in Symbian Web Runtime
m (Unprotected "CS001234 - Checking network state in WRT") |
|||
| Line 9: | Line 9: | ||
|subcategory=System information | |subcategory=System information | ||
|creationdate=December 18, 2008 | |creationdate=December 18, 2008 | ||
| − | |keywords=SystemInfo.networkname, SystemInfo.networkregistrationstatus, | + | |keywords=x-systeminfo-widget, SystemInfo.signalbars, SystemInfo.networkname, SystemInfo.networkregistrationstatus, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo() |
}} | }} | ||
| − | |||
==Overview== | ==Overview== | ||
| − | |||
| − | This information | + | This code snippet shows how to obtain information (network name, signal strength, and network registration status) about the currently available network 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. |
| − | + | ||
| − | + | ||
| − | + | ||
| − | The | + | '''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 selected 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: | |
| − | + | ||
| − | + | ||
<code xml> | <code xml> | ||
| − | < | + | <body> |
| − | + | <embed type="application/x-systeminfo-widget" hidden="yes" /> | |
| + | </body> | ||
| + | </code> | ||
| − | + | Add also the components into which the information is printed: | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | < | + | <code xml> |
| − | < | + | <table> |
| − | + | <tr><td>Signal strength:</td><td id="signalStrength"></td></tr> | |
| − | + | <tr><td>Network name:</td><td id="networkName"></td></tr> | |
| − | + | <tr><td>Network state:</td><td id="networkState"></td></tr> | |
| − | + | </table> | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
</code> | </code> | ||
| − | + | After that, the information can be obtained: | |
| − | <code | + | <code javascript> |
| − | + | 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; | ||
| + | } | ||
| + | // Update network information every half second | ||
| + | setInterval(updateNetworkData, 500); | ||
} | } | ||
| − | + | ||
| − | + | function updateNetworkData() { | |
| − | + | // Get signal strength | |
| − | + | var signalStrength = sysInfo.signalbars; | |
| − | + | document.getElementById("signalStrength").innerHTML = signalStrength; | |
| − | + | ||
| − | + | // Get network name | |
| − | + | var networkName = sysInfo.networkname; | |
| − | + | document.getElementById("networkName").innerHTML = networkName; | |
| − | + | ||
| − | + | // Get network registration status of the device | |
| − | + | var networkRegistrationStatus = sysInfo.networkregistrationstatus; | |
| − | + | var statusText = ""; | |
| − | + | switch (networkRegistrationStatus) { | |
| − | + | case 0: | |
| − | + | statusText = "Unknown"; | |
| − | + | break; | |
| − | + | case 1: | |
| − | + | case 2: | |
| − | + | case 3: | |
| − | + | statusText = "Not registered"; | |
| − | + | break; | |
| − | + | case 4: | |
| − | + | statusText = "Registered, network busy"; | |
| − | + | break; | |
| − | + | case 5: | |
| − | + | statusText = "Registered on home network"; | |
| − | + | break; | |
| − | + | case 6: | |
| − | + | statusText = "Registration denied"; | |
| − | + | break; | |
| − | + | case 7: | |
| − | + | statusText = "Registered on visited network (roaming)"; | |
| − | + | break; | |
| − | + | default: | |
| − | + | statusText = "Unknown"; | |
| − | + | break; | |
| − | + | } | |
| − | + | ||
| − | } | + | document.getElementById("networkState").innerHTML = statusText; |
| − | + | ||
| − | . | + | |
| − | + | ||
| − | + | ||
} | } | ||
</code> | </code> | ||
| − | ==Source | + | ==Source (WRT 1.1)== |
<code javascript> | <code javascript> | ||
| − | + | var serviceObj = null; | |
| − | + | window.onload = init; | |
| − | + | ||
| − | + | function init() { | |
| − | + | // Obtain the SystemInfo service object | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | function | + | |
try { | try { | ||
| − | + | serviceObj = device.getServiceObject("Service.SysInfo", "ISysInfo"); | |
| − | + | } catch (ex) { | |
| − | } catch ( | + | alert("Service object cannot be found."); |
| − | alert( " | + | return; |
} | } | ||
| − | // | + | // Update network information every half second |
| − | + | setInterval(updateNetworkData, 500); | |
| − | + | ||
| − | setInterval( updateNetworkData, | + | |
} | } | ||
| − | + | ||
function updateNetworkData() { | function updateNetworkData() { | ||
| − | // | + | // Initialize the criteria for the service object and obtain the |
| − | var | + | // information |
| − | + | var criteria = new Object(); | |
| − | + | criteria.Entity = "Network"; | |
| − | + | ||
| − | + | // Get signal strength | |
| + | criteria.Key = "SignalStrength"; | ||
| + | try { | ||
| + | var result = serviceObj.ISysInfo.GetInfo(criteria, | ||
| + | displaySignalStrength); | ||
| + | } catch (ex) { | ||
| + | alert(ex); | ||
| + | return; | ||
} | } | ||
| − | // | + | |
| − | + | // Get network name | |
| − | + | criteria.Key = "CurrentNetwork"; | |
| + | try { | ||
| + | var result = serviceObj.ISysInfo.GetInfo(criteria, | ||
| + | displayNetworkName); | ||
| + | } catch (ex) { | ||
| + | alert(ex); | ||
| + | return; | ||
} | } | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | // Get network registration status of the device | |
| − | + | criteria.Key = "RegistrationStatus"; | |
| − | + | try { | |
| − | + | var result = serviceObj.ISysInfo.GetInfo(criteria); | |
| − | // Get network | + | displayRegistrationStatus(result); |
| − | + | } catch (ex) { | |
| − | + | alert(ex); | |
| − | + | return; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
| + | } | ||
| − | + | function displaySignalStrength(transId, eventCode, result) { | |
| − | + | // On error situation, display the error message | |
| − | // | + | if (eventCode == 4) { |
| − | + | alert("Error " + result.ErrorCode + ": " + result.ErrorMessage); | |
| − | + | return; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
| − | + | var signalStrength = result.ReturnValue.Status; | |
| − | var | + | document.getElementById("signalStrength").innerHTML = |
| − | + | signalStrength + " dB"; | |
| − | + | } | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | function displayNetworkName(transId, eventCode, result) { | |
| − | + | // On error situation, display the error message | |
| − | + | if (eventCode == 4) { | |
| − | + | alert("Error " + result.ErrorCode + ": " + result.ErrorMessage); | |
| − | + | return; | |
| − | + | ||
| − | + | ||
} | } | ||
| − | + | ||
| − | + | var networkName = result.ReturnValue.NetworkName; | |
| − | + | document.getElementById("networkName").innerHTML = networkName; | |
| − | + | } | |
| − | + | ||
| − | + | function displayRegistrationStatus(result) { | |
| − | + | var networkRegistrationStatus = result.ReturnValue.Status; | |
| − | + | document.getElementById("networkState").innerHTML = | |
| − | + | networkRegistrationStatus; | |
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
| − | + | ||
} | } | ||
</code> | </code> | ||
==Postconditions== | ==Postconditions== | ||
| − | |||
| − | |||
| − | |||
| − | |||
| − | + | Information about the current network is obtained. | |
==Supplementary material== | ==Supplementary material== | ||
| + | |||
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Checking_network_state_in_WRT.zip]]. | You can view the source file and executable application in the attached ZIP archive. The archive is available for download at [[Media:Checking_network_state_in_WRT.zip]]. | ||
| − | [[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:S60 5th Edition]] | + | [[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:S60 3rd Edition, Feature Pack 1]][[Category:S60 3rd Edition, Feature Pack 2]][[Category:S60 5th Edition]] |
Revision as of 10:12, 16 January 2009
Article Metadata
Tested with
Compatibility
Article
Overview
This code snippet shows how to obtain information (network name, signal strength, and network registration status) about the currently available network 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 selected 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>
Add also the components into which the information is printed:
<table>
<tr><td>Signal strength:</td><td id="signalStrength"></td></tr>
<tr><td>Network name:</td><td id="networkName"></td></tr>
<tr><td>Network state:</td><td id="networkState"></td></tr>
</table>
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;
}
// Update network information every half second
setInterval(updateNetworkData, 500);
}
function updateNetworkData() {
// Get signal strength
var signalStrength = sysInfo.signalbars;
document.getElementById("signalStrength").innerHTML = signalStrength;
// Get network name
var networkName = sysInfo.networkname;
document.getElementById("networkName").innerHTML = networkName;
// Get network registration status of the device
var networkRegistrationStatus = sysInfo.networkregistrationstatus;
var statusText = "";
switch (networkRegistrationStatus) {
case 0:
statusText = "Unknown";
break;
case 1:
case 2:
case 3:
statusText = "Not registered";
break;
case 4:
statusText = "Registered, network busy";
break;
case 5:
statusText = "Registered on home network";
break;
case 6:
statusText = "Registration denied";
break;
case 7:
statusText = "Registered on visited network (roaming)";
break;
default:
statusText = "Unknown";
break;
}
document.getElementById("networkState").innerHTML = statusText;
}
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;
}
// Update network information every half second
setInterval(updateNetworkData, 500);
}
function updateNetworkData() {
// Initialize the criteria for the service object and obtain the
// information
var criteria = new Object();
criteria.Entity = "Network";
// Get signal strength
criteria.Key = "SignalStrength";
try {
var result = serviceObj.ISysInfo.GetInfo(criteria,
displaySignalStrength);
} catch (ex) {
alert(ex);
return;
}
// Get network name
criteria.Key = "CurrentNetwork";
try {
var result = serviceObj.ISysInfo.GetInfo(criteria,
displayNetworkName);
} catch (ex) {
alert(ex);
return;
}
// Get network registration status of the device
criteria.Key = "RegistrationStatus";
try {
var result = serviceObj.ISysInfo.GetInfo(criteria);
displayRegistrationStatus(result);
} catch (ex) {
alert(ex);
return;
}
}
function displaySignalStrength(transId, eventCode, result) {
// On error situation, display the error message
if (eventCode == 4) {
alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
return;
}
var signalStrength = result.ReturnValue.Status;
document.getElementById("signalStrength").innerHTML =
signalStrength + " dB";
}
function displayNetworkName(transId, eventCode, result) {
// On error situation, display the error message
if (eventCode == 4) {
alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
return;
}
var networkName = result.ReturnValue.NetworkName;
document.getElementById("networkName").innerHTML = networkName;
}
function displayRegistrationStatus(result) {
var networkRegistrationStatus = result.ReturnValue.Status;
document.getElementById("networkState").innerHTML =
networkRegistrationStatus;
}
Postconditions
Information about the current network is obtained.
Supplementary material
You can view the source file and executable application in the attached ZIP archive. The archive is available for download at Media:Checking_network_state_in_WRT.zip.

