android wifi的知識

首先,要實現一個簡單的WIFI鏈接設置,咱們須要掌握和WIFI功能相關的一些類,好比WIfiManager,WifiInfo,ScanResult,WifiConfiguration等,提供了WIFI鏈接相關的基本的API. 好比:
打開關閉網關:wifiManager.setWifiEnabled(true/false);
掃描周邊網絡:wifiManager.getScanResults();
鏈接指定網絡:wifiManager.enableNetwork(networkId,true);
添加網絡:wifiManager.addNetwork(wcg);
移除網絡:wifiManager.removeNetwork(netId);
獲取網卡狀態:wifiManager.getWifiState()
……
2 掃描的網絡將會被保存在一個List<ScanResult>中,同時WifiManager會爲咱們維護一個List<WifiConfiguration>,這個List中保存了咱們已經鏈接過的配置好的網絡鏈接.
當咱們選擇一個網絡時,判斷它是否存在於這個List中,存在便可直接鏈接,不然須要用戶輸入密碼建立一個新的WifiConfiguration.
3 得到的ScanResult中將會保存有該無線鏈接的相關信息,包括SSID,BSSID,capabilities,level等屬性,其中SSID號是該鏈接的一個標識符,好比咱們常常看到的TP_LINKXXX.
capabilities中保存了相關加密信息,好比WEB和WPA等.level則表示信號度.
4 在獲取鏈接狀態時,即調用wifiManager.getWifiState()或者wifiInfo.getSupplicantState()時,一般在用戶已經受權成功後,咱們得到的狀態值就爲COMPLETED,此時無論網絡是否已經鏈接成功,咱們都沒法得到新的狀態. 因此要判斷WIFI網絡是否已經真的鏈接成功須要用到以下方法判斷:


?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
ConnectivityManager connManager = (ConnectivityManager) WifiConnection.this
            .getSystemService(CONNECTIVITY_SERVICE);
// 獲取表明聯網狀態的NetWorkInfo對象
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
 
if(networkInfo !=null&& networkInfo.getType() ==1
&& wifiAdmin.getWifiInfo().getSSID()!=null)
{
 
 
//WIFI網絡鏈接成功
 
}



5 獲取本地IP地址的方法:

?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
publicString getLocalIpAddress() {     
      try{     
           for(Enumeration<NetworkInterface> en = NetworkInterface     
                  .getNetworkInterfaces(); en.hasMoreElements();) {     
               NetworkInterface intf = en.nextElement();     
               for(Enumeration<InetAddress> enumIpAddr = intf     
                       .getInetAddresses(); enumIpAddr.hasMoreElements();) {     
                  InetAddress inetAddress = enumIpAddr.nextElement();     
                  if(!inetAddress.isLoopbackAddress()) {     
                       returninetAddress.getHostAddress().toString();     
                  }     
               }     
           }     
      }catch(SocketException ex) {     
           Log.e("WifiPreference IpAddress", ex.toString());     
      }     
      returnnull;     
   }     


6 在建立一個新的WifiConfiguration時,切記SSID和preSharedKey必須添加雙引號,不然必將會致使鏈接失敗.正確寫法以下:
?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//建立一個新的WifiConfiguration
WifiConfiguration wcg =newWifiConfiguration();
wcg.BSSID = mBSSID;
//SSID和preSharedKey必須添加雙引號,不然將會致使鏈接失敗
wcg.SSID ="\""+ mSSID +"\"";
wcg.hiddenSSID =false;
wcg.status = WifiConfiguration.Status.ENABLED;
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//若是加密模式爲WEP
if(mSecurity.equals("WEP"))
{
   wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
   wcg.wepKeys[0] ="\""+ editText.getText().toString() +"\"";//This is the WEP Password
   wcg.wepTxKeyIndex =0;
}
//若是加密模式爲WPA EPA
elseif(mSecurity.equals("WPA EAP"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey ="\""+ editText.getText().toString() +"\"";
}
//若是加密模式爲WPA PSK
elseif(mSecurity.equals("WPA PSK"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey ="\""+ editText.getText().toString() +"\"";
}
//無加密
else
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
} 首先,要實現一個簡單的WIFI鏈接設置,咱們須要掌握和WIFI功能相關的一些類,好比WIfiManager,WifiInfo,ScanResult,WifiConfiguration等,提供了WIFI鏈接相關的基本的API. 好比:
打開關閉網關:wifiManager.setWifiEnabled(true/false);
掃描周邊網絡:wifiManager.getScanResults();
鏈接指定網絡:wifiManager.enableNetwork(networkId,true);
添加網絡:wifiManager.addNetwork(wcg);
移除網絡:wifiManager.removeNetwork(netId);
獲取網卡狀態:wifiManager.getWifiState()
……
2 掃描的網絡將會被保存在一個List<ScanResult>中,同時WifiManager會爲咱們維護一個List<WifiConfiguration>,這個List中保存了咱們已經鏈接過的配置好的網絡鏈接.
當咱們選擇一個網絡時,判斷它是否存在於這個List中,存在便可直接鏈接,不然須要用戶輸入密碼建立一個新的WifiConfiguration.
3 得到的ScanResult中將會保存有該無線鏈接的相關信息,包括SSID,BSSID,capabilities,level等屬性,其中SSID號是該鏈接的一個標識符,好比咱們常常看到的TP_LINKXXX.
capabilities中保存了相關加密信息,好比WEB和WPA等.level則表示信號度.
4 在獲取鏈接狀態時,即調用wifiManager.getWifiState()或者wifiInfo.getSupplicantState()時,一般在用戶已經受權成功後,咱們得到的狀態值就爲COMPLETED,此時無論網絡是否已經鏈接成功,咱們都沒法得到新的狀態. 因此要判斷WIFI網絡是否已經真的鏈接成功須要用到以下方法判斷:


?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
ConnectivityManager connManager = (ConnectivityManager) WifiConnection.this
            .getSystemService(CONNECTIVITY_SERVICE);
// 獲取表明聯網狀態的NetWorkInfo對象
NetworkInfo networkInfo = connManager.getActiveNetworkInfo();
 
if(networkInfo !=null&& networkInfo.getType() ==1
&& wifiAdmin.getWifiInfo().getSSID()!=null)
{
 
 
//WIFI網絡鏈接成功
 
}



5 獲取本地IP地址的方法:

?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
publicString getLocalIpAddress() {     
      try{     
           for(Enumeration<NetworkInterface> en = NetworkInterface     
                  .getNetworkInterfaces(); en.hasMoreElements();) {     
               NetworkInterface intf = en.nextElement();     
               for(Enumeration<InetAddress> enumIpAddr = intf     
                       .getInetAddresses(); enumIpAddr.hasMoreElements();) {     
                  InetAddress inetAddress = enumIpAddr.nextElement();     
                  if(!inetAddress.isLoopbackAddress()) {     
                       returninetAddress.getHostAddress().toString();     
                  }     
               }     
           }     
      }catch(SocketException ex) {     
           Log.e("WifiPreference IpAddress", ex.toString());     
      }     
      returnnull;     
   }     


6 在建立一個新的WifiConfiguration時,切記SSID和preSharedKey必須添加雙引號,不然必將會致使鏈接失敗.正確寫法以下:
?
代碼片斷,雙擊複製
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//建立一個新的WifiConfiguration
WifiConfiguration wcg =newWifiConfiguration();
wcg.BSSID = mBSSID;
//SSID和preSharedKey必須添加雙引號,不然將會致使鏈接失敗
wcg.SSID ="\""+ mSSID +"\"";
wcg.hiddenSSID =false;
wcg.status = WifiConfiguration.Status.ENABLED;
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
wcg.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
wcg.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
wcg.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
//若是加密模式爲WEP
if(mSecurity.equals("WEP"))
{
   wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
   wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
   wcg.wepKeys[0] ="\""+ editText.getText().toString() +"\"";//This is the WEP Password
   wcg.wepTxKeyIndex =0;
}
//若是加密模式爲WPA EPA
elseif(mSecurity.equals("WPA EAP"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey ="\""+ editText.getText().toString() +"\"";
}
//若是加密模式爲WPA PSK
elseif(mSecurity.equals("WPA PSK"))
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
wcg.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
wcg.preSharedKey ="\""+ editText.getText().toString() +"\"";
}
//無加密
else
{
wcg.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
}
相關文章
相關標籤/搜索