我如何编程机器人寻找一个特定的网络? [英] How do I program android to look for a particular network?

查看:143
本文介绍了我如何编程机器人寻找一个特定的网络?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的应用程序只有当它的校园网络上,以便访问校园数据的工作。当运行在公共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.

这篇关于我如何编程机器人寻找一个特定的网络?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆