@@ -24,6 +24,20 @@ class FailureCodes {
24
24
static int FAILED_TO_BIND_CONFIG = 4 ;
25
25
}
26
26
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
+
27
41
public class IOTWifiModule extends ReactContextBaseJavaModule {
28
42
private WifiManager wifiManager ;
29
43
private ConnectivityManager connectivityManager ;
@@ -67,13 +81,10 @@ public void run() {
67
81
}).start ();
68
82
}
69
83
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 );
75
86
if (!removeSSID (ssid )) {
76
- callback .invoke (errorFromCode (FailureCodes .SYSTEM_ADDED_CONFIG_EXISTS ));
87
+ iotWifiCallback .invoke (errorFromCode (FailureCodes .SYSTEM_ADDED_CONFIG_EXISTS ));
77
88
return ;
78
89
}
79
90
@@ -85,31 +96,31 @@ private void connectToWifi(String ssid, String passphrase, Boolean isWEP, Boolea
85
96
wifiManager .disconnect ();
86
97
boolean success = wifiManager .enableNetwork (networkId , true );
87
98
if (!success ) {
88
- callback .invoke (errorFromCode (FailureCodes .FAILED_TO_ADD_CONFIG ));
99
+ iotWifiCallback .invoke (errorFromCode (FailureCodes .FAILED_TO_ADD_CONFIG ));
89
100
return ;
90
101
}
91
102
success = wifiManager .reconnect ();
92
103
if (!success ) {
93
- callback .invoke (errorFromCode (FailureCodes .FAILED_TO_CONNECT ));
104
+ iotWifiCallback .invoke (errorFromCode (FailureCodes .FAILED_TO_CONNECT ));
94
105
return ;
95
106
}
96
107
boolean connected = pollForValidSSSID (10 , ssid );
97
108
if (!connected ) {
98
- callback .invoke (errorFromCode (FailureCodes .FAILED_TO_CONNECT ));
109
+ iotWifiCallback .invoke (errorFromCode (FailureCodes .FAILED_TO_CONNECT ));
99
110
return ;
100
111
}
101
112
if (!bindNetwork ) {
102
- callback .invoke ();
113
+ iotWifiCallback .invoke ();
103
114
return ;
104
115
}
105
116
try {
106
- bindToNetwork (ssid , callback );
117
+ bindToNetwork (ssid , iotWifiCallback );
107
118
} catch (Exception e ) {
108
119
Log .d ("IoTWifi" , "Failed to bind to Wifi: " + ssid );
109
- callback .invoke ();
120
+ iotWifiCallback .invoke ();
110
121
}
111
122
} else {
112
- callback .invoke (errorFromCode (FailureCodes .FAILED_TO_ADD_CONFIG ));
123
+ iotWifiCallback .invoke (errorFromCode (FailureCodes .FAILED_TO_ADD_CONFIG ));
113
124
}
114
125
}
115
126
@@ -149,7 +160,7 @@ private boolean pollForValidSSSID(int maxSeconds, String expectedSSID) {
149
160
return false ;
150
161
}
151
162
152
- private void bindToNetwork (final String ssid , final Callback callback ) {
163
+ private void bindToNetwork (final String ssid , final IOTWifiCallback callback ) {
153
164
NetworkRequest .Builder builder = new NetworkRequest .Builder ();
154
165
builder .addTransportType (NetworkCapabilities .TRANSPORT_WIFI );
155
166
connectivityManager .requestNetwork (builder .build (), new ConnectivityManager .NetworkCallback () {
@@ -218,7 +229,10 @@ private boolean removeSSID(String ssid) {
218
229
if (existingNetworkId == -1 ) {
219
230
return success ;
220
231
}
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
+ }
222
236
//If not our config then success would be false
223
237
return success ;
224
238
}
@@ -253,7 +267,9 @@ private WifiConfiguration getExistingNetworkConfig(String ssid) {
253
267
String comparableSSID = ('"' + ssid + '"' ); // Add quotes because wifiConfig.SSID has them
254
268
if (configList != null ) {
255
269
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 )) {
257
273
Log .d ("IoTWifi" , "Found Matching Wifi: " + wifiConfig .toString ());
258
274
existingNetworkConfigForSSID = wifiConfig ;
259
275
break ;
@@ -263,4 +279,4 @@ private WifiConfiguration getExistingNetworkConfig(String ssid) {
263
279
}
264
280
return existingNetworkConfigForSSID ;
265
281
}
266
- }
282
+ }
0 commit comments