WIFI連接實現

轉載地址: 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

有錯誤多多指出


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章