【发布时间】:2011-08-24 06:48:37
【问题描述】:
我想做一个让用户连接到 wifi 网络的应用程序,但是我无法连接到网络
我当前的代码是:
WifiManager wifi = (WifiManager) getSystemService(WIFI_SERVICE);
wifi.setWifiEnabled(true);
WifiConfiguration wc = new WifiConfiguration();
wifi.startScan();
List<ScanResult> l=wifi.getScanResults();
wc.SSID = l.get(NUMBER).SSID;
post(wc.SSID);
/*This is the bit that I think is failing, my network does not have these properties.. but I can't see how to get them from the Scan Result*/
wc.preSharedKey = "\"passw0rd123\"";
wc.hiddenSSID = false;
wc.status = WifiConfiguration.Status.ENABLED;
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
int res = wifi.addNetwork(wc);
post("add Network returned " + res);
boolean b = wifi.enableNetwork(res, true);
post("enableNetwork returned " + b);
我认为这与我的网络配置不同的设置(在我的评论之后)有关,但我不知道如何从 ScanResult 中获取这些设置..
编辑: 我也想知道它是否连接正确。
【问题讨论】:
-
嘿伙计,你在那个问题上找到了什么吗?