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

代码片段一:

WifiManager wifiMan = (WifiManager) getSystemService(Context.WIFI_SERVICE);WifiInfo info = wifiMan.getConnectionInfo();String mac = info.getMacAddress();// 获得本机的MAC地址String ssid = info.getSSID();// 获得本机所链接的WIFI名称int ipAddress = info.getIpAddress();String ipString = "";// 本机在WIFI状态下路由分配给的IP地址// 获得IP地址的方法一:if (ipAddress != 0) {       ipString = ((ipAddress & 0xff) + "." + (ipAddress >> 8 & 0xff) + "." + (ipAddress >> 16 & 0xff) + "." + (ipAddress >> 24 & 0xff));}// 获得IP地址的方法二(反射的方法):try {Field field = info.getClass().getDeclaredField("mIpAddress");field.setAccessible(true);ipString = (String) field.get(info);System.out.println("obj" + ipString);} catch (Exception e) {// TODO Auto-generated catch blocke.printStackTrace();}

代码片段二:

 package android.net.wifi; import android.net.NetworkUtils;  public int getIpAddress() {        if (mIpAddress == null || mIpAddress instanceof Inet6Address) return 0;        return NetworkUtils.inetAddressToInt(mIpAddress);    }

代码片段三:

 package android.net; public class NetworkUtils {/***     * Convert a IPv4 address from an InetAddress to an integer     * @param inetAddr is an InetAddress corresponding to the IPv4 address     * @return the IP address as an integer in network byte order     */    public static int inetAddressToInt(InetAddress inetAddr)            throws IllegalArgumentException {        byte [] addr = inetAddr.getAddress();        if (addr.length != 4) {            throw new IllegalArgumentException("Not an IPv4 address");        }        return ((addr[3] & 0xff) << 24) | ((addr[2] & 0xff) << 16) |                ((addr[1] & 0xff) << 8) | (addr[0] & 0xff);        } } 

代码片段四:

package android.net.wifi;

/** * 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; mLinkSpeed = source.mLinkSpeed; mIpAddress = source.mIpAddress; mMacAddress = source.mMacAddress; mMeteredHint = source.mMeteredHint; } }



   

更多相关文章

  1. android tab上显示数字
  2. Android(安卓)在非UI线程直接更新UI信息
  3. 圆形button
  4. Xposed: 勾住(Hook) Android应用程序对象的方法,实现AOP
  5. android如何改变默认横竖屏方向
  6. Android(安卓)五大布局
  7. Android的NDK开发(2)————利用Android(安卓)NDK编写一个简单
  8. Android(安卓)Fragment使用和切换 笔记
  9. android运行python脚本_如何在Android中从Java代码执行Python脚

随机推荐

  1. Android(安卓)源码编译如何确定模块安装
  2. Android(安卓)Dialog风格弹出框的Activit
  3. Image
  4. Android(安卓)支付宝,微信支付
  5. Android(安卓)Dimension
  6. Android(安卓)TV listView焦点平滑移动
  7. Android兼容性优化-Android(安卓)8.0设置
  8. android 文件存储注意点
  9. Android(安卓)Bitmap与String互转
  10. 屏幕方向android:screenOrientation