@@ -128,41 +128,73 @@ public void connectionStatus(Callback connectionStatusResult) {
128128 public Boolean connectTo (ScanResult result , String password , String ssid ) {
129129 //Make new configuration
130130 WifiConfiguration conf = new WifiConfiguration ();
131- conf .SSID = "\" " + ssid + "\" " ;
132- String Capabilities = result .capabilities ;
133- if (Capabilities .contains ("WPA2" )) {
134- conf .preSharedKey = "\" " + password + "\" " ;
135- } else if (Capabilities .contains ("WPA" )) {
136- conf .preSharedKey = "\" " + password + "\" " ;
137- } else if (Capabilities .contains ("WEP" )) {
131+
132+ if (Build .VERSION .SDK_INT >= Build .VERSION_CODES .LOLLIPOP ) {
133+ conf .SSID = ssid ;
134+ } else {
135+ conf .SSID = "\" " + ssid + "\" " ;
136+ }
137+
138+ String capabilities = result .capabilities ;
139+
140+ if (capabilities .contains ("WPA" ) ||
141+ capabilities .contains ("WPA2" ) ||
142+ capabilities .contains ("WPA/WPA2 PSK" )) {
143+
144+ // appropriate ciper is need to set according to security type used,
145+ // ifcase of not added it will not be able to connect
146+ conf .preSharedKey = "\" " + password + "\" " ;
147+
148+ conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
149+
150+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
151+
152+ conf .status = WifiConfiguration .Status .ENABLED ;
153+
154+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .TKIP );
155+ conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .CCMP );
156+
157+ conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .WPA_PSK );
158+
159+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .TKIP );
160+ conf .allowedPairwiseCiphers .set (WifiConfiguration .PairwiseCipher .CCMP );
161+
162+ conf .allowedProtocols .set (WifiConfiguration .Protocol .RSN );
163+ conf .allowedProtocols .set (WifiConfiguration .Protocol .WPA );
164+
165+ } else if (capabilities .contains ("WEP" )) {
138166 conf .wepKeys [0 ] = "\" " + password + "\" " ;
139167 conf .wepTxKeyIndex = 0 ;
140168 conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
141169 conf .allowedGroupCiphers .set (WifiConfiguration .GroupCipher .WEP40 );
170+
142171 } else {
143172 conf .allowedKeyManagement .set (WifiConfiguration .KeyMgmt .NONE );
144173 }
174+
145175 //Remove the existing configuration for this netwrok
146176 List <WifiConfiguration > mWifiConfigList = wifi .getConfiguredNetworks ();
147- String comparableSSID = ( '"' + ssid + '"' ); //Add quotes because wifiConfig.SSID has them
177+
148178 int updateNetwork = -1 ;
179+
149180 for (WifiConfiguration wifiConfig : mWifiConfigList ){
150- if (wifiConfig .SSID .equals (comparableSSID )){
181+ if (wifiConfig .SSID .equals (conf . SSID )){
151182 conf .networkId = wifiConfig .networkId ;
152183 updateNetwork = wifi .updateNetwork (conf );
153184 }
154185 }
155186
156- // If network not already in configured networks add new network
187+ // If network not already in configured networks add new network
157188 if ( updateNetwork == -1 ) {
158- updateNetwork = wifi .addNetwork (conf );
189+ updateNetwork = wifi .addNetwork (conf );
190+ wifi .saveConfiguration ();
159191 };
160192
161- if ( updateNetwork == -1 ) {
162- return false ;
163- }
193+ if ( updateNetwork == -1 ) {
194+ return false ;
195+ }
164196
165- boolean disconnect = wifi .disconnect ();
197+ boolean disconnect = wifi .disconnect ();
166198 if ( !disconnect ) {
167199 return false ;
168200 };
0 commit comments