I have given data from my runtime
ssid = "Some SSID";
password = "myPassword";
and I want to use them to connect to a WIFI. My Code so far:
try {
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
conf.preSharedKey = "\"" + password + "\"";
conf.status = WifiConfiguration.Status.ENABLED;
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
conf.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
WifiManager wifiManager = (WifiManager) MainActivity.this.getSystemService(Context.WIFI_SERVICE);
int netId = wifiManager.addNetwork(conf);
wifiManager.enableNetwork(netId, true);
return true;
} catch (Exception ex) {
ex.printStackTrace();
return false;
}
// ignore the returns
The problem is, my addNetork()
returns -1 and thus my enableNetowrk()
fails. I am open to any suggestions or even refactor. I also know that it will be WPA for all cases so there is no differ needed...