I am programming an Android app for an IoT device. I have to give the device the WiFi username and password, so I am using an android app to do this. I have the following code, but it seems to always connect to the network weather or not the correct password is given.
This I am testing this on the same AP that my phone is connected to.
The desired action is
Disconnect from current AP -> Attempt to connect using credentials given -> Reconnect to original network.
What steps do I need to take to correctly verify a wifi network and password?
code :
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + ssid + "\"";
for(ScanResult sr: wifiList){
if(sr.SSID.equals(ssid)){
if(sr.capabilities.contains("WEP")){
if(isNumeric(pass)){
conf.wepKeys[0] = pass ;
}else{
conf.wepKeys[0] = "\"" + pass + "\"";
}
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
}else if(sr.capabilities.contains("PSK")){
conf.preSharedKey = "\""+ pass +"\"";
}else if(sr.capabilities.contains("EAP")){
wifiName.setError("EAP networks not supported");
//todo support EAP
}else{
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
WifiManager wifiManager = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.equals(conf)) {
wifiManager.disconnect();
if(!wifiManager.enableNetwork(i.networkId, true)){
wifiPassword.setError("Incorrect Password");
wifiManager.reconnect();
return;
}else{
wifiManager.reconnect();
addUser(deviceSN, ssid, pass);
}
}
}
break;
}
}