我如何编程机器人寻找一个特定的网络?机器人、网络

2023-09-04 05:12:33 作者:关东煮小熊崽

我的应用程序只有当它的校园网络上,以便访问校园数据的工作。当运行在公共WiFi或3G网络中的应用程序,该应用程序将挂起然后强制关闭。怎样设置我的应用程序,以检查该专用网络/ WiFi或检查哪些连接Android是目前使用的,如果不等于校园无线网络,然后......?

My application only works if it's on the campus network in order to access campus data. When running the application on public wifi or 3g network, the application will hang then force close. How do I program my application to check for that private network/wifi or check what connection android is currently using, if not equal to "campus wifi" then....?

推荐答案

从你说的话,我认为要强制应用程序只使用校园无线网络。

From what you are saying i think you want to enforce application to use only Campus wifi.

所以,在这里你去

创建WifiConfiguration例如:

Create WifiConfiguration instance:

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的网络,你需要做的:

Then, for WEP network you need to do this:

conf.wepKeys[0] = "\"" + networkPass + "\""; 
conf.wepTxKeyIndex = 0;
conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
conf.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 

有关WPA网络,你需要添加密码是这样的:

For WPA network you need to add passphrase like this:

conf.preSharedKey = "\""+ networkPass +"\"";

有关开放式的网络,你需要做的:

For Open network you need to do this:

conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);

然后,你需要将它添加到Android的WiFi管理器设置:

Then, you need to add it to Android wifi manager settings:

WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); 
wifiManager.add(conf);

最后,您可能需要启用它,因此Android conntects吧:

And finally, you might need to enable it, so Android conntects to it:

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的情况下,如果你的密码是十六进制,你不需要用引号括起来。

Note that In case of WEP, if your password is in hex, you do not need to surround it with quotes.