Skip to content

Commit 1a9b662

Browse files
authored
Merge pull request #6 from josectobar/fix/update_android_10
added changes for wifiModule.java
2 parents ac0bf76 + 00927fe commit 1a9b662

File tree

1 file changed

+33
-17
lines changed

1 file changed

+33
-17
lines changed

android/src/main/java/com/tadasr/IOTWifi/IOTWifiModule.java

+33-17
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,20 @@ class FailureCodes {
2424
static int FAILED_TO_BIND_CONFIG = 4;
2525
}
2626

27+
class IOTWifiCallback {
28+
private Callback callback;
29+
30+
public IOTWifiCallback(Callback callback) {
31+
this.callback = callback;
32+
}
33+
34+
public void invoke(Object... args) {
35+
if (callback == null) return;
36+
callback.invoke(args);
37+
callback = null;
38+
}
39+
}
40+
2741
public class IOTWifiModule extends ReactContextBaseJavaModule {
2842
private WifiManager wifiManager;
2943
private ConnectivityManager connectivityManager;
@@ -67,13 +81,10 @@ public void run() {
6781
}).start();
6882
}
6983

70-
private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolean bindNetwork, Callback callback) {
71-
if (Build.VERSION.SDK_INT > 28) {
72-
callback.invoke("Not supported on Android Q");
73-
return;
74-
}
84+
private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolean bindNetwork, final Callback callback) {
85+
IOTWifiCallback iotWifiCallback = new IOTWifiCallback(callback);
7586
if (!removeSSID(ssid)) {
76-
callback.invoke(errorFromCode(FailureCodes.SYSTEM_ADDED_CONFIG_EXISTS));
87+
iotWifiCallback.invoke(errorFromCode(FailureCodes.SYSTEM_ADDED_CONFIG_EXISTS));
7788
return;
7889
}
7990

@@ -85,31 +96,31 @@ private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolea
8596
wifiManager.disconnect();
8697
boolean success = wifiManager.enableNetwork(networkId, true);
8798
if (!success) {
88-
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
99+
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
89100
return;
90101
}
91102
success = wifiManager.reconnect();
92103
if (!success) {
93-
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
104+
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
94105
return;
95106
}
96107
boolean connected = pollForValidSSSID(10, ssid);
97108
if (!connected) {
98-
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
109+
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_CONNECT));
99110
return;
100111
}
101112
if (!bindNetwork) {
102-
callback.invoke();
113+
iotWifiCallback.invoke();
103114
return;
104115
}
105116
try {
106-
bindToNetwork(ssid, callback);
117+
bindToNetwork(ssid, iotWifiCallback);
107118
} catch (Exception e) {
108119
Log.d("IoTWifi", "Failed to bind to Wifi: " + ssid);
109-
callback.invoke();
120+
iotWifiCallback.invoke();
110121
}
111122
} else {
112-
callback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
123+
iotWifiCallback.invoke(errorFromCode(FailureCodes.FAILED_TO_ADD_CONFIG));
113124
}
114125
}
115126

@@ -149,7 +160,7 @@ private boolean pollForValidSSSID(int maxSeconds, String expectedSSID) {
149160
return false;
150161
}
151162

152-
private void bindToNetwork(final String ssid, final Callback callback) {
163+
private void bindToNetwork(final String ssid, final IOTWifiCallback callback) {
153164
NetworkRequest.Builder builder = new NetworkRequest.Builder();
154165
builder.addTransportType(NetworkCapabilities.TRANSPORT_WIFI);
155166
connectivityManager.requestNetwork(builder.build(), new ConnectivityManager.NetworkCallback() {
@@ -218,7 +229,10 @@ private boolean removeSSID(String ssid) {
218229
if (existingNetworkId == -1) {
219230
return success;
220231
}
221-
success = wifiManager.removeNetwork(existingNetworkId) && wifiManager.saveConfiguration();
232+
success = wifiManager.removeNetwork(existingNetworkId);
233+
if (success && Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
234+
success = wifiManager.saveConfiguration();
235+
}
222236
//If not our config then success would be false
223237
return success;
224238
}
@@ -253,7 +267,9 @@ private WifiConfiguration getExistingNetworkConfig(String ssid) {
253267
String comparableSSID = ('"' + ssid + '"'); // Add quotes because wifiConfig.SSID has them
254268
if (configList != null) {
255269
for (WifiConfiguration wifiConfig : configList) {
256-
if (wifiConfig.SSID.equals(comparableSSID)) {
270+
String savedSSID = wifiConfig.SSID;
271+
if (savedSSID == null) continue; // In few cases SSID is found to be null, ignore those configs
272+
if (savedSSID.equals(comparableSSID)) {
257273
Log.d("IoTWifi", "Found Matching Wifi: "+ wifiConfig.toString());
258274
existingNetworkConfigForSSID = wifiConfig;
259275
break;
@@ -263,4 +279,4 @@ private WifiConfiguration getExistingNetworkConfig(String ssid) {
263279
}
264280
return existingNetworkConfigForSSID;
265281
}
266-
}
282+
}

0 commit comments

Comments
 (0)