×
Namespaces

Variants
Actions
(Difference between revisions)

Checking network state in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
Forum Nokia KB (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(15 intermediate revisions by 5 users not shown)
Line 1: Line 1:
__NOTOC__
+
[[Category:Symbian Web Runtime]][[Category:Code Examples]][[Category:S60 3rd Edition FP1]][[Category:S60 3rd Edition FP2]][[Category:S60 5th Edition]][[Category:Networking]]
__NOEDITSECTION__
+
{{Abstract|This code example shows how to obtain information (network name, signal strength, and network registration status) about the currently available network on Symbian Web Runtime.}}
{{KBCS}}
+
{{CodeSnippet
+
|id=CS001234
+
|platform=S60 5th Edition
+
|devices=Nokia 5800 XpressMusic
+
|category=Web Runtime (WRT)
+
|subcategory=System information
+
|creationdate=December 18, 2008
+
|keywords=SystemInfo.networkname, SystemInfo.networkregistrationstatus, SystemInfo.signalbars
+
}}
+
  
 +
{{ArticleMetaData <!-- v1.2 -->
 +
|sourcecode= [[Media:Checking network state in WRT.zip]]
 +
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
 +
|devices= Nokia 5800 XpressMusic
 +
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
 +
|platform= S60 5th Edition
 +
|devicecompatability= <!-- Compatible devices (e.g.: All* (must have GPS) ) -->
 +
|dependencies= <!-- Any other/external dependencies e.g.: Google Maps Api v1.0 -->
 +
|signing= <!-- Empty or one of Self-Signed, DevCert, Manufacturer -->
 +
|capabilities= <!-- Capabilities required by the article/code example (e.g. Location, NetworkServices. -->
 +
|keywords= x-systeminfo-widget, SystemInfo.signalbars, SystemInfo.networkname, SystemInfo.networkregistrationstatus, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo()
 +
|language= <!-- Language category code for non-English topics - e.g. Lang-Chinese -->
 +
|translated-by= <!-- [[User:XXXX]] -->
 +
|translated-from-title= <!-- Title only -->
 +
|translated-from-id= <!-- Id of translated revision -->
 +
|review-by= <!-- After re-review: [[User:username]] -->
 +
|review-timestamp= <!-- After re-review: YYYYMMDD -->
 +
|update-by= <!-- After significant update: [[User:username]]-->
 +
|update-timestamp= <!-- After significant update: YYYYMMDD -->
 +
|creationdate= 20081210
 +
|author= [[User:Dekuykin]]
 +
<!-- The following are not in current metadata -->
 +
|id= CS001234
 +
}}
  
 
==Overview==
 
==Overview==
This code snippet shows how to obtain information about the currently available networks using System Info Platform Service for S60 Web Runtime, introduced in S60 5th Edition.
 
  
This information includes:
+
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. This snippet demonstrates both versions.
- network name
+
- signal strength
+
- network registration status
+
  
The code snippet uses the <tt>JavaScript WRT SystemInfo Service API</tt>.
+
{{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).}}
  
To get required informations the following properties are used:
+
==Source (WRT 1.0)==
<tt>SystemInfo.networkname</tt> - for getting network name
+
<tt>SystemInfo.networkregistrationstatus</tt> - for getting current registration status
+
<tt>SystemInfo.signalbars</tt> - for getting signal strength
+
  
To get access to the System Info API embedded object, <tt>application/x-systeminfo-widget</tt> is used.
+
Embed the SystemInfo widget into the document:
 +
<code xml>
 +
<body>
 +
    <embed type="application/x-systeminfo-widget" hidden="yes" />
 +
</body>
 +
</code>
  
==Source file: NetworkState.html==
+
Add also the components into which the information is printed:
  
 
<code xml>
 
<code xml>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
+
<table>
    "http://www.w3.org/TR/html4/strict.dtd">
+
     <tr><td>Signal strength:</td><td id="signalStrength"></td></tr>
 
+
    <tr><td>Network name:</td><td id="networkName"></td></tr>
<html>
+
    <tr><td>Network state:</td><td id="networkState"></td></tr>
  <head>
+
</table>
     <title>
+
    Network State
+
    </title>
+
 
+
<script type="text/javascript" src="networkState.js">
+
</script>
+
 
+
    <link href="networkState.css" rel="stylesheet" type="text/css">
+
 
+
  </head>
+
 
+
  <body id="bodyId" class="bodyClass" onLoad="setup();">
+
 
+
    <embed type="application/x-systeminfo-widget" hidden="yes"></embed>
+
 
+
    <div id="mainDiv" class="mainDiv">
+
      <br>
+
      <table class="mainTable">
+
        <tr>
+
          <td class="name">
+
          SIGNAL STRENGTH
+
          </td>
+
          <td class="value" id="progressbarContainer">
+
            <table class="strengthTable" id="strengthTable">
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
              <tr>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
                <td></td>
+
              </tr>
+
            </table>
+
          </td>
+
        </tr>
+
        <tr>
+
          <td class="name">
+
            NETWORK
+
          </td>
+
          <td id = "networkName" class="value">
+
          </td>
+
        </tr>
+
        <tr>
+
          <td class="name">
+
            STATE
+
          </td>
+
          <td id="networkState" class="value">
+
          </td>
+
        </tr>
+
      </table>
+
    </div>
+
 
+
  </body>
+
 
+
</html>
+
 
</code>
 
</code>
  
==Source file: networkState.css==
+
After that, the information can be obtained:
  
<code css>
+
<code javascript>
.bodyClass {
+
var sysInfo = null;
    background-color: white;
+
    width: 100%;
+
    height: 100%;
+
    padding: 5px;
+
    margin: 0px;
+
    text-align: center;
+
    vertical-align: middle;
+
}
+
  
.mainTable, .strengthTable {
+
window.onload = init;
    color: black;
+
    background-color: white;
+
    border: inset 3px silver;
+
    border-collapse: collapse;
+
    padding: 3px;
+
    margin: 3px;
+
    text-align: center;
+
    vertical-align: middle;
+
    width: 70%;
+
}
+
  
.strengthTable {
+
function init() {
     border: 1px solid gray;
+
     // Obtain the SystemInfo object
     width: 90%;
+
     try {
     padding: 0px;
+
        sysInfo = document.embeds[0];
    margin: 0px;
+
     } catch (ex) {
     height: 50px;
+
        alert("SystemInfo object cannot be found.");
 +
        return;
 +
     }
 +
    // Update network information every half second
 +
    setInterval(updateNetworkData, 500);
 
}
 
}
 
+
.white, .green, .gray {
+
function updateNetworkData() {
     border: dotted 1px gray;
+
     // Get signal strength
     padding: 0px;
+
     var signalStrength = sysInfo.signalbars;
     margin: 0px;  
+
     document.getElementById("signalStrength").innerHTML = signalStrength;
     background-color: white;
+
      
     border-collapse: collapse;
+
     // Get network name
}
+
     var networkName = sysInfo.networkname;
 
+
     document.getElementById("networkName").innerHTML = networkName;
.name {
+
 
     font-weight: bold;
+
     // Get network registration status of the device
     color: black;
+
     var networkRegistrationStatus = sysInfo.networkregistrationstatus;
     padding: 3px;
+
     var statusText = "";
     margin: 0px;
+
     switch (networkRegistrationStatus) {
     border: solid 1px silver;
+
     case 0:
     border-collapse: collapse;
+
        statusText = "Unknown";
     text-align: center;
+
        break;
    vertical-align: middle;
+
     case 1:
     width: 80%;
+
     case 2:
}
+
     case 3:
 
+
        statusText = "Not registered";
.value {
+
        break;
     font-weight: bold;
+
     case 4:
     color: black;
+
        statusText = "Registered, network busy";
    padding: 3px;
+
        break;
     margin: 0px;
+
     case 5:
    border: solid 1px silver;
+
        statusText = "Registered on home network";
     border-collapse: collapse;
+
        break;
    text-align: center;
+
     case 6:
     vertical-align: middle;
+
        statusText = "Registration denied";
     width: 20%;
+
        break;
}
+
     case 7:
 
+
        statusText = "Registered on visited network (roaming)";
.green {
+
        break;
     background-color: green;
+
     default:
}
+
        statusText = "Unknown";
 
+
        break;
.gray {
+
     }
     background-color: #FF9966;
+
   
}
+
    document.getElementById("networkState").innerHTML = statusText;
 
+
.mainDiv {
+
text-align: center;
+
vertical-align: middle;
+
 
}
 
}
 
</code>
 
</code>
  
==Source file: networkState.js==
+
==Source (WRT 1.1)==
  
 
<code javascript>
 
<code javascript>
// networkState.js
+
var serviceObj = null;
 +
 
 +
window.onload = init;
  
// Global variable capturing the reference to System Info object.
+
function init() {
var sytemObject;
+
    // Obtain the SystemInfo service object
/**
+
* Function is called during loading of snippet.
+
* Initializes so global variable.
+
* Starts a loop which apdates information about network state.
+
*/
+
function setup() {
+
 
     try {
 
     try {
         // Take access to System Info API
+
         serviceObj = device.getServiceObject("Service.SysInfo", "ISysInfo");
        sytemObject = document.embeds[0];
+
     } catch (ex) {
     } catch (e) {
+
         alert("Service object cannot be found.");
         alert( "Setup error: " + e );
+
        return;
 
     }
 
     }
     // Interval of refreshing information in milliseconds
+
     // Update network information every half second
    var interval = 500;
+
     setInterval(updateNetworkData, 500);
    // Set interval for updating network information and data on display
+
     setInterval( updateNetworkData, interval );
+
 
}
 
}
 
+
 
function updateNetworkData() {
 
function updateNetworkData() {
     // Get signal level
+
     // Initialize the criteria for the service object and obtain the
     var level = 0;
+
    // information
     level = sytemObject.signalbars;
+
     var criteria = new Object();
      
+
     criteria.Entity = "Network";
     if (level == undefined) {
+
 
     level = 0;  
+
     // Get signal strength
 +
     criteria.Key = "SignalStrength";
 +
    try {
 +
        var result = serviceObj.ISysInfo.GetInfo(criteria,
 +
            displaySignalStrength);
 +
     } catch (ex) {
 +
        alert(ex);
 +
        return;
 
     }
 
     }
     // Check if level incorrect
+
 
     if (level < 0) {
+
     // Get network name
     level = 0;  
+
     criteria.Key = "CurrentNetwork";
 +
    try {
 +
        var result = serviceObj.ISysInfo.GetInfo(criteria,
 +
            displayNetworkName);
 +
     } catch (ex) {
 +
        alert(ex);
 +
        return;
 
     }
 
     }
     if (level > 7) {
+
 
     level = 7;  
+
     // Get network registration status of the device
 +
    criteria.Key = "RegistrationStatus";
 +
    try {
 +
        var result = serviceObj.ISysInfo.GetInfo(criteria);
 +
        displayRegistrationStatus(result);
 +
     } catch (ex) {
 +
        alert(ex);
 +
        return;
 
     }
 
     }
    // Get network name
+
}
    var networkName = sytemObject.networkname;
+
  
    if (networkName == undefined) {
+
function displaySignalStrength(transId, eventCode, result) {
        networkName = "Unknown";
+
     // On error situation, display the error message
    }
+
     if (eventCode == 4) {
 
+
         alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
     // Get network registartion status of the device
+
         return;
    var networkRegistrationStatus = sytemObject.networkregistrationstatus;
+
    // Variable indicating network registration status
+
    var statusText = "";
+
   
+
    // Check network registration status value
+
    // See JavaScript WRT SystemInfo Service API documentation
+
    // for meaning of used constants
+
     if (networkRegistrationStatus != undefined) {
+
         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;
+
        }
+
    } else {
+
        statusText = "Undefined";  
+
 
     }
 
     }
  
     // Configure view of table which is used for bar showing.
+
     var signalStrength = result.ReturnValue.Status;
     var levelTable = document.getElementById('strengthTable');
+
     document.getElementById("signalStrength").innerHTML =
    // Get width of application window
+
        signalStrength + " dB";
    var windowWidth = screen.availWidth;
+
}
    // Get height of application window
+
 
    var windowHeight = screen.availHeight;
+
function displayNetworkName(transId, eventCode, result) {
    // Width of element which progress bar consists of
+
     // On error situation, display the error message
    var barElementWidth;
+
     if (eventCode == 4) {
    // Height of element which progress bar consists of
+
         alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
    var barElementHeight;
+
         return;
   
+
    // Progress bar consists of 7 columns:
+
    // one column for one signal level
+
    var progressbarColumnsCount = 7;
+
   
+
    // Row count in table which forms the progress bar
+
    var tableRowCount = 7;   
+
   
+
    // Width of progress bar = windowWidth / 2
+
    barElementWidth =
+
        Math.round( ( windowWidth / 2 ) / progressbarColumnsCount );
+
   
+
     // Height of progress bar =
+
    // min( windowHeight / 2, barElementWidth*tan(30) )
+
     // tan(30) = 0.578
+
    var tan30 = 0.6;
+
    if (windowHeight > (barElementWidth * tan30 * 2 * tableRowCount)) {
+
         barElementHeight = Math.round(barElementWidth * tan30);
+
    } else {
+
         barElementHeight =
+
            Math.round(( windowHeight / 2 ) / tableRowCount);
+
 
     }
 
     }
  
    // Variable rowString is used for forming table row HTML code
+
     var networkName = result.ReturnValue.NetworkName;
     var rowString = "";
+
     document.getElementById("networkName").innerHTML = networkName;
     // Auxiliary variable: table row number from end of table
+
}
    var counter = 1;
+
    // Fill table containing progress bar
+
    for (var j = level; j > 0; j--) {
+
        rowString = ""
+
        for (var i = 1; i < counter; i++) {
+
            rowString = rowString + "<td class=\"white\" width=\"" +
+
                barElementWidth + "px\" height=\"" +
+
                barElementHeight + "px\"></td>";   
+
        }
+
           
+
        for (var i = counter; i <= level; i++) {
+
            rowString = rowString + "<td class=\"green\" width=\"" +
+
            barElementWidth + "px\" height=\"" +
+
            barElementHeight + "px\" bgcolor=\"green\"></td>";
+
        }
+
  
        for (var i = level+1; i <= tableRowCount; i++) {
+
function displayRegistrationStatus(result) {
            rowString = rowString + "<td class=\"gray\" width=\"" +
+
     var networkRegistrationStatus = result.ReturnValue.Status;
                barElementWidth + "px\" height=\"" +
+
     document.getElementById("networkState").innerHTML =
                barElementHeight + "px\"></td>";
+
         networkRegistrationStatus;
        }
+
        levelTable.rows[tableRowCount - counter].innerHTML = rowString;
+
        counter++;
+
     }
+
   
+
    // Fill table rows which are not used for displaying progress bar.
+
    for (var line = 0; line < tableRowCount - level; line++) {
+
        rowString = "";
+
        for (var column = 0; column < tableRowCount - line - 1; column++) {
+
            rowString = rowString + "<td class=\"white\" width=\"" +
+
                barElementWidth + "px\" height=\"" +
+
                barElementHeight + "px\"></td>";
+
        }
+
       
+
        for(var column = tableRowCount - line - 1; column < tableRowCount;
+
                column++) {
+
            rowString = rowString + "<td class=\"gray\" width=\"" +
+
                barElementWidth + "px\" height=\"" +
+
                barElementHeight + "px\"></td>";
+
        }
+
        levelTable.rows[line].innerHTML = rowString;
+
     }
+
   
+
    // TD which contains network name indication
+
    var networkNameLabel = document.getElementById('networkName');
+
    // Update network name is indicated
+
    networkNameLabel.innerHTML = networkName;
+
    // TD which contains network state indication
+
    var stateLabel = document.getElementById('networkState');
+
    // Update network state indication
+
    stateLabel.innerHTML = statusText;
+
    // Set position of progress bar to center of table cell
+
    var progressBarContainer =  
+
         document.getElementById('progressbarContainer');
+
    progressBarContainer.align = "center";
+
    progressBarContainer.valign = "middle";
+
    progressBarContainer.Style.textAlign = "center";
+
 
}
 
}
 
</code>
 
</code>
  
 
==Postconditions==
 
==Postconditions==
* After starting the snippet, the information about network state exists on display:
 
  SIGNAL STRENGTH: Progress bar indicating a signal strength.<br>
 
  NAME: Network name of the current network.<br>
 
  STATE: Registration status of the device.<br>
 
  
* Shown information is refreshed every 0,5 seconds.
+
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]].
 
  
[[Category:Web Runtime (WRT)]][[Category:Code Examples]][[Category:S60 5th Edition]]
+
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]].

Revision as of 09:50, 5 October 2012

This code example shows how to obtain information (network name, signal strength, and network registration status) about the currently available network on Symbian Web Runtime.

Article Metadata
Code ExampleTested with
Devices(s): Nokia 5800 XpressMusic
CompatibilityArticle
Keywords: x-systeminfo-widget, SystemInfo.signalbars, SystemInfo.networkname, SystemInfo.networkregistrationstatus, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo()
Created: dekuykin (10 Dec 2008)
Last edited: hamishwillee (05 Oct 2012)

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. This snippet demonstrates both versions.

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 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.

107 page views in the last 30 days.