Wifi Scanner with wifiwizard

Hi. Does anyone implemented a wifi scanner? I am trying to use WifiWizard plugin, I installed and it is working, but I am having trouble with the provider I use to get the networks: This is what I have, I use some of the code :

It is done for Ionic v1 but I can’t get the provider to return the array of the scanned networks…

import { Injectable } from ‘@angular/core’;
import ‘rxjs/add/operator/map’;
import { Platform } from ‘ionic-angular’;

declare let WifiWizard: any;
let items: WifiInfo[] = [];

export class WifiInfo {
ssid: string;

constructor(ssid: string) {
this.ssid = ssid;
}
}

@Injectable()
export class Service {

currentWifi: WifiInfo;
// users: any; // if it’s a class member
constructor(private platform: Platform) {
}

public init(): WifiInfo {
this.platform.ready().then(() => {
WifiWizard.startScan(successNetwork, failNetwork);
function successNetwork(e){
WifiWizard.getScanResults(listHandler, failNetwork);
// alert(“getScanResults:”);
}

  function listHandler(a){
  //  let network_array = [];
    var users: WifiInfo[] = []; // if it's a local variable
    //var users: WifiInfo; // if it's a local variable
    for(var i=0; i<a.length; i++){
      users.push(a[i].SSID);
      //users = new WifiInfo(a[i].SSID);
      //network_array.push(a[i].SSID);
      //this.users.push(a[i].SSID);
    }
  //  return users;

    alert("network_array:"+  users);

//return “this.users.ssid”;
/* this.unique_array = network_array.filter(function(elem, pos) {
return network_array.indexOf(elem) == pos;
});*/
//alert(“network_array:”+network_array);
}

/*  function listHandler(e){
    alert("listHandler: " + e);
  }*/
  function failNetwork(e){
    alert("Network Failure: " + e);
  }
});
return this.currentWifi;

}

public getWifiInformation() : WifiInfo {
return this.currentWifi;
}

}

Thanks.

Done. I managed to do it, new service created. But I can connect only to open wifi’s when you previously connected manually, I mean, you have to connect using the wifi phone access and then you can connect using the app. Does anyone had the same problem? Library bug?

I have tried making this work in an Ionic2 app…no luck :frowning:
Have followed your example, https://www.joshmorony.com/using-cordova-plugins-in-ionic-2-with-ionic-native/, https://dynamicremo.blogspot.dk/2016/01/ionic-wifi-app-using-phonegapcordova.html?showComment=1484847043482#c3071129093613651586, among others, but no luck.

Can you or anyone else publish a Ionic2 app with a working wifi scanner?

I am also stuck here. Can you please share the code. I am new to Ionic 2.

hey, i have a problem that i cant pass the results of “startScan” to a class variable. I can log the results though.

I do get the following error: ERROR: Error: Uncaught (in promise): TypeError: Cannot set property ‘networks’ of null.

Here is my code: `import { Component } from ‘@angular/core’;
import { NavController, NavParams, Platform } from ‘ionic-angular’;

declare let WifiWizard: any;

@Component({
selector: ‘page-hotspot’,
templateUrl: ‘hotspot.html’
})
export class HotSpotPage {

//networks: Array<{ssid: string, bssid: string, level: string}>;
public networks: any[] = [];
public scanResults: any[] = [];

constructor(public platform: Platform) {

}  

ionViewDidLoad() {
    this.platform.ready().then(() => {
        this.startScan();
    });
}

startScan() {
    if (typeof WifiWizard !== 'undefined') {
            console.log("WifiWizard loaded: ");
            console.log(WifiWizard);
        } else {
            console.warn('WifiWizard not loaded.');
        }

        WifiWizard.startScan(successNetwork, failNetwork);

        let networks: any [] = []; 

        function successNetwork(e) {
            WifiWizard.getScanResults(listHandler, failNetwork);
        }

        function failNetwork(e) {
            console.log("" + e);
        }

        function listHandler(a) { 

            let networks: any[] = []
            for (let x in a) {                    
                console.log(a[x].SSID + ", " + a[x].BSSID + ", " + a[x].level);  
                networks.push({
                    ssid: a[x].SSID,
                    bssid: a[x].BSSID,
                    level: a[x].level});
                console.log(networks[x].ssid + " " + networks[x].bssid); // Logging is fine!                   
            }       
            this.networks = networks; //---> ERROR: Error: Uncaught (in promise): TypeError: Cannot set property 'networks' of null

        }
}

}
`

Can somebody help?

Okay I solved it by replacing function with an arrow function (help from here: [Ionic: Uncaught (in promise): TypeError: Cannot read property of null] (Ionic: Uncaught (in promise): TypeError: Cannot read property of null))

So my startScan() function looks like this now:

startScan() {
        if (typeof WifiWizard !== 'undefined') {
                console.log("WifiWizard loaded: ");
                console.log(WifiWizard);
        } else {
            console.warn('WifiWizard not loaded.');
        }              

        let successNetwork = (e: any) => {
            WifiWizard.getScanResults(listHandler, failNetwork);
        }

        let failNetwork = (e: any) => {
            console.log("" + e);
        }

        let listHandler = (a: any) => {
            this.networks = [];
            for (let x in a) {                    
                console.log(a[x].SSID + ", " + a[x].BSSID + ", " + a[x].level);  
                this.networks.push({
                    ssid: a[x].SSID,
                    bssid: a[x].BSSID,
                    level: a[x].level});                
            }  
        }
        WifiWizard.startScan(successNetwork, failNetwork);
    }

To get the plugin working just add it via CLI:

cordova plugin add https://github.com/hoerresb/WifiWizard

and then declare it in the ts file where you want to use it (https://github.com/hoerresb/WifiWizard/issues/94):

declare let WifiWizard: any;

That’s all, package.json and config.xml don’t have any entries linked to the plugin.

Greetings

1 Like