1111import com .facebook .react .modules .core .DeviceEventManagerModule ;
1212import com .facebook .react .shell .MainReactPackage ;
1313import com .facebook .soloader .SoLoader ;
14-
1514import android .provider .Settings ;
1615import android .net .wifi .ScanResult ;
1716import android .net .wifi .WifiManager ;
@@ -41,13 +40,14 @@ public class AndroidWifiModule extends ReactContextBaseJavaModule {
4140
4241 //WifiManager Instance
4342 WifiManager wifi ;
44- ReactApplicationContext context ;
43+ ReactApplicationContext reactContext ;
4544
4645 //Constructor
4746 public AndroidWifiModule (ReactApplicationContext reactContext ) {
4847 super (reactContext );
49- wifi = (WifiManager )reactContext .getApplicationContext ().getSystemService (Context .WIFI_SERVICE );
50- context = (ReactApplicationContext ) getReactApplicationContext ();
48+
49+ wifi = (WifiManager )reactContext .getSystemService (Context .WIFI_SERVICE );
50+ this .reactContext = reactContext ;
5151 }
5252
5353 //Name for module register to use:
@@ -205,84 +205,92 @@ public void connectionStatus(Callback connectionStatusResult) {
205205 public Boolean connectTo (ScanResult result , String password , String ssid ) {
206206 //Make new configuration
207207 WifiConfiguration conf = new WifiConfiguration ();
208- if (Build .VERSION .SDK_INT >= 26 ) {
209- conf .SSID = "\" " + ssid + "\" " ;
210- } else if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
211- conf .SSID = ssid ;
212- } else {
213- conf .SSID = "\" " + ssid + "\" " ;
214- }
208+
209+ //clear alloweds
210+ conf .allowedAuthAlgorithms .clear ();
211+ conf .allowedGroupCiphers .clear ();
212+ conf .allowedKeyManagement .clear ();
213+ conf .allowedPairwiseCiphers .clear ();
214+ conf .allowedProtocols .clear ();
215+
216+ // Quote ssid and password
217+ conf .SSID = String .format ("\" %s\" " , ssid );
218+ conf .preSharedKey = String .format ("\" %s\" " , password );
219+
220+ WifiConfiguration tempConfig = this .IsExist (conf .SSID );
221+ if (tempConfig != null ) {
222+ wifi .removeNetwork (tempConfig .networkId );
223+ }
224+
225+ String capabilities = result .capabilities ;
226+
227+ // appropriate ciper is need to set according to security type used
228+ if (capabilities .contains ("WPA" ) ||
229+ capabilities .contains ("WPA2" ) ||
230+ capabilities .contains ("WPA/WPA2 PSK" )) {
231+
232+ // This is needed for WPA/WPA2
233+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
234+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .OPEN );
235+
236+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
237+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
238+
239+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
240+
241+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
242+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
243+
244+ conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
245+ conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
246+ conf .status = WifiConfiguration .Status .ENABLED ;
247+
248+ } else if (capabilities .contains ("WEP" )) {
249+ // This is needed for WEP
250+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
251+ conf .wepKeys [0 ] = "\" " + password + "\" " ;
252+ conf .wepTxKeyIndex = 0 ;
253+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .OPEN );
254+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .SHARED );
255+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
256+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
257+ } else {
258+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
259+ }
215260
216261 List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
217262 int updateNetwork = -1 ;
218263
219264 // Use the existing network config if exists
220265 for (WifiConfiguration wifiConfig : mWifiConfigList ) {
221266 if (wifiConfig .SSID .equals (conf .SSID )) {
222- updateNetwork =wifiConfig .networkId ;
223- conf =wifiConfig ;
224- }
225- }
226-
227- // If a new network, try to configure it
228- if (updateNetwork == -1 ) {
229- String capabilities = result .capabilities ;
230-
231- if (capabilities .contains ("WPA" ) ||
232- capabilities .contains ("WPA2" ) ||
233- capabilities .contains ("WPA/WPA2 PSK" )) {
234-
235- // appropriate ciper is need to set according to security type used,
236- // ifcase of not added it will not be able to connect
237- conf .preSharedKey = "\" " + password + "\" " ;
238-
239- conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
240-
241- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
242-
243- conf .status = WifiConfiguration .Status .ENABLED ;
244-
245- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
246- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
247-
248- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
249-
250- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
251- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
252-
253- conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
254- conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
255-
256- } else if (capabilities .contains ("WEP" )) {
257- conf .wepKeys [0 ] = "\" " + password + "\" " ;
258- conf .wepTxKeyIndex = 0 ;
259- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
260- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
261-
262- } else {
263- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
267+ conf =wifiConfig ;
268+ updateNetwork =conf .networkId ;
264269 }
265270 }
266271
267272 // If network not already in configured networks add new network
268273 if ( updateNetwork == -1 ) {
269- updateNetwork = wifi .addNetwork (conf );
270- wifi .saveConfiguration ();
271- };
274+ updateNetwork = wifi .addNetwork (conf );
275+ wifi .saveConfiguration ();
276+ }
272277
278+ // if network not added return false
273279 if ( updateNetwork == -1 ) {
274280 return false ;
275281 }
276282
283+ // disconnect current network
277284 boolean disconnect = wifi .disconnect ();
278285 if ( !disconnect ) {
279286 return false ;
280- };
287+ }
281288
289+ // enable new network
282290 boolean enableNetwork = wifi .enableNetwork (updateNetwork , true );
283291 if ( !enableNetwork ) {
284292 return false ;
285- };
293+ }
286294
287295 return true ;
288296 }
@@ -343,25 +351,24 @@ public void getIP(final Callback callback) {
343351 //This method will remove the wifi network as per the passed SSID from the device list
344352 @ ReactMethod
345353 public void isRemoveWifiNetwork (String ssid , final Callback callback ) {
346- List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
347- for (WifiConfiguration wifiConfig : mWifiConfigList ) {
348- String comparableSSID = ('"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
349- if (wifiConfig .SSID .equals (comparableSSID )) {
350- wifi .removeNetwork (wifiConfig .networkId );
351- wifi .saveConfiguration ();
352- callback .invoke (true );
353- return ;
354- }
355- }
354+ List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
355+ for (WifiConfiguration wifiConfig : mWifiConfigList ) {
356+ String comparableSSID = ('"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
357+ if (wifiConfig .SSID .equals (comparableSSID )) {
358+ wifi .removeNetwork (wifiConfig .networkId );
359+ wifi .saveConfiguration ();
360+ callback .invoke (true );
361+ return ;
362+ }
363+ }
356364 callback .invoke (false );
357365 }
358366
359- // This method is similar to `loadWifiList` but it forcefully starts the wifi scanning on android and in the callback fetches the list
360367 @ ReactMethod
361368 public void reScanAndLoadWifiList (Callback successCallback , Callback errorCallback ) {
362369 WifiReceiver receiverWifi = new WifiReceiver (wifi , successCallback , errorCallback );
363- getReactApplicationContext ().getCurrentActivity ().registerReceiver (receiverWifi , new IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ));
364- wifi .startScan ();
370+ getReactApplicationContext ().getCurrentActivity ().registerReceiver (receiverWifi , new IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ));
371+ wifi .startScan ();
365372 }
366373
367374 public static String longToIP (int longIp ){
@@ -381,6 +388,16 @@ public static String longToIP(int longIp){
381388 return sb .toString ();
382389 }
383390
391+ private WifiConfiguration IsExist (String SSID ) {
392+ List <WifiConfiguration > existingConfigs = wifi .getConfiguredNetworks ();
393+ for (WifiConfiguration existingConfig : existingConfigs ) {
394+ if (existingConfig .SSID .equals ("\" " + SSID + "\" " )) {
395+ return existingConfig ;
396+ }
397+ }
398+ return null ;
399+ }
400+
384401 class WifiReceiver extends BroadcastReceiver {
385402
386403 private Callback successCallback ;
0 commit comments