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 connected and unavailable callback for Api >= 29 #115

Merged
merged 3 commits into from
Jan 15, 2021
Merged
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
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
"name": "cordova-plugin-wifiwizard2",
"version": "3.1.1",
"cordova": {
"id": "wifiwizard2",
"id": "cordova-plugin-wifiwizard2",
"platforms": [
"android",
"ios"
]
},
"repository": {
"type": "git",
"url": "git+https://github.com/tripflex/wifiwizard2.git"
"url": "git+https://github.com/maxcodefaster/WifiWizard2.git"
},
"author": "Myles McNamara",
"license": "Apache 2.0",
Expand Down
4 changes: 2 additions & 2 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

<plugin xmlns="http://apache.org/cordova/ns/plugins/1.0"
xmlns:android="http://schemas.android.com/apk/res/android"
id="wifiwizard2"
id="cordova-plugin-wifiwizard2"
version="3.1.1">

<name>WifiWizard2</name>
<description>Cordova Wifi Manager for Android and iOS</description>
<license>Apache 2.0</license>
<keywords>cordova,network,wifi,phonegap</keywords>
<repo>https://github.com/tripflex/wifiwizard2.git</repo>
<repo>https://github.com/maxcodefaster/WifiWizard2.git</repo>
<issue>https://github.com/tripflex/wifiwizard2/issues</issue>

<js-module src="www/WifiWizard2.js" name="WifiWizard2">
Expand Down
22 changes: 17 additions & 5 deletions src/android/wifiwizard2/WifiWizard2.java
Original file line number Diff line number Diff line change
Expand Up @@ -465,10 +465,18 @@ 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("WiFi connected");
}
@Override
public void onUnavailable() {
super.onUnavailable();
Log.d(TAG, "WiFi not available");
callbackContext.error("WiFi not available");
}
};

Expand All @@ -480,11 +488,14 @@ public void onAvailable(Network network) {

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, 15000);
} 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 +893,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 +1843,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 +1867,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
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