1212import com .facebook .react .shell .MainReactPackage ;
1313import com .facebook .soloader .SoLoader ;
1414
15+ import android .os .Build ;
1516import android .net .wifi .ScanResult ;
1617import android .net .wifi .WifiManager ;
1718import android .net .wifi .WifiConfiguration ;
@@ -35,11 +36,13 @@ public class AndroidWifiModule extends ReactContextBaseJavaModule {
3536
3637 //WifiManager Instance
3738 WifiManager wifi ;
39+ ReactApplicationContext reactContext ;
3840
3941 //Constructor
4042 public AndroidWifiModule (ReactApplicationContext reactContext ) {
4143 super (reactContext );
4244 wifi = (WifiManager )reactContext .getSystemService (Context .WIFI_SERVICE );
45+ this .reactContext = reactContext ;
4346 }
4447
4548 //Name for module register to use:
@@ -125,15 +128,32 @@ public void connectionStatus(Callback connectionStatusResult) {
125128 }
126129
127130 //Method to connect to WIFI Network
128- public Boolean connectTo (ScanResult result , String password , String ssid ) {
131+ public Boolean connectTo (ScanResult result , String password , String ssid ) {
129132 //Make new configuration
130133 WifiConfiguration conf = new WifiConfiguration ();
134+
135+ conf .allowedAuthAlgorithms .clear ();
136+ conf .allowedGroupCiphers .clear ();
137+ conf .allowedKeyManagement .clear ();
138+ conf .allowedPairwiseCiphers .clear ();
139+ conf .allowedProtocols .clear ();
140+
141+ conf .SSID = String .format ("\" %s\" " , ssid );
142+ conf .preSharedKey = String .format ("\" %s\" " , password );
131143
132- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
133- conf .SSID = ssid ;
134- } else {
135- conf .SSID = "\" " + ssid + "\" " ;
136- }
144+ // if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
145+ // conf.SSID = ssid;
146+ // conf.preSharedKey = password;
147+ // } else {
148+ // conf.SSID = "\"" + ssid + "\"";
149+ // conf.preSharedKey = "\"" + password + "\"";
150+ // }
151+
152+
153+ WifiConfiguration tempConfig = this .IsExist (conf .SSID );
154+ if (tempConfig != null ) {
155+ wifi .removeNetwork (tempConfig .networkId );
156+ }
137157
138158 String capabilities = result .capabilities ;
139159
@@ -143,28 +163,31 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
143163
144164 // appropriate ciper is need to set according to security type used,
145165 // ifcase of not added it will not be able to connect
146- conf .preSharedKey = "\" " + password + "\" " ;
166+ // conf.preSharedKey = "\"" + password + "\"";
147167
148- conf . allowedProtocols . set ( WifiConfiguration . Protocol . RSN );
149-
150- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt . WPA_PSK );
168+ // This is needed for WPA/WPA2
169+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
170+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm . OPEN );
151171
152- conf .status = WifiConfiguration .Status .ENABLED ;
153-
154- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
155172 conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
173+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
156174
157175 conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
158176
159- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
160177 conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
178+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
161179
162180 conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
163181 conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
164-
182+ conf . status = WifiConfiguration . Status . ENABLED ;
165183 } else if (capabilities .contains ("WEP" )) {
166184 conf .wepKeys [0 ] = "\" " + password + "\" " ;
167185 conf .wepTxKeyIndex = 0 ;
186+ // This is needed for WEP
187+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
188+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .OPEN );
189+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .SHARED );
190+
168191 conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
169192 conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
170193
@@ -180,15 +203,15 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
180203 for (WifiConfiguration wifiConfig : mWifiConfigList ){
181204 if (wifiConfig .SSID .equals (conf .SSID )){
182205 conf .networkId = wifiConfig .networkId ;
183- updateNetwork = wifi . updateNetwork ( conf ) ;
206+ updateNetwork = conf . networkId ;
184207 }
185208 }
186209
187210 // If network not already in configured networks add new network
188211 if ( updateNetwork == -1 ) {
189212 updateNetwork = wifi .addNetwork (conf );
190213 wifi .saveConfiguration ();
191- };
214+ }
192215
193216 if ( updateNetwork == -1 ) {
194217 return false ;
@@ -197,12 +220,12 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
197220 boolean disconnect = wifi .disconnect ();
198221 if ( !disconnect ) {
199222 return false ;
200- };
223+ }
201224
202225 boolean enableNetwork = wifi .enableNetwork (updateNetwork , true );
203226 if ( !enableNetwork ) {
204227 return false ;
205- };
228+ }
206229
207230 return true ;
208231 }
@@ -254,25 +277,24 @@ public void getIP(final Callback callback) {
254277 //This method will remove the wifi network as per the passed SSID from the device list
255278 @ ReactMethod
256279 public void isRemoveWifiNetwork (String ssid , final Callback callback ) {
257- List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
258- for (WifiConfiguration wifiConfig : mWifiConfigList ) {
259- String comparableSSID = ('"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
260- if (wifiConfig .SSID .equals (comparableSSID )) {
261- wifi .removeNetwork (wifiConfig .networkId );
262- wifi .saveConfiguration ();
263- callback .invoke (true );
264- return ;
265- }
266- }
280+ List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
281+ for (WifiConfiguration wifiConfig : mWifiConfigList ) {
282+ String comparableSSID = ('"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
283+ if (wifiConfig .SSID .equals (comparableSSID )) {
284+ wifi .removeNetwork (wifiConfig .networkId );
285+ wifi .saveConfiguration ();
286+ callback .invoke (true );
287+ return ;
288+ }
289+ }
267290 callback .invoke (false );
268291 }
269292
270- // This method is similar to `loadWifiList` but it forcefully starts the wifi scanning on android and in the callback fetches the list
271293 @ ReactMethod
272294 public void reScanAndLoadWifiList (Callback successCallback , Callback errorCallback ) {
273295 WifiReceiver receiverWifi = new WifiReceiver (wifi , successCallback , errorCallback );
274- getReactApplicationContext ().getCurrentActivity ().registerReceiver (receiverWifi , new IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ));
275- wifi .startScan ();
296+ getReactApplicationContext ().getCurrentActivity ().registerReceiver (receiverWifi , new IntentFilter (WifiManager .SCAN_RESULTS_AVAILABLE_ACTION ));
297+ wifi .startScan ();
276298 }
277299
278300 public static String longToIP (int longIp ){
@@ -292,6 +314,16 @@ public static String longToIP(int longIp){
292314 return sb .toString ();
293315 }
294316
317+ private WifiConfiguration IsExist (String SSID ) {
318+ List <WifiConfiguration > existingConfigs = wifi .getConfiguredNetworks ();
319+ for (WifiConfiguration existingConfig : existingConfigs ) {
320+ if (existingConfig .SSID .equals ("\" " + SSID + "\" " )) {
321+ return existingConfig ;
322+ }
323+ }
324+ return null ;
325+ }
326+
295327 class WifiReceiver extends BroadcastReceiver {
296328
297329 private Callback successCallback ;
0 commit comments