×
Namespaces

Variants
Actions
(Difference between revisions)

Checking network state in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
dekuykin (Talk | contribs)
hamishwillee (Talk | contribs)
m (Hamishwillee - Add Abstract. Tidy wiki text)
(22 intermediate revisions by 7 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.}}
{{CodeSnippet
+
 
|id= 
+
{{ArticleMetaData <!-- v1.2 -->
|platform=S60 5th Edition
+
|sourcecode= [[Media:Checking network state in WRT.zip]]
|devices=Nokia 5800 XpressMusic
+
|installfile= <!-- Link to installation file (e.g. [[Media:The Installation File.sis]]) -->
|category=Web Runtime (WRT)
+
|devices= Nokia 5800 XpressMusic
|subcategory=S60 Platform SystemInfo Services
+
|sdk= <!-- SDK(s) built and tested against (e.g. [http://linktosdkdownload/ Nokia Qt SDK 1.1]) -->
|creationdate=November 24, 2008
+
|platform= S60 5th Edition
|keywords=SystemInfo.networkname, SystemInfo.networkregistrationstatus, SystemInfo.signalbars
+
|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 network which is currently available for using by device.
 
This information includes:
 
- network name;
 
- signal strength;
 
- network registration status.
 
The code snippet  uses <tt>JavaScript WRT SystemInfo Service API</tt>.
 
To get required informations these properties are used:
 
<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 System Info API embedded object <tt>application/x-systeminfo-widget</tt> is used.
+
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|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 file: NetworkState.html==
+
==Source (WRT 1.0)==
  
 +
Embed the SystemInfo widget into the document:
 
<code xml>
 
<code xml>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"  
+
<body>
    "http://www.w3.org/TR/html4/strict.dtd">
+
    <embed type="application/x-systeminfo-widget" hidden="yes" />
 +
</body>
 +
</code>
  
<html>
+
Add also the components into which the information is printed:
  <head>
+
    <title>
+
    Network State
+
    </title>
+
  
<script type="text/javascript" src="networkState.js">
+
<code xml>
</script>
+
<table>
 
+
     <tr><td>Signal strength:</td><td id="signalStrength"></td></tr>
     <link href="networkState.css" rel="stylesheet" type="text/css">
+
    <tr><td>Network name:</td><td id="networkName"></td></tr>
 
+
    <tr><td>Network state:</td><td id="networkState"></td></tr>
  </head>
+
</table>
 
+
  <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;
  
// Global variable capturing the reference to System Info object.
+
window.onload = init;
var sytemObject;
+
 
/**
+
function init() {
* Function is called during loading of snippet.
+
    // Obtain the SystemInfo service object
* 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() {
 +
    // Initialize the criteria for the service object and obtain the
 +
    // information
 +
    var criteria = new Object();
 +
    criteria.Entity = "Network";
  
function updateNetworkData() {
+
     // Get signal strength
     // Get signal level
+
     criteria.Key = "SignalStrength";
     var level = 0;
+
     try {
     level = sytemObject.signalbars;
+
        var result = serviceObj.ISysInfo.GetInfo(criteria,
   
+
            displaySignalStrength);
    if (level == undefined) {
+
     } catch (ex) {
    level = 0;  
+
        alert(ex);
     }
+
        return;
    // Check if level incorrect
+
    if (level < 0) {
+
    level = 0;   
+
    }
+
    if (level > 7) {
+
    level = 7;  
+
 
     }
 
     }
 +
 
     // Get network name
 
     // Get network name
     var networkName = sytemObject.networkname;
+
     criteria.Key = "CurrentNetwork";
 +
    try {
 +
        var result = serviceObj.ISysInfo.GetInfo(criteria,
 +
            displayNetworkName);
 +
    } catch (ex) {
 +
        alert(ex);
 +
        return;
 +
    }
  
    if (networkName == undefined) {
+
     // Get network registration status of the device
        networkName = "Unknown";
+
     criteria.Key = "RegistrationStatus";
    }
+
     try {
 
+
         var result = serviceObj.ISysInfo.GetInfo(criteria);
     // Get network registartion status of the device
+
         displayRegistrationStatus(result);
     var networkRegistrationStatus = sytemObject.networkregistrationstatus;
+
    } catch (ex) {
    // Variable indicating network registration status
+
         alert(ex);
    var statusText = "";
+
         return;
      
+
    // 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.
+
function displaySignalStrength(transId, eventCode, result) {
    var levelTable = document.getElementById('strengthTable');
+
     // On error situation, display the error message
     // Get width of application window
+
     if (eventCode == 4) {
    var windowWidth = screen.availWidth;
+
         alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
    // Get height of application window
+
         return;
    var windowHeight = screen.availHeight;
+
    // Width of element which progress bar consists of
+
    var barElementWidth;
+
    // Height of element which progress bar consists of
+
    var barElementHeight;
+
   
+
    // 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 signalStrength = result.ReturnValue.Status;
     var rowString = "";
+
     document.getElementById("signalStrength").innerHTML =
     // Auxiliary variable: table row number from end of table
+
         signalStrength + " dB";
    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 displayNetworkName(transId, eventCode, result) {
            rowString = rowString + "<td class=\"gray\" width=\"" +
+
    // On error situation, display the error message
                barElementWidth + "px\" height=\"" +
+
    if (eventCode == 4) {
                barElementHeight + "px\"></td>";
+
        alert("Error " + result.ErrorCode + ": " + result.ErrorMessage);
        }
+
         return;
        levelTable.rows[tableRowCount - counter].innerHTML = rowString;
+
         counter++;
+
 
     }
 
     }
   
+
 
     // Fill table rows which are not used for displaying progress bar.
+
     var networkName = result.ReturnValue.NetworkName;
    for (var line = 0; line < tableRowCount - level; line++) {
+
     document.getElementById("networkName").innerHTML = networkName;
        rowString = "";
+
}
        for (var column = 0; column < tableRowCount - line - 1; column++) {
+
 
            rowString = rowString + "<td class=\"white\" width=\"" +
+
function displayRegistrationStatus(result) {
                barElementWidth + "px\" height=\"" +
+
     var networkRegistrationStatus = result.ReturnValue.Status;
                barElementHeight + "px\"></td>";
+
     document.getElementById("networkState").innerHTML =
        }
+
        networkRegistrationStatus;
       
+
        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 information about network state exists on display:
 
  
SIGNAL STRENGTH: progress bar indicating a signal strength.<br>
+
Information about the current network is obtained.  
NAME: network name of current network.<br>
+
STATE: registration status of the device.<br>
+
  
Shown information is refreshed every 0,5 seconds.
 
 
==Supplementary material==
 
==Supplementary material==
  
You can see source file and executable application in attached zip archive. 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]]
+

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.

134 page views in the last 30 days.
×