I use WifiManager to check for a specific SSID and check for a given WPA password, but I get a weird result.
The code is as follows:
WifiConfiguration wc = new WifiConfiguration();
wc.SSID = "\"" + ssid + "\"";
wc2.preSharedKey = "\"" + password + "\"";
wc.hiddenSSID = true;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
boolean b = wifi.enableNetwork(res, true);
The strange part is that enableNetwork () returns true even if the Wi-Fi network with the SSID is not on the network (?!). However, note that if the target Wi-Fi network is present and the password is correct, the code successfully connects.
Android documentation says enableNetwork () "returns true if the operation completed successfully." My questions:
1) How to enable the network if it does not already exist?
2) I initialized the WifiConfiguration parameter, wc, is it wrong?
3) Is it right to do / test a connection to a Wi-Fi network?