Skip to content

Commit

Permalink
Included base repo changes for android and added PR:tripflex#134 upda…
Browse files Browse the repository at this point in the history
…tes for ios
  • Loading branch information
RaghavBiz4 committed Apr 19, 2023
1 parent 4407a8c commit 117298f
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 19 deletions.
30 changes: 24 additions & 6 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,26 +465,43 @@ private boolean add(CallbackContext callbackContext, JSONArray data) {
}

if(API_VERSION >= 29) {
networkCallback = new ConnectivityManager.NetworkCallback() {
this.networkCallback = new ConnectivityManager.NetworkCallback() {
@Override
public void onAvailable(Network network) {
connectivityManager.setProcessDefaultNetwork(network);
connectivityManager.bindProcessToNetwork(network);
Log.d(TAG, "WiFi connected");
callbackContext.success("onAvailable");
}

@Override
public void onUnavailable() {
super.onUnavailable();
Log.d(TAG, "WiFi not available");
callbackContext.error("onUnavailable");
}
};

WifiNetworkSpecifier.Builder builder = new WifiNetworkSpecifier.Builder();
builder.setSsid(newSSID);
if (newSSID.endsWith("#")) {
newSSID = newSSID.replace("#", "");
builder.setSsidPattern(new PatternMatcher(newSSID, PatternMatcher.PATTERN_PREFIX));
}else {
builder.setSsid(newSSID);
}
builder.setWpa2Passphrase(newPass);

WifiNetworkSpecifier wifiNetworkSpecifier = builder.build();

NetworkRequest.Builder networkRequestBuilder1 = new NetworkRequest.Builder();
networkRequestBuilder1.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
//removeCapability added for hotspots without internet
networkRequestBuilder1.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET);
networkRequestBuilder1.setNetworkSpecifier(wifiNetworkSpecifier);

NetworkRequest nr = networkRequestBuilder1.build();
ConnectivityManager cm = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.requestNetwork(nr, this.networkCallback);
//timeout add because "No devices found" wasn't handled correct and doesn't throw Unavailable
cm.requestNetwork(nr, this.networkCallback, 60000);
} else {
// After processing authentication types, add or update network
if(wifi.networkId == -1) { // -1 means SSID configuration does not exist yet
Expand Down Expand Up @@ -882,6 +899,7 @@ private boolean disconnectNetwork(CallbackContext callbackContext, JSONArray dat
try{
ConnectivityManager cm = (ConnectivityManager) cordova.getActivity().getApplicationContext().getSystemService(Context.CONNECTIVITY_SERVICE);
cm.unregisterNetworkCallback(this.networkCallback);
connectivityManager.bindProcessToNetwork(null);
return true;
}
catch(Exception e) {
Expand Down Expand Up @@ -1831,7 +1849,7 @@ private void onSuccessfulConnection() {
// Marshmallow (API 23+) or newer uses bindProcessToNetwork
final NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
// .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build();

networkCallback = new ConnectivityManager.NetworkCallback() {
Expand All @@ -1855,7 +1873,7 @@ public void onAvailable(Network network) {
// Lollipop (API 21-22) use setProcessDefaultNetwork (deprecated in API 23 - Marshmallow)
final NetworkRequest request = new NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
// .removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.removeCapability(NetworkCapabilities.NET_CAPABILITY_INTERNET)
.build();

networkCallback = new ConnectivityManager.NetworkCallback() {
Expand Down
58 changes: 48 additions & 10 deletions src/ios/WifiWizard2.m
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#import <net/if.h>
#import <SystemConfiguration/CaptiveNetwork.h>
#import <NetworkExtension/NetworkExtension.h>
#import <NetworkExtension/NEHotspotNetwork.h>

@implementation WifiWizard2

Expand Down Expand Up @@ -194,20 +195,57 @@ - (void)iOSDisconnectNetwork:(CDVInvokedUrlCommand*)command {
callbackId:command.callbackId];
}

- (void)getConnectedSSID:(CDVInvokedUrlCommand*)command {
CDVPluginResult *pluginResult = nil;
NSDictionary *r = [self fetchSSIDInfo];
// - (void)getConnectedSSID:(CDVInvokedUrlCommand*)command {
// CDVPluginResult *pluginResult = nil;
// NSDictionary *r = [self fetchSSIDInfo];

NSString *ssid = [r objectForKey:(id)kCNNetworkInfoKeySSID]; //@"SSID"
// NSString *ssid = [r objectForKey:(id)kCNNetworkInfoKeySSID]; //@"SSID"

if (ssid && [ssid length]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssid];
// if (ssid && [ssid length]) {
// pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssid];
// } else {
// pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
// }

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

- (void)getConnectedSSID:(CDVInvokedUrlCommand*)command {
__block NSString *ssid = nil;
__block CDVPluginResult *pluginResult = nil;
if (@available(iOS 14.0, *)) {
[NEHotspotNetwork fetchCurrentWithCompletionHandler:^(NEHotspotNetwork * _Nullable currentNetwork) {
ssid = [currentNetwork SSID];
NSLog(@"debugjeje");

NSLog(@"%@", currentNetwork);
if (ssid && [ssid length]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssid];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
}
[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
NSArray *ifs = (__bridge_transfer NSArray *)CNCopySupportedInterfaces();
NSDictionary *info;
for (NSString *ifnam in ifs) {
info = (__bridge_transfer NSDictionary *)CNCopyCurrentNetworkInfo((__bridge CFStringRef)ifnam);
if (info && [info count]) {
ssid = [info objectForKey:@"SSID"];
break;
}
}
if (ssid && [ssid length]) {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:ssid];
} else {
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:@"Not available"];
}
[self.commandDelegate sendPluginResult:pluginResult
callbackId:command.callbackId];
}

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

- (void)getConnectedBSSID:(CDVInvokedUrlCommand*)command {
Expand Down
11 changes: 8 additions & 3 deletions www/WifiWizard2.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,8 +161,11 @@ var WifiWizard2 = {
WifiWizard2.add(wifiConfig).then(function (newNetID) {

// Successfully updated or added wifiConfig
cordova.exec(resolve, reject, "WifiWizard2", "connect", [WifiWizard2.formatWifiString(SSID), bindAll]);

if(device.platform === "Android" && !(parseInt(device.version.split('.')[0]) >= 10)) {
cordova.exec(resolve, reject, "WifiWizard2", "connect", [WifiWizard2.formatWifiString(SSID), bindAll]);
} else {
resolve(newNetID);
}
// Catch error adding/updating network
}).catch(function (error) {

Expand All @@ -172,7 +175,9 @@ var WifiWizard2 = {

// This error above should only be returned when the add method was able to pull a network ID (as it tries to update instead of adding)
// Lets go ahead and attempt to connect to that SSID (using the existing wifi configuration)
cordova.exec(resolve, reject, "WifiWizard2", "connect", [WifiWizard2.formatWifiString(SSID), bindAll]);
if(device.platform === "Android" && !(parseInt(device.version.split('.')[0]) >= 10)) {
cordova.exec(resolve, reject, "WifiWizard2", "connect", [WifiWizard2.formatWifiString(SSID), bindAll]);
}

} else {

Expand Down

0 comments on commit 117298f

Please sign in to comment.