Skip to content

Commit

Permalink
Add support for API level 29
Browse files Browse the repository at this point in the history
  • Loading branch information
Viggo Chavine authored and ViggoChavine committed Apr 3, 2020
1 parent 0ec67cd commit d803f8f
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 36 deletions.
103 changes: 72 additions & 31 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import org.apache.cordova.*;

import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.Future;
import java.lang.InterruptedException;

import org.json.JSONArray;
Expand All @@ -41,12 +41,16 @@
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiNetworkSpecifier;
import android.net.ConnectivityManager;
import android.net.ConnectivityManager.NetworkCallback;
import android.net.NetworkSpecifier;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.os.Build.VERSION;
import android.os.PatternMatcher;

import java.net.URL;
import java.net.InetAddress;
Expand Down Expand Up @@ -460,26 +464,49 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
wifi.priority = getMaxWifiPriority(wifiManager) + 1;
}

// After processing authentication types, add or update network
if (wifi.networkId == -1) { // -1 means SSID configuration does not exist yet
if( API_VERSION >= 29 ){
networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
connectivityManager.setProcessDefaultNetwork(network);
}
};

int newNetId = wifiManager.addNetwork(wifi);
if( newNetId > -1 ){
callbackContext.success( newNetId );
} else {
callbackContext.error( "ERROR_ADDING_NETWORK" );
}
WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(newSSID);
builder.setWpa2Passphrase(newPass);

WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();

NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);

NetworkRequest nr = networkRequestBuilder1.build();
ConnectivityManager cm = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(nr, this.networkCallback);
} else {
// After processing authentication types, add or update network
if (wifi.networkId == -1) { // -1 means SSID configuration does not exist yet

int updatedNetID = wifiManager.updateNetwork(wifi);
int newNetId = wifiManager.addNetwork(wifi);
if( newNetId > -1 ){
callbackContext.success( newNetId );
} else {
callbackContext.error( "ERROR_ADDING_NETWORK" );
}

if( updatedNetID > -1 ){
callbackContext.success( updatedNetID );
} else {
callbackContext.error( "ERROR_UPDATING_NETWORK" );
}

int updatedNetID = wifiManager.updateNetwork(wifi);

if( updatedNetID > -1 ){
callbackContext.success( updatedNetID );
} else {
callbackContext.error( "ERROR_UPDATING_NETWORK" );
}

}
}

// WifiManager configurations are presistent for API 26+
Expand Down Expand Up @@ -821,35 +848,47 @@ private boolean disconnectNetwork(CallbackContext callbackContext, JSONArray dat
return false;
}

int networkIdToDisconnect = ssidToNetworkId(ssidToDisconnect);
if( API_VERSION < 29){
int networkIdToDisconnect = ssidToNetworkId(ssidToDisconnect);

if (networkIdToDisconnect > 0) {
if (networkIdToDisconnect > 0) {

if( wifiManager.disableNetwork(networkIdToDisconnect) ){
if( wifiManager.disableNetwork(networkIdToDisconnect) ){

maybeResetBindALL();
maybeResetBindALL();

// We also remove the configuration from the device (use "disable" to keep config)
if( wifiManager.removeNetwork(networkIdToDisconnect) ){
callbackContext.success("Network " + ssidToDisconnect + " disconnected and removed!");
} else {
callbackContext.error("DISCONNECT_NET_REMOVE_ERROR");
Log.d(TAG, "WifiWizard2: Unable to remove network!");
return false;
}

// We also remove the configuration from the device (use "disable" to keep config)
if( wifiManager.removeNetwork(networkIdToDisconnect) ){
callbackContext.success("Network " + ssidToDisconnect + " disconnected and removed!");
} else {
callbackContext.error("DISCONNECT_NET_REMOVE_ERROR");
Log.d(TAG, "WifiWizard2: Unable to remove network!");
callbackContext.error("DISCONNECT_NET_DISABLE_ERROR");
Log.d(TAG, "WifiWizard2: Unable to disable network!");
return false;
}

} else {
callbackContext.error("DISCONNECT_NET_DISABLE_ERROR");
Log.d(TAG, "WifiWizard2: Unable to disable network!");
return false;
}

return true;
return true;
} else {
callbackContext.error("DISCONNECT_NET_ID_NOT_FOUND");
Log.d(TAG, "WifiWizard2: Network not found to disconnect.");
return false;
}
} else {
try{
ConnectivityManager cm = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(this.networkCallback);
return true;
}
catch(Exception e) {
callbackContext.error(e.getMessage());
return false;
}
}
}

/**
Expand Down Expand Up @@ -1209,7 +1248,7 @@ private boolean isWifiEnabled(CallbackContext callbackContext) {
private int ssidToNetworkId(String ssid) {

try {

int maybeNetId = Integer.parseInt(ssid);
Log.d(TAG, "ssidToNetworkId passed SSID is integer, probably a Network ID: " + ssid);
return maybeNetId;
Expand Down Expand Up @@ -1661,6 +1700,8 @@ public void onReceive(final Context context, final Intent intent) {
// Verify the desired network ID is what we actually connected to
if ( desired != null && info.getNetworkId() == desired.apId ) {
onSuccessfulConnection();
} else {
Log.e(TAG, "Could not connect to the desired ssid: " + ssid);
}

}
Expand Down
15 changes: 10 additions & 5 deletions www/WifiWizard2.js
Original file line number Diff line number Diff line change
Expand Up @@ -616,12 +616,17 @@ var WifiWizard2 = {
}
ssid = ssid.trim();

if (ssid.charAt(0) != '"') {
ssid = '"' + ssid;
}
if(device.platform === "Android" && parseInt(device.version.split('.')[0]) >= 10){
// Do not add "" To the SSID, as the new method for Android Q does not support it
}
else {
if (ssid.charAt(0) != '"') {
ssid = '"' + ssid;
}

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

return ssid;
Expand Down

0 comments on commit d803f8f

Please sign in to comment.