문제

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 .

도움이 되었습니까?

해결책 2

After adding A SSID to configuration list I Unregistered the BroadcastReceiver .

so after next scan that function will not get executed.

다른 팁

This code should let you connect to a concrete 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;
        }
    }
}
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top