I am trying to connect an open Wifi network
using the following code:
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + SSID + "\"";
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int netid = wifiManager.addNetwork(conf);
wifiManager.saveConfiguration();
if(netid != -1){
wifiManager.enableNetwork(netid,true);
}
Wifi connects 100%
times using above code but takes long time to connect. enableNetwork
method returns true
immediately and after 20-40 seconds
wifi becomes connected. I also tried wifimanager.reconnct()
but no luck.
this method connects to your wifi - networkSSID -is your network ssid and networPass is your network password
private void connect(String networkSSID ,String networkPass )
{
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\"";
if (scanResult.capabilities.contains("WEP")) {
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
}
else if (scanResult.capabilities.contains("WPA")) {
conf.preSharedKey = "\"" + networkPass + "\"";
}
else
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
wifiManager.addNetwork(conf);
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for (WifiConfiguration i : list) {
if (i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wifiManager.disconnect();
wifiManager.enableNetwork(i.networkId, true);
wifiManager.reconnect();
break;
}
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.