44import com .facebook .react .bridge .*;
55import com .facebook .systrace .Systrace ;
66import com .facebook .systrace .SystraceMessage ;
7- // import com.facebook.react.LifecycleState;
87import com .facebook .react .ReactInstanceManager ;
98import com .facebook .react .ReactRootView ;
109import com .facebook .react .modules .core .DefaultHardwareBackBtnHandler ;
@@ -67,20 +66,20 @@ public void loadWifiList(Callback successCallback, Callback errorCallback) {
6766 JSONObject wifiObject = new JSONObject ();
6867 if (!result .SSID .equals ("" )){
6968 try {
70- wifiObject .put ("SSID" , result .SSID );
71- wifiObject .put ("BSSID" , result .BSSID );
72- wifiObject .put ("capabilities" , result .capabilities );
73- wifiObject .put ("frequency" , result .frequency );
74- wifiObject .put ("level" , result .level );
75- wifiObject .put ("timestamp" , result .timestamp );
76- //Other fields not added
77- //wifiObject.put("operatorFriendlyName", result.operatorFriendlyName);
78- //wifiObject.put("venueName", result.venueName);
79- //wifiObject.put("centerFreq0", result.centerFreq0);
80- //wifiObject.put("centerFreq1", result.centerFreq1);
81- //wifiObject.put("channelWidth", result.channelWidth);
69+ wifiObject .put ("SSID" , result .SSID );
70+ wifiObject .put ("BSSID" , result .BSSID );
71+ wifiObject .put ("capabilities" , result .capabilities );
72+ wifiObject .put ("frequency" , result .frequency );
73+ wifiObject .put ("level" , result .level );
74+ wifiObject .put ("timestamp" , result .timestamp );
75+ //Other fields not added
76+ //wifiObject.put("operatorFriendlyName", result.operatorFriendlyName);
77+ //wifiObject.put("venueName", result.venueName);
78+ //wifiObject.put("centerFreq0", result.centerFreq0);
79+ //wifiObject.put("centerFreq1", result.centerFreq1);
80+ //wifiObject.put("channelWidth", result.channelWidth);
8281 } catch (JSONException e ) {
83- errorCallback .invoke (e .getMessage ());
82+ errorCallback .invoke (e .getMessage ());
8483 }
8584 wifiArray .put (wifiObject );
8685 }
@@ -116,10 +115,8 @@ public void forceWifiUsage(boolean useWifi) {
116115
117116 context .startActivity (intent );
118117 }
119-
120118 }
121119
122-
123120 if (((Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) && canWriteFlag ) || ((Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) && !(Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ))) {
124121 final ConnectivityManager manager = (ConnectivityManager ) context
125122 .getSystemService (Context .CONNECTIVITY_SERVICE );
@@ -146,8 +143,6 @@ public void onAvailable(Network network) {
146143 }
147144 });
148145 }
149-
150-
151146 }
152147 } else {
153148 if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .M ) {
@@ -206,56 +201,54 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
206201 //Make new configuration
207202 WifiConfiguration conf = new WifiConfiguration ();
208203
209- //clear alloweds
204+ //clear alloweds
210205 conf .allowedAuthAlgorithms .clear ();
211206 conf .allowedGroupCiphers .clear ();
212207 conf .allowedKeyManagement .clear ();
213208 conf .allowedPairwiseCiphers .clear ();
214209 conf .allowedProtocols .clear ();
215210
216- // Quote ssid and password
211+ // Quote ssid and password
217212 conf .SSID = String .format ("\" %s\" " , ssid );
218- conf .preSharedKey = String .format ("\" %s\" " , password );
213+ conf .preSharedKey = String .format ("\" %s\" " , password );
219214
220- WifiConfiguration tempConfig = this .IsExist (conf .SSID );
215+ WifiConfiguration tempConfig = this .IsExist (conf .SSID );
221216 if (tempConfig != null ) {
222217 wifi .removeNetwork (tempConfig .networkId );
223218 }
224219
225220 String capabilities = result .capabilities ;
226221
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" )) {
222+ // appropriate ciper is need to set according to security type used
223+ if (capabilities .contains ("WPA" ) || capabilities .contains ("WPA2" ) || capabilities .contains ("WPA/WPA2 PSK" )) {
231224
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 );
225+ // This is needed for WPA/WPA2
226+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
227+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .OPEN );
235228
236- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
237- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
229+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
230+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
238231
239- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
232+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
240233
241- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
242- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
234+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
235+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
243236
244- conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
245- conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
246- conf .status = WifiConfiguration .Status .ENABLED ;
237+ conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
238+ conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
239+ conf .status = WifiConfiguration .Status .ENABLED ;
247240
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 );
241+ } else if (capabilities .contains ("WEP" )) {
242+ // This is needed for WEP
243+ // Reference - https://android.googlesource.com/platform/frameworks/base/+/refs/heads/master/wifi/java/android/net/wifi/WifiConfiguration.java#149
244+ conf .wepKeys [0 ] = "\" " + password + "\" " ;
245+ conf .wepTxKeyIndex = 0 ;
246+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .OPEN );
247+ conf .allowedAuthAlgorithms .set (WifiConfiguration .AuthAlgorithm .SHARED );
248+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
249+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
250+ } else {
251+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
259252 }
260253
261254 List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
@@ -264,29 +257,29 @@ public Boolean connectTo(ScanResult result, String password, String ssid) {
264257 // Use the existing network config if exists
265258 for (WifiConfiguration wifiConfig : mWifiConfigList ) {
266259 if (wifiConfig .SSID .equals (conf .SSID )) {
267- conf =wifiConfig ;
260+ conf =wifiConfig ;
268261 updateNetwork =conf .networkId ;
269262 }
270263 }
271264
272265 // If network not already in configured networks add new network
273266 if ( updateNetwork == -1 ) {
274- updateNetwork = wifi .addNetwork (conf );
275- wifi .saveConfiguration ();
267+ updateNetwork = wifi .addNetwork (conf );
268+ wifi .saveConfiguration ();
276269 }
277270
278- // if network not added return false
271+ // if network not added return false
279272 if ( updateNetwork == -1 ) {
280273 return false ;
281274 }
282275
283- // disconnect current network
276+ // disconnect current network
284277 boolean disconnect = wifi .disconnect ();
285278 if ( !disconnect ) {
286279 return false ;
287280 }
288281
289- // enable new network
282+ // enable new network
290283 boolean enableNetwork = wifi .enableNetwork (updateNetwork , true );
291284 if ( !enableNetwork ) {
292285 return false ;
@@ -353,13 +346,13 @@ public void getIP(final Callback callback) {
353346 public void isRemoveWifiNetwork (String ssid , final Callback callback ) {
354347 List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
355348 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- }
349+ String comparableSSID = ('"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
350+ if (wifiConfig .SSID .equals (comparableSSID )) {
351+ wifi .removeNetwork (wifiConfig .networkId );
352+ wifi .saveConfiguration ();
353+ callback .invoke (true );
354+ return ;
355+ }
363356 }
364357 callback .invoke (false );
365358 }
@@ -400,50 +393,50 @@ private WifiConfiguration IsExist(String SSID) {
400393
401394 class WifiReceiver extends BroadcastReceiver {
402395
403- private Callback successCallback ;
404- private Callback errorCallback ;
405- private WifiManager wifi ;
406-
407- public WifiReceiver (final WifiManager wifi , Callback successCallback , Callback errorCallback ) {
408- super ();
409- this .successCallback = successCallback ;
410- this .errorCallback = errorCallback ;
411- this .wifi = wifi ;
412- }
413-
414- // This method call when number of wifi connections changed
415- public void onReceive (Context c , Intent intent ) {
416- // LocalBroadcastManager.getInstance(c).unregisterReceiver(this);
417- c .unregisterReceiver (this );
418- // getReactApplicationContext().getCurrentActivity().registerReceiver
419- try {
420- List < ScanResult > results = this .wifi .getScanResults ();
421- JSONArray wifiArray = new JSONArray ();
422-
423- for (ScanResult result : results ) {
424- JSONObject wifiObject = new JSONObject ();
425- if (!result .SSID .equals ("" )){
426- try {
427- wifiObject .put ("SSID" , result .SSID );
428- wifiObject .put ("BSSID" , result .BSSID );
429- wifiObject .put ("capabilities" , result .capabilities );
430- wifiObject .put ("frequency" , result .frequency );
431- wifiObject .put ("level" , result .level );
432- wifiObject .put ("timestamp" , result .timestamp );
433- } catch (JSONException e ) {
434- this .errorCallback .invoke (e .getMessage ());
435- return ;
436- }
437- wifiArray .put (wifiObject );
396+ private Callback successCallback ;
397+ private Callback errorCallback ;
398+ private WifiManager wifi ;
399+
400+ public WifiReceiver (final WifiManager wifi , Callback successCallback , Callback errorCallback ) {
401+ super ();
402+ this .successCallback = successCallback ;
403+ this .errorCallback = errorCallback ;
404+ this .wifi = wifi ;
405+ }
406+
407+ // This method call when number of wifi connections changed
408+ public void onReceive (Context c , Intent intent ) {
409+
410+ c .unregisterReceiver (this );
411+
412+ try {
413+ List < ScanResult > results = this .wifi .getScanResults ();
414+ JSONArray wifiArray = new JSONArray ();
415+
416+ for (ScanResult result : results ) {
417+ JSONObject wifiObject = new JSONObject ();
418+ if (!result .SSID .equals ("" )){
419+ try {
420+ wifiObject .put ("SSID" , result .SSID );
421+ wifiObject .put ("BSSID" , result .BSSID );
422+ wifiObject .put ("capabilities" , result .capabilities );
423+ wifiObject .put ("frequency" , result .frequency );
424+ wifiObject .put ("level" , result .level );
425+ wifiObject .put ("timestamp" , result .timestamp );
426+ } catch (JSONException e ) {
427+ this .errorCallback .invoke (e .getMessage ());
428+ return ;
438429 }
430+ wifiArray .put (wifiObject );
439431 }
440- this .successCallback .invoke (wifiArray .toString ());
441- return ;
442- } catch (IllegalViewOperationException e ) {
443- this .errorCallback .invoke (e .getMessage ());
444- return ;
445432 }
446- }
447- }
433+ this .successCallback .invoke (wifiArray .toString ());
434+ return ;
435+ } catch (IllegalViewOperationException e ) {
436+ this .errorCallback .invoke (e .getMessage ());
437+ return ;
438+ }
439+ }
440+ }
448441}
449442
0 commit comments