×
Namespaces

Variants
Actions
(Difference between revisions)

Checking network state in Symbian Web Runtime

From Nokia Developer Wiki
Jump to: navigation, search
seppo_fn (Talk | contribs)
tapla (Talk | contribs)
Line 9: Line 9:
 
|subcategory=System information
 
|subcategory=System information
 
|creationdate=December 18, 2008
 
|creationdate=December 18, 2008
|keywords=SystemInfo.networkname, SystemInfo.networkregistrationstatus, SystemInfo.signalbars
+
|keywords=x-systeminfo-widget, SystemInfo.signalbars, SystemInfo.networkname, SystemInfo.networkregistrationstatus, device.getServiceObject(), Service.SysInfo, Service.SysInfo.GetInfo()
 
}}
 
}}
 
  
 
==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:
+
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.
- 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:
 
+
==Source file: NetworkState.html==
+
  
 
<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() {
 
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 name
 
    var networkName = sytemObject.networkname;
 
  
    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, 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]].
 
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


Template:KBCS

Article Metadata
Tested 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: (18 Dec 2008)
Last edited: tapla (16 Jan 2009)

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.

87 page views in the last 30 days.