WIFI连接实现

来源:互联网 发布:二次元狂热 淘宝 编辑:程序博客网 时间:2024/05/18 01:23

转载地址: http://blog.csdn.net/liuhui_8989/article/details/22962537



目录(?)[+]

实现目标:搜索WIFI,手动输入密码并保存,连接WIFI。第二次连接该WIFI信号不需要输入密码

首先在AndroidManifest.XML中开启响应的权限

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"></uses-permission><!-- 允许程序改变网络链接状态 -->
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission><!--允许程序访问访问WIFI网络状态信息 -->
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission><!-- 允许程序改变WIFI链接状态 -->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
 来自CODE的代码片
wifiPermission

1、开启WIFI

<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a><a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a><a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a>
//开启WIFI
public void WifiOpen(){
if(!localWifiManager.isWifiEnabled()){
localWifiManager.setWifiEnabled(true);
}
}
 来自CODE的代码片
OpenWIFI

2、扫描WIFI信号
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
//扫描wifi
public void WifiStartScan(){
localWifiManager.startScan();
}
 来自CODE的代码片
StartScan

3、得到扫描WIFI结果
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a>
//得到Scan结果
public List<ScanResult> getScanResults(){
return localWifiManager.getScanResults();//得到扫描结果
}
 来自CODE的代码片
getScan

4、得到WIFi的配置好的信息,包含配置好的密码
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;"> 1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;"> 2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;"> 3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;"> 4</a><a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;"> 5</a><a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;"> 6</a><a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;"> 7</a><a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;"> 8</a>
//得到Wifi配置好的信息
public void getConfiguration(){
wifiConfigList = localWifiManager.getConfiguredNetworks();//得到配置好的网络信息
for(int i =0;i<wifiConfigList.size();i++){
Log.i("getConfiguration",wifiConfigList.get(i).SSID);
Log.i("getConfiguration",String.valueOf(wifiConfigList.get(i).networkId));
}
}
 来自CODE的代码片
getConfig

5、根据WIFI的名称SSID判定指定WIFI是否已经配置好,配置好则返回其networkId,用于连接。之前尝试了BSSID地址没成功,所以只能使用SSID
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a><a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a><a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a><a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a><a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a><a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a><a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a><a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a>
//判定指定WIFI是否已经配置好,依据WIFI的地址BSSID,返回NetId
public int IsConfiguration(String SSID){
Log.i("IsConfiguration",String.valueOf(wifiConfigList.size()));
for(int i = 0; i < wifiConfigList.size(); i++){
Log.i(wifiConfigList.get(i).SSID,String.valueOf( wifiConfigList.get(i).networkId));
if(wifiConfigList.get(i).SSID.equals(SSID)){//地址相同
return wifiConfigList.get(i).networkId;
}
}
return -1;
}
 来自CODE的代码片
IsConfiguration

6、如果需要连接的WIFI没有配置好,即没有保存密码。则为指定名称ssid的WIFI添加密码信息psw,添加成功后返回给其分配的networId,同于连接
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a><a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a><a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a><a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a><a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a><a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a><a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a><a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a><a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a><a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a><a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a><a target=_blank id="L15" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L15" rel="#L15" style="color: rgb(102, 102, 102); text-decoration: none;"> 15</a><a target=_blank id="L16" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L16" rel="#L16" style="color: rgb(102, 102, 102); text-decoration: none;"> 16</a><a target=_blank id="L17" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L17" rel="#L17" style="color: rgb(102, 102, 102); text-decoration: none;"> 17</a><a target=_blank id="L18" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L18" rel="#L18" style="color: rgb(102, 102, 102); text-decoration: none;"> 18</a><a target=_blank id="L19" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L19" rel="#L19" style="color: rgb(102, 102, 102); text-decoration: none;"> 19</a><a target=_blank id="L20" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L20" rel="#L20" style="color: rgb(102, 102, 102); text-decoration: none;"> 20</a>
//添加指定WIFI的配置信息,原列表不存在此SSID
public int AddWifiConfig(List<ScanResult> wifiList,String ssid,String pwd){
int wifiId = -1;
for(int i = 0;i < wifiList.size(); i++){
ScanResult wifi = wifiList.get(i);
if(wifi.SSID.equals(ssid)){
Log.i("AddWifiConfig","equals");
WifiConfiguration wifiCong = new WifiConfiguration();
wifiCong.SSID = "\""+wifi.SSID+"\"";//\"转义字符,代表"
wifiCong.preSharedKey = "\""+pwd+"\"";//WPA-PSK密码
wifiCong.hiddenSSID = false;
wifiCong.status = WifiConfiguration.Status.ENABLED;
wifiId = localWifiManager.addNetwork(wifiCong);//将配置好的特定WIFI密码信息添加,添加完成后默认是不激活状态,成功返回ID,否则为-1
if(wifiId != -1){
return wifiId;
}
}
}
return wifiId;
}
 来自CODE的代码片
AddWifiConfig

7、根据步骤6配置好需要连接的WIFI密码信息后,下面通过networkId连接指定WIFI。在连接经过步骤6刚添加配置信息的WIFI信号之前需要重新执行下步骤4,得到新的配置好信息的列表
<a target=_blank id="L1" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L1" rel="#L1" style="color: rgb(102, 102, 102); text-decoration: none;">  1</a><a target=_blank id="L2" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L2" rel="#L2" style="color: rgb(102, 102, 102); text-decoration: none;">  2</a><a target=_blank id="L3" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L3" rel="#L3" style="color: rgb(102, 102, 102); text-decoration: none;">  3</a><a target=_blank id="L4" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L4" rel="#L4" style="color: rgb(102, 102, 102); text-decoration: none;">  4</a><a target=_blank id="L5" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L5" rel="#L5" style="color: rgb(102, 102, 102); text-decoration: none;">  5</a><a target=_blank id="L6" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L6" rel="#L6" style="color: rgb(102, 102, 102); text-decoration: none;">  6</a><a target=_blank id="L7" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L7" rel="#L7" style="color: rgb(102, 102, 102); text-decoration: none;">  7</a><a target=_blank id="L8" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L8" rel="#L8" style="color: rgb(102, 102, 102); text-decoration: none;">  8</a><a target=_blank id="L9" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L9" rel="#L9" style="color: rgb(102, 102, 102); text-decoration: none;">  9</a><a target=_blank id="L10" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L10" rel="#L10" style="color: rgb(102, 102, 102); text-decoration: none;"> 10</a><a target=_blank id="L11" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L11" rel="#L11" style="color: rgb(102, 102, 102); text-decoration: none;"> 11</a><a target=_blank id="L12" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L12" rel="#L12" style="color: rgb(102, 102, 102); text-decoration: none;"> 12</a><a target=_blank id="L13" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L13" rel="#L13" style="color: rgb(102, 102, 102); text-decoration: none;"> 13</a><a target=_blank id="L14" href="http://blog.csdn.net/liuhui_8989/article/details/22962537#L14" rel="#L14" style="color: rgb(102, 102, 102); text-decoration: none;"> 14</a>
//连接指定Id的WIFI
public boolean ConnectWifi(int wifiId){
for(int i = 0; i < wifiConfigList.size(); i++){
WifiConfiguration wifi = wifiConfigList.get(i);
if(wifi.networkId == wifiId){
while(!(localWifiManager.enableNetwork(wifiId, true))){//激活该Id,建立连接
//status:0--已经连接,1--不可连接,2--可以连接
Log.i("ConnectWifi",String.valueOf(wifiConfigList.get(wifiId).status));
}
return true;
}
}
return false;
}
 来自CODE的代码片
ConnectWifi

这只是简单的应用。

project源码:http://download.csdn.net/detail/liuhui_8989/7154671

有错误多多指出


0 0