How do I connect to a particular Wifi network using the SSID? I have tried using the code below.
wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiConfiguration config = new WifiConfiguration();
if(wifi.startScan()){
List<ScanResult> results = wifi.getScanResults();
for(int i=0;i<results.size();i++)
{ Log.e("VALUE"," "+results.get(i).toString());
Log.e("",""+results.get(i).SSID);
if(results.get(i).SSID.equalsIgnoreCase("\"MAC\""))
{
Log.e("","mac.....");
try{
String ssid="\""+results.get(i).SSID+"\mac"";
Log.e("SSId"," "+ssid);
config.SSID=ssid;
}catch(Exception e){Log.e("","Error : "+e.toString());}
config.preSharedKey="0a0b0f62170ecc5bcf721b6ff170b8b560101b5d56b00a26abec217e0bb4aa1f";
config.status=WifiConfiguration.Status.ENABLED;
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res=wifi.addNetwork(config);
Log.e("ENABLE ",""+wifi.enableNetwork(res, false));
break;
}
}
}
I have not found any information anywhere on how to do this? Where am I going wrong?
Your problem may be related to the problem I was having. If you take the SSID value directly from a scan result it is not in double quotes, but for some reason WifiConfiguration requires that the SSID is in double quotes. Please see my answer.
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.