根据你所说的,我认为你想强制应用程序仅使用校园 wifi。
所以你来吧
创建 WifiConfiguration 实例:
String networkSSID = "put network SSID here";
String networkPass = "put network password here";
WifiConfiguration conf = new WifiConfiguration();
conf.SSID = "\"" + networkSSID + "\""; //ssid must be in quotes
然后,对于 WEP 网络,您需要执行以下操作:
conf.wepKeys[0] = "\"" + networkPass + "\"";
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
对于 WPA 网络,您需要添加如下密码:
conf.preSharedKey = "\""+ networkPass +"\"";
对于开放网络,您需要执行以下操作:
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
然后,您需要将其添加到 Android wifi 管理器设置中:
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
wifiManager.add(conf);
最后,您可能需要启用它,以便 Android 连接到它:
List<WifiConfiguration> list = wifiManager.getConfiguredNetworks();
for( WifiConfiguration i : list ) {
if(i.SSID != null && i.SSID.equals("\"" + networkSSID + "\"")) {
wm.disconnect();
wm.enableNetwork(i.networkId, true);
wm.reconnect();
break;
}
}
请注意,对于 WEP,如果您的密码是十六进制,则无需将其用引号引起来。