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);
}
相关文章
相关标签/搜索