Adding an Android WiFi connection results in -1

爱⌒轻易说出口 提交于 2019-12-13 00:40:56

问题


I am trying to create a wi-fi connection from a scan result. The only advertised capability is ESS and it is a network with no security details.

    WifiConfiguration wc = new WifiConfiguration();
        wc.SSID = result.SSID;
        wc.BSSID = result.BSSID;

        //No password. it should be an open network
        wc.status = WifiConfiguration.Status.ENABLED;
        wc.priority = 100000;
        wc.hiddenSSID = false;
        int netId = mainWifi.addNetwork(wc);

        if (netId == -1) 
        {
            showMessageDialog("Error connecting to network.");
            return;
        }
        mainWifi.enableNetwork(netId, true);
        mainWifi.setWifiEnabled(true);

I keep getting -1, which is completely unhelpful and neither the console or logcat is giving me any output on this.

Am I missing something? Is there a way to debug this problem?


回答1:


The SSID must be in Quotes:

wc.SSID = "\"SSID_NAME\""; //IMP! This should be in Quotes!!

Answer comes from this question: https://stackoverflow.com/a/8818921/178931




回答2:


WifiConfiguration wc = new WifiConfiguration();
    wc.SSID = "\"" + result.SSID + "\"";
    wc.BSSID = "\"" + result.BSSID + "\"";


来源:https://stackoverflow.com/questions/12603757/adding-an-android-wifi-connection-results-in-1

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!