diff --git a/.github/workflows/maven.yml b/.github/workflows/maven.yml new file mode 100644 index 0000000..1531e21 --- /dev/null +++ b/.github/workflows/maven.yml @@ -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 diff --git a/WifiWizard.js b/WifiWizard.js new file mode 100644 index 0000000..1ac34ca --- /dev/null +++ b/WifiWizard.js @@ -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; diff --git a/src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java b/src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java index 2c4ac8f..b455c33 100644 --- a/src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java +++ b/src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java @@ -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; @@ -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); @@ -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; } } @@ -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 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; } }