Android获取WIFI状态下的IP地址以及MAC地址

http://blog.csdn.net/tianshan2008/article/details/8102701

Android获取WIFI下的IP地址以及MAC地址 

代码片段一:

[java]  view plain copy
  1. WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);  
  2. WifiInfo info = wifiMan.getConnectionInfo();  
  3. String mac = info.getMacAddress();// 获得本机的MAC地址  
  4. String ssid = info.getSSID();// 获得本机所链接的WIFI名称  
  5.   
  6. int ipAddress = info.getIpAddress();  
  7. String ipString = "";// 本机在WIFI状态下路由分配给的IP地址  
  8.   
  9. // 获得IP地址的方法一:  
  10. if (ipAddress != 0) {  
  11.        ipString = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "."   
  12.         + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));  
  13. }  
  14. // 获得IP地址的方法二(反射的方法):  
  15. try {  
  16.     Field field = info.getClass().getDeclaredField("mIpAddress");  
  17.     field.setAccessible(true);  
  18.     ipString = (String) field.get(info);  
  19.     System.out.println("obj" + ipString);  
  20. catch (Exception e) {  
  21.     // TODO Auto-generated catch block  
  22.     e.printStackTrace();  
  23. }  

代码片段二:

[java]  view plain copy
  1. package android.net.wifi;  
  2.   
  3. import android.net.NetworkUtils;  
  4.   
  5. public int getIpAddress() {  
  6.        if (mIpAddress == null || mIpAddress instanceof Inet6Address) return 0;  
  7.        return NetworkUtils.inetAddressToInt(mIpAddress);  
  8.    }  

代码片段三:

[java]  view plain copy
  1.  package android.net;  
  2.   
  3.  public class NetworkUtils {  
  4. /*** 
  5.      * Convert a IPv4 address from an InetAddress to an integer 
  6.      * @param inetAddr is an InetAddress corresponding to the IPv4 address 
  7.      * @return the IP address as an integer in network byte order 
  8.      */  
  9.     public static int inetAddressToInt(InetAddress inetAddr)  
  10.             throws IllegalArgumentException {  
  11.         byte [] addr = inetAddr.getAddress();  
  12.         if (addr.length != 4) {  
  13.             throw new IllegalArgumentException("Not an IPv4 address");  
  14.         }  
  15.         return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |  
  16.                 ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);  
  17.   
  18.         }  
  19.   
  20.  }   

代码片段四:

[java]  view plain copy
  1. <pre name="code" class="java">package android.net.wifi;</pre><br>  
  2. /** * Copy constructor * @hide */ public WifiInfo(WifiInfo source) { if (source != null) { mSupplicantState = source.mSupplicantState; mBSSID = source.mBSSID; mSSID = source.mSSID; mNetworkId = source.mNetworkId; mHiddenSSID = source.mHiddenSSID; mRssi = source.mRssi;  
  3.  mLinkSpeed = source.mLinkSpeed; mIpAddress = source.mIpAddress; mMacAddress = source.mMacAddress; mMeteredHint = source.mMeteredHint; } }<p></p>  
  4. <pre></pre>  
  5. <br>  
  6. <p></p>  
  7. <p></p>  
  8. <br>  
  9. <pre name="code" class="java"><pre></pre>  
  10. <pre></pre>  
  11. <pre></pre>  
  12.   
  13. </pre>  

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值