1

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 .

4

2 回答 2

1

将 SSID 添加到配置列表后,我取消注册了BroadcastReceiver.

所以在下一次扫描之后,该函数将不会被执行。

于 2014-05-29T14:04:49.797 回答
1

此代码应允许您连接到具体的 SSID:

public void connectToWifi(String ssid) {

    WifiConfiguration conf = new WifiConfiguration();
    conf.SSID = "\"" + ssid + "\""; // Please note the quotes.
                                                // String should contain
                                                // ssid in quotes

    conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

    wifiMgr.addNetwork(conf);
    Log.d(TAG, ssid+" added");

    List<WifiConfiguration> list = wifiMgr.getConfiguredNetworks();
    for (WifiConfiguration i : list) {
        if (i.SSID != null && i.SSID.equals("\"" + ssid + "\"")) {
            wifiMgr.disconnect();
            wifiMgr.enableNetwork(i.networkId, true);
            wifiMgr.reconnect();
            Log.d(PluginConstants.LOG_TAG, "conneting to: ssid");
            break;
        }
    }
}
于 2014-03-26T09:21:21.420 回答