I have used WifiManager to scan the available wifi using,
mainWifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mainWifi.startScan();
I can get list of networks.
but when I try to connect to Speecific wifi SSID String hotspot="some_wifi_name";
in this case, It add the hotspot to WifiConfig but after that it goes in Infinite loop while connecting and again scans the result, an loop goes on.
Following is Broadcast Receiver
I used for connecting thi my SSID,
class WifiReceiver extends BroadcastReceiver {
ArrayList<String> connections = new ArrayList<String>();
WifiConfiguration conf;
public void onReceive(Context c, Intent intent) {
WifiManager wifiManager = (WifiManager) c
.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> wifiList;
wifiList = mainWifi.getScanResults();
for (int i = 0; i < wifiList.size(); i++) {
String ssid = wifiList.get(i).SSID;
int rssi = wifiList.get(i).level;
Log.d("SSID: ", "" + wifiList.get(i).SSID.toString()
+ " signal: " + rssi);
if (ssid != null && ssid.equals(hotspot)) {
Log.d("SSID Hotspot: ",
"" + wifiList.get(i).SSID.toString());
conf = new WifiConfiguration();
conf.SSID = "\"" + hotspot + "\"";
conf.allowedKeyManagement
.set(WifiConfiguration.KeyMgmt.NONE);
wifiManager.addNetwork(conf);
}
}
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
loops:
for (WifiConfiguration j : list) {
if (j.SSID != null && j.SSID.equals("\"" + hotspot + "\"")) {
Log.d("Config", "" + j.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(j.networkId, true);
wifiManager.reconnect();
}
}
}
}
Why It is scanning the wifi again after getting desired SSID and goes in the Infinite Loop, where is the wrong thing .