如何连接到$ P配置P-$ WIFI上网的Andr​​oid? [英] How to connect to pre-configured Wifi in Android?

查看:261
本文介绍了如何连接到$ P配置P-$ WIFI上网的Andr​​oid?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我一直有一些麻烦搞清楚这个东西出来为我自己和我相当新的Andr​​oid在这样倒很AP preciate一些这方面的帮助。

我已经采取了看答案,像这样的对苏:<一href=\"http://stackoverflow.com/questions/8818290/how-to-connect-to-a-specific-wifi-network-in-android-programmatically\">How连接到Android中编程一个特定的wifi网络?

和我看到,你必须配置网络,手动放在密码,并将其添加到配置网络列表等。

我不知道什么人会在多个无线网络的情况下做的已经被配置/保存用户,并选择特定的一个连接到编程?
这是否意味着他们已经出现在getConfiguredNetworks()方法调用?如果是这样,怎么会去执行.enableNetwork(INT的NetID,布尔disableOthers)?

作为一个侧面说明,无论这些功能是WifiManager类的一部分。但愿这一切是有意义的!


解决方案

  WifiManager wifiManager =(WifiManager)context.getSystemService(WIFI_SERVICE);
INT NETID = -1;
为(WifiConfiguration TMP:wifiManager.getConfiguredNetworks())
   如果(tmp.SSID.equals(\\+你的SSID+\\))
   {
      NETID = tmp.networkId;
      wifiManager.enableNetwork(NETID,真);
   }

只要修改字符串你的SSID中的3号线到您想要连接的SSID。

和清单中添加此权限:

 &LT;使用许可权的android:NAME =android.permission.ACCESS_NETWORK_STATE/&GT;
&LT;使用许可权的android:NAME =android.permission.CHANGE_WIFI_STATE/&GT;

我的第一个计算器的贡献,希望它可以帮助别人:D

So I've been having some trouble figuring this stuff out for myself and I'm rather new at Android so would really appreciate some help on this.

I have taken a look at answers such as these on SO: How to connect to a specific wifi network in Android programmatically?

and I see that you have to configure a network, manually put in the password, and add it to the list of configured networks, etc.

I was wondering what one would do in the case of multiple wifi already being configured/saved by the user, and choosing a specific one to connect to programmatically? Does this mean that they already appear in the getConfiguredNetworks() method call? If so, how would one go about executing .enableNetwork(int netId, boolean disableOthers)?

As a side note, both of those functions are part of the WifiManager class. Hopefully this all made sense!

解决方案

WifiManager wifiManager = (WifiManager)context.getSystemService(WIFI_SERVICE);              
int netId = -1;         
for (WifiConfiguration tmp : wifiManager.getConfiguredNetworks()) 
   if (tmp.SSID.equals( "\""+"Your SSID"+"\"")) 
   {
      netId = tmp.networkId;                   
      wifiManager.enableNetwork(netId, true);
   }

Just change the string "Your SSID" in the 3rd line to the SSID you want to connect.

And add this permissions in the manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

My first contribution to StackOverflow, hope it helps someone :D

这篇关于如何连接到$ P配置P-$ WIFI上网的Andr​​oid?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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