问题
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