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

Create maven.yml #132

Open
wants to merge 7 commits 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
25 changes: 25 additions & 0 deletions .github/workflows/maven.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This workflow will build a Java project with Maven
# For more information see: https://help.github.com/actions/language-and-framework-guides/building-and-testing-java-with-maven

name: Java CI with Maven

on:
push:
branches: [ master ]
pull_request:
branches: [ master ]

jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'adopt'
- name: Build with Maven
run: mvn -B package --file pom.xml
138 changes: 138 additions & 0 deletions WifiWizard.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
/*
* This is the interface for the WifiWizard Phonegap plugin.
*/

var WifiWizard = {

/**
* This method formats wifi information into an object for use with the
* addNetwork function.
* @param SSID the SSID of the network enclosed in double quotes
* @param password the password for the network enclosed in double quotes
* @param algorithm the authentication algorithm
* @return wifiConfig a JSON object properly formatted for the plugin.
*/
formatWifiConfig: function(SSID, password, algorithm) {
console.log("WifiWizard configuration method entered.");
var wifiConfig = {
'SSID':formatSSID(SSID),
'Password':password,
'AuthAlg':algorithm
};
return wifiConfig;
},

/**
* This method formats a given SSID and ensures that it is appropriate.
* If the SSID is not wrapped in double quotes, it wraps it in double quotes.
* @param ssid the SSID to format
*/
formatSSID: function(ssid) {
ssid = ssid.trim()

if (ssid.charAt(0) != '"' ) {
ssid = '"' + ssid;
}

if (ssid.charAt(ssid.length-1) != '"' ) {
ssid = ssid + '"';
}

return ssid;
},

/**
* This methods adds a network to the list of available networks.
* Currently, only WPA authentication method is supported.
*
* @param wifi is JSON formatted information necessary for adding the Wifi
* network. Ex:
* wifi = {
* 'SSID': '\'MyNetwork\'',
* 'Password': '\'suchsecretpasswordwow\'',
* 'AuthAlg': 'WPA'
* }
* @param win is a callback function that gets called if the plugin is
* successful.
* @param fail is a callback function that gets called if the plugin gets
* an error
* @return `this` so you can chain calls together.
*/
addNetwork: function(wifi, win, fail) {
console.log("WifiWizard add method entered.");
if (wifi !== null && typeof wifi === 'object') {
// Ok to proceed!
}
else {
console.log('WifiWizard: Invalid parameter. wifi not an object.');
}

var networkInformation = [];

if (wifi.SSID !== undefined && wifi.SSID !== '') {
networkInformation.push(wifi.SSID);
}
else {
// i dunno, like, reject the call or something? what are you even doing?
console.log('WifiWizard: No SSID given.');
return false;
}

if (wifi.Password !== undefined) {
networkInformation.push(wifi.Password);
}
else {
// Assume no password for open networks.
networkInformation.push('');
console.log('WifiWizard: No password given.');
}

if (wifi.AuthAlg !== undefined && wifi.AuthAlg !== '') {
networkInformation.push(wifi.AuthAlg);
}
else {
console.log('WifiWizard: No authentication algorithm given.');
return false;
}

cordova.exec(win, fail, 'WifiWizard', 'addNetwork', networkInformation);

},

// Remove network
removeNetwork: function(SSID, win, fail) {
console.log("WifiWizard remove method entered.");
cordova.exec(win, fail, 'WifiWizard', 'removeNetwork', [SSID]);

},

// Connect to Network
connectNetwork: function(SSID, win, fail) {
console.log("WifiWizard connect method entered.");
cordova.exec(win, fail, 'WifiWizard', 'connectNetwork', [SSID]);
},

// Disconnect from network
disconnectNetwork: function(SSID, win, fail) {
console.log("WifiWizard disconnect method entered.");
cordova.exec(win, fail, 'WifiWizard', 'disconnectNetwork', [SSID]);

},

/**
* Hands the list of available networks to the `win` success callback function.
* @param win callback function that receives list of networks
* @param fail callback function if error
* @return a list of networks
*/
listNetworks: function(win, fail) {
console.log("WifiWizard list method entered.");
if (typeof win != "function") {
console.log("listNetworks first parameter must be a function to handle list.");
return;
}
cordova.exec(win, fail, 'WifiWizard', 'listNetworks', []);
}
};

// module.exports = WifiWizard;
71 changes: 8 additions & 63 deletions src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,11 @@
import org.json.JSONException;
import org.json.JSONObject;

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 Down Expand Up @@ -91,7 +89,7 @@ else if(action.equals(START_SCAN)) {
return this.startScan(callbackContext);
}
else if(action.equals(GET_SCAN_RESULTS)) {
return this.getScanResults(callbackContext, data);
return this.getScanResults(callbackContext);
}
else if(action.equals(DISCONNECT)) {
return this.disconnect(callbackContext);
Expand Down Expand Up @@ -270,15 +268,12 @@ private boolean connectNetwork(CallbackContext callbackContext, JSONArray data)
// a disconnect(), this will not reconnect.
wifiManager.disableNetwork(networkIdToConnect);
wifiManager.enableNetwork(networkIdToConnect, true);

SupplicantState supState;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
callbackContext.success(supState.toString());
callbackContext.success("Network " + ssidToConnect + " connected!");
return true;

}else{
callbackContext.error("WifiWizard: cannot connect to network");
}
else {
callbackContext.error("Network " + ssidToConnect + " not found!");
Log.d(TAG, "WifiWizard: Network not found to connect.");
return false;
}
}
Expand Down Expand Up @@ -370,71 +365,21 @@ private boolean listNetworks(CallbackContext callbackContext) {
* @param data JSONArray with [0] == JSONObject
* @return true
*/
private boolean getScanResults(CallbackContext callbackContext, JSONArray data) {
private boolean getScanResults(CallbackContext callbackContext) {
List<ScanResult> scanResults = wifiManager.getScanResults();

JSONArray returnList = new JSONArray();

Integer numLevels = null;

if(!validateData(data)) {
callbackContext.error("WifiWizard: disconnectNetwork invalid data");
Log.d(TAG, "WifiWizard: disconnectNetwork invalid data");
return false;
}else if (!data.isNull(0)) {
try {
JSONObject options = data.getJSONObject(0);

if (options.has("numLevels")) {
Integer levels = options.optInt("numLevels");

if (levels > 0) {
numLevels = levels;
} else if (options.optBoolean("numLevels", false)) {
// use previous default for {numLevels: true}
numLevels = 5;
}
}
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error(e.toString());
return false;
}
}

for (ScanResult scan : scanResults) {
/*
* @todo - breaking change, remove this notice when tidying new release and explain changes, e.g.:
* 0.y.z includes a breaking change to WifiWizard.getScanResults().
* Earlier versions set scans' level attributes to a number derived from wifiManager.calculateSignalLevel.
* This update returns scans' raw RSSI value as the level, per Android spec / APIs.
* If your application depends on the previous behaviour, we have added an options object that will modify behaviour:
* - if `(n == true || n < 2)`, `*.getScanResults({numLevels: n})` will return data as before, split in 5 levels;
* - if `(n > 1)`, `*.getScanResults({numLevels: n})` will calculate the signal level, split in n levels;
* - if `(n == false)`, `*.getScanResults({numLevels: n})` will use the raw signal level;
*/

int level;

if (numLevels == null) {
level = scan.level;
} else {
level = wifiManager.calculateSignalLevel(scan.level, numLevels);
}

JSONObject lvl = new JSONObject();
try {
lvl.put("level", level);
lvl.put("level", scan.level);
lvl.put("SSID", scan.SSID);
lvl.put("BSSID", scan.BSSID);
lvl.put("frequency", scan.frequency);
lvl.put("capabilities", scan.capabilities);
// lvl.put("timestamp", scan.timestamp);
returnList.put(lvl);
} catch (JSONException e) {
e.printStackTrace();
callbackContext.error(e.toString());
return false;
}
}

Expand Down