@@ -205,73 +205,76 @@ 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-
209- if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
210- conf .SSID = ssid ;
211- } else {
212- conf .SSID = "\" " + ssid + "\" " ;
213- }
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+ }
214215
215- String capabilities = result .capabilities ;
216-
217- if (capabilities .contains ("WPA" ) ||
218- capabilities .contains ("WPA2" ) ||
219- capabilities .contains ("WPA/WPA2 PSK" )) {
220-
221- // appropriate ciper is need to set according to security type used,
222- // ifcase of not added it will not be able to connect
223- conf .preSharedKey = "\" " + password + "\" " ;
224-
225- conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
226-
227- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
228-
229- conf .status = WifiConfiguration .Status .ENABLED ;
230-
231- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
232- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
233-
234- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
235-
236- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
237- conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
238-
239- conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
240- conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
241-
242- } else if (capabilities .contains ("WEP" )) {
243- conf .wepKeys [0 ] = "\" " + password + "\" " ;
244- conf .wepTxKeyIndex = 0 ;
245- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
246- conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
216+ List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
217+ int updateNetwork = -1 ;
247218
248- } else {
249- conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
219+ // Use the existing network config if exists
220+ for (WifiConfiguration wifiConfig : mWifiConfigList ) {
221+ if (wifiConfig .SSID .equals (conf .SSID )) {
222+ updateNetwork =wifiConfig .networkId ;
223+ conf =wifiConfig ;
224+ }
250225 }
251226
252- //Remove the existing configuration for this netwrok
253- List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
227+ // If a new network, try to configure it
228+ if (updateNetwork == -1 ) {
229+ String capabilities = result .capabilities ;
254230
255- int updateNetwork = -1 ;
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 );
256240
257- for (WifiConfiguration wifiConfig : mWifiConfigList ){
258- if (wifiConfig .SSID .equals (conf .SSID )){
259- conf .networkId = wifiConfig .networkId ;
260- updateNetwork = wifi .updateNetwork (conf );
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 );
261264 }
262265 }
263266
264- // If network not already in configured networks add new network
267+ // If network not already in configured networks add new network
265268 if ( updateNetwork == -1 ) {
266- updateNetwork = wifi .addNetwork (conf );
267- wifi .saveConfiguration ();
269+ updateNetwork = wifi .addNetwork (conf );
270+ wifi .saveConfiguration ();
268271 };
269272
270- if ( updateNetwork == -1 ) {
271- return false ;
272- }
273+ if ( updateNetwork == -1 ) {
274+ return false ;
275+ }
273276
274- boolean disconnect = wifi .disconnect ();
277+ boolean disconnect = wifi .disconnect ();
275278 if ( !disconnect ) {
276279 return false ;
277280 };
@@ -426,3 +429,4 @@ public void onReceive(Context c, Intent intent) {
426429 }
427430 }
428431}
432+
0 commit comments