Please note, this is a STATIC archive of website developer.mozilla.org from 03 Nov 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Monitoring WiFi access points

Code with UniversalXPConnect privileges can monitor the list of available WiFi access points to obtain information about them including their SSID, MAC address, and signal strength. This capability was introduced primarily to allow WiFi-based location services to be used by geolocation services.

Example

This example simply displays a list of the available access points.

<html>
<head>
<title>WiFi Monitor Example</title>
<script>

var count = 0;

function test() {
}

test.prototype =
{
  onChange: function (accessPoints)
  {
    netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
    var d = document.getElementById("d");
    d.innerHTML = "";

    for (var i=0; i<accessPoints.length; i++) {
      var a = accessPoints[i];
      d.innerHTML += "<p>" + a.mac + " " + a.ssid + " " + a.signal + "</p>";
    }

    var c = document.getElementById("c");
    c.innerHTML = "<p>" + count++ + "</p>";

  },

  onError: function (value) {
     alert("error: " +value);
  },

  QueryInterface: function(iid) {
        netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        if (iid.equals(Components.interfaces.nsIWifiListener) ||
            iid.equals(Components.interfaces.nsISupports))
            return this;
        throw Components.results.NS_ERROR_NO_INTERFACE;
   },
}

  netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');

  var listener = new test();
  var wifi_service = Components.classes["@mozilla.org/wifi/monitor;1"].getService(Components.interfaces.nsIWifiMonitor);

  wifi_service.startWatching(listener);

</script>
</head>

<body>
<div id="d"><p></p></div>
<div id="c"><p></p></div>
</body>
</html>

The nsIWifiListener object

The first thing the code above does is to prototype the listener object that will be receiving notifications of changes to the access point list. This object is described in lines 11 through 40 in the code above.

The onChange() method (lines 13 through 27) begins by enabling UniversalXPConnect privileges, then clearing out the div (d) that will receive the updated list of access points. Then the loop in lines 19-22 iterates over the list of access points received in the accessPoints array and adds them to the HTML in the div.

Then the c div is updated with the number of times the list has been refreshed.

The onError() method simply opens an alert that displays the error code received.

Starting to monitor access points

To start monitoring the access points, we instantiate the listener (on line 44), then get access to the WiFi monitor interface on line 45. The monitoring is started up on line 47, by calling the WiFi monitoring service's startWatching() method.

See also

Document Tags and Contributors

Tags: 
 Contributors to this page: trevorh, Sephr, Sheppy
 Last updated by: trevorh,