Monitoring WiFi access points

  • Revision slug: Monitoring_WiFi_access_points
  • Revision title: Monitoring WiFi access points
  • Revision id: 120457
  • Created:
  • Creator: Sephr
  • Is current revision? No
  • Comment no wording changes

Revision Content

{{ gecko_minversion_header("1.9.1") }}

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.

Note: A glitch of some kind in the wiki is causing line 21 to display incorrectly; the first "dd" should be simply "d", but the second "d" is getting added for some reason.
<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 = 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

  • {{ interface("nsIWifiMonitor") }}
  • {{ interface("nsIWifiListener") }}
  • {{ interface("nsIWifiAccessPoint") }}

Revision Source

<p>{{ gecko_minversion_header("1.9.1") }}</p>
<p>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 <a class="internal" href="/En/Using_geolocation" title="En/Using geolocation">geolocation</a> services.</p>
<h2>Example</h2>
<p>This example simply displays a list of the available access points.</p>
<div class="note"><strong>Note:</strong> A glitch of some kind in the wiki is causing line 21 to display incorrectly; the first "dd" should be simply "d", but the second "d" is getting added for some reason.</div>
<pre class="brush: html">&lt;html&gt;
&lt;head&gt;
&lt;title&gt;WiFi Monitor Example&lt;/title&gt;
&lt;script&gt;

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&lt;accessPoints.length; i++) {
      var a = accessPoints[i];
      d.innerHTML = d.innerHTML + "&lt;p&gt;" + a.mac + " " + a.ssid + " " + a.signal + "&lt;/p&gt;";
    }

    var c = document.getElementById("c");
    c.innerHTML = "&lt;p&gt;" + count++ + "&lt;/p&gt;";

  },

  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);

&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;div id="d"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;
&lt;div id="c"&gt;&lt;p&gt;&lt;/p&gt;&lt;/div&gt;
&lt;/body&gt;
&lt;/html&gt;
</pre>
<h3>The nsIWifiListener object</h3>
<p>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.</p>
<p>The <code>onChange()</code> method (lines 13 through 27) begins by enabling UniversalXPConnect privileges, then clearing out the <code>div</code> (<code>d</code>) 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 <code>div</code>.</p>
<p>Then the <code>c</code> <code>div</code> is updated with the number of times the list has been refreshed.</p>
<p>The <code>onError()</code> method simply opens an alert that displays the error code received.</p>
<h3>Starting to monitor access points</h3>
<p>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 <code>startWatching()</code> method.</p><h2>See also</h2>
<ul> <li>{{ interface("nsIWifiMonitor") }}</li> <li>{{ interface("nsIWifiListener") }}</li> <li>{{ interface("nsIWifiAccessPoint") }}</li>
</ul>
Revert to this revision