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

Call success callback after checking connection #46

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
38 changes: 36 additions & 2 deletions src/android/src/com/pylonproducts/wifiwizard/WifiWizard.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,15 @@
package com.pylonproducts.wifiwizard;

import org.apache.cordova.*;

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

import org.json.JSONArray;
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;
Expand Down Expand Up @@ -268,8 +271,39 @@ private boolean connectNetwork(CallbackContext callbackContext, JSONArray data)
// a disconnect(), this will not reconnect.
wifiManager.disableNetwork(networkIdToConnect);
wifiManager.enableNetwork(networkIdToConnect, true);
callbackContext.success("Network " + ssidToConnect + " connected!");
return true;

final int TIMES_TO_RETRY = 30;
for(int i = 0; i < TIMES_TO_RETRY; i++) {
WifiInfo info = wifiManager.getConnectionInfo();
NetworkInfo.DetailedState connectionState = info.getDetailedStateOf(info.getSupplicantState());

boolean isConnected =
// need to ensure we're on correct network because sometimes this code is
// reached before the initial network has disconnected
info.getNetworkId() == networkIdToConnect && (
connectionState == NetworkInfo.DetailedState.CONNECTED ||
// Android seems to sometimes get stuck in OBTAINING_IPADDR after it has received one
(connectionState == NetworkInfo.DetailedState.OBTAINING_IPADDR && info.getIpAddress() != 0)
);
if (isConnected) {
callbackContext.success("Network " + ssidToConnect + " connected!");
return true;
}

Log.d(TAG, "WifiWizard: Got " + connectionState.name() + " on " + (i + 1) + " out of " + TIMES_TO_RETRY);
final int ONE_SECOND = 1000;
try {
Thread.sleep(ONE_SECOND);
}
catch (InterruptedException e) {
Log.e(TAG, e.getMessage());
callbackContext.error("Received InterruptedException while connecting");
return false;
}
}
callbackContext.error("Network " + ssidToConnect + " failed to finish connecting within the timeout");
Log.d(TAG, "WifiWizard: Network failed to finish connecting within the timeout");
return false;
}
else {
callbackContext.error("Network " + ssidToConnect + " not found!");
Expand Down