Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scan function that will return networks #55

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 79 additions & 1 deletion src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,23 @@

import org.apache.cordova.*;
import java.util.List;
import java.util.concurrent.Future;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiEnterpriseConfig;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.SupplicantState;
import android.content.Context;
import android.util.Log;


Expand All @@ -42,6 +46,7 @@ public class WifiWizard extends CordovaPlugin {
private static final String LIST_NETWORKS = "listNetworks";
private static final String START_SCAN = "startScan";
private static final String GET_SCAN_RESULTS = "getScanResults";
private static final String SCAN = "scan";
private static final String GET_CONNECTED_SSID = "getConnectedSSID";
private static final String IS_WIFI_ENABLED = "isWifiEnabled";
private static final String SET_WIFI_ENABLED = "setWifiEnabled";
Expand Down Expand Up @@ -93,6 +98,9 @@ else if(action.equals(START_SCAN)) {
else if(action.equals(GET_SCAN_RESULTS)) {
return this.getScanResults(callbackContext, data);
}
else if(action.equals(SCAN)) {
return this.scan(callbackContext, data);
}
else if(action.equals(DISCONNECT)) {
return this.disconnect(callbackContext);
}
Expand Down Expand Up @@ -459,6 +467,76 @@ private boolean startScan(CallbackContext callbackContext) {
}
}

private class ScanSyncContext {
public boolean finished = false;
}

/**
* Scans networks and sends the list back on the success callback
* @param callbackContext A Cordova callback context
* @param data JSONArray with [0] == JSONObject
* @return true
*/
private boolean scan(final CallbackContext callbackContext, final JSONArray data) {
Log.v(TAG, "Entering startScan");
final ScanSyncContext syncContext = new ScanSyncContext();

final BroadcastReceiver receiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Entering onReceive");
synchronized (syncContext) {
if (syncContext.finished) {
Log.v(TAG, "In onReceive, already finished");
return;
}
syncContext.finished = true;
context.unregisterReceiver(this);
}
Log.v(TAG, "In onReceive, success");
getScanResults(callbackContext, data);
}
};

final Context context = cordova.getActivity().getApplicationContext();

Log.v(TAG, "Submitting timeout to threadpool");
cordova.getThreadPool().submit(new Runnable() {
public void run() {
Log.v(TAG, "Entering timeout");
final int TEN_SECONDS = 10000;
try {
Thread.sleep(TEN_SECONDS);
} catch (InterruptedException e) {
Log.e(TAG, "Received InterruptedException e, " + e);
// keep going into error
}
Log.v(TAG, "Thread sleep done");
synchronized (syncContext) {
if (syncContext.finished) {
Log.v(TAG, "In timeout, already finished");
return;
}
syncContext.finished = true;
context.unregisterReceiver(receiver);
}
Log.v(TAG, "In timeout, error");
callbackContext.error("Timed out waiting for scan to complete");
}
});

Log.v(TAG, "Registering broadcastReceiver");
context.registerReceiver(
receiver,
new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)
);

if (!wifiManager.startScan()) {
callbackContext.error("Scan failed");
}
Log.v(TAG, "Starting wifi scan");
return true;
}

/**
* This method retrieves the SSID for the currently connected network
*
Expand Down
1 change: 1 addition & 0 deletions src/ios/NXWWifiWizard.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- (void)listNetworks:(CDVInvokedUrlCommand*)command;
- (void)getScanResults:(CDVInvokedUrlCommand*)command;
- (void)startScan:(CDVInvokedUrlCommand*)command;
- (void)scan:(CDVInvokedUrlCommand*)command;
- (void)disconnect:(CDVInvokedUrlCommand*)command;
- (void)getConnectedSSID:(CDVInvokedUrlCommand*)command;
- (void)getConnectedBSSID:(CDVInvokedUrlCommand*)command;
Expand Down
9 changes: 9 additions & 0 deletions src/ios/NXWWifiWizard.m
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,15 @@ - (void)startScan:(CDVInvokedUrlCommand*)command {
callbackId:command.callbackId];
}

- (void)scan:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not supported"];

[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

- (void)disconnect:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;

Expand Down
20 changes: 20 additions & 0 deletions www/WifiWizard.js
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,26 @@ var WifiWizard = {
cordova.exec(win, fail, 'WifiWizard', 'startScan', []);
},

/**
* Scan WiFi networks and return results
* @param win callback function
* @param fail callback function if error
*/
scan: function(options, win, fail) {
if (typeof options === 'function') {
fail = win;
win = options;
options = {};
}

if (typeof win != 'function' ) {
console.log("scan first parameters must be a function to handle list.");
return;
}

cordova.exec(win, fail, 'WifiWizard', 'scan', [options]);
},

/**
* Disconnect current wifi.
* @param win callback function
Expand Down