Android获取手机及路由器的Mac地址和IP地址

Android获取Mac地址及IP地址需要添加权限,

在AndroidManifest.xml中:

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

1、获取手机的Mac地址,在Wifi未开启或者未连接的情况下也能获取手机Mac地址,代码如下:

public static String getMacAddress(Context context) {
    String macAddress = null;
    WifiInfo wifiInfo = getWifiInfo(context);
    if (wifiInfo != null) {
        macAddress = wifiInfo.getMacAddress();
    }
    return macAddress;
}

2、获取手机IP地址,只有在Wifi连接的情况下才能获取IP地址,否则为0,对应转换IP为0.0.0.0,其中getIpAddress()获取的IP地址是整型,需要转换为IP地址。代码如下:

public static String getIpAddress(Context context) {
    String IpAddress = null;
    WifiInfo wifiInfo = getWifiInfo(context);
    if (wifiInfo != null) {
        IpAddress = intToIpAddress(wifiInfo.getIpAddress());
    }
    return IpAddress;
}

public static String intToIpAddress(long ipInt) {
    StringBuffer sb = new StringBuffer();
    sb.append(ipInt & 0xFF).append(".");
    sb.append((ipInt >> 8) & 0xFF).append(".");
    sb.append((ipInt >> 16) & 0xFF).append(".");
    sb.append((ipInt >> 24) & 0xFF);
    return sb.toString();
}

3、获取路由器的Mac地址,只有连接成功才能获取(废话…),代码如下:

public static String getConnectedWifiMacAddress(Context context) {
    String connectedWifiMacAddress = null;
    WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
    List wifiList;

    if (wifiManager != null) {
        wifiList = wifiManager.getScanResults();
        WifiInfo info = wifiManager.getConnectionInfo();
        if (wifiList != null && info != null) {
            for (int i = 0; i < wifiList.size(); i++) {
                ScanResult result = wifiList.get(i);
                if (info.getBSSID().equals(result.BSSID)) {
                    connectedWifiMacAddress = result.BSSID;
                }
            }
        }
    }
    return connectedWifiMacAddress;
}

完整的获取Mac及IP地址工具类代码如下:

package com.cpxiao.wifimacdemo;

import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

import java.util.List;

/**
 * Created by cpxiao on 15/9/21.
 */
public class MacAddressUtils {

    /**
     * 获取手机的Mac地址,在Wifi未开启或者未连接的情况下也能获取手机Mac地址
     */
    public static String getMacAddress(Context context) {
        String macAddress = null;
        WifiInfo wifiInfo = getWifiInfo(context);
        if (wifiInfo != null) {
            macAddress = wifiInfo.getMacAddress();
        }
        return macAddress;
    }

    /**
     * 获取手机的Ip地址
     */
    public static String getIpAddress(Context context) {
        String IpAddress = null;
        WifiInfo wifiInfo = getWifiInfo(context);
        if (wifiInfo != null) {
            IpAddress = intToIpAddress(wifiInfo.getIpAddress());
        }
        return IpAddress;
    }

    /**
     * 获取WifiInfo
     */
    public static WifiInfo getWifiInfo(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        WifiInfo info = null;
        if (null != wifiManager) {
            info = wifiManager.getConnectionInfo();
        }
        return info;
    }


    public static long ipAddressToint(String ip) {
        String[] items = ip.split("\\.");
        return Long.valueOf(items[0]) << 24
                | Long.valueOf(items[1]) << 16
                | Long.valueOf(items[2]) << 8
                | Long.valueOf(items[3]);
    }

    public static String intToIpAddress(long ipInt) {
        StringBuffer sb = new StringBuffer();
        sb.append(ipInt & 0xFF).append(".");
        sb.append((ipInt >> 8) & 0xFF).append(".");
        sb.append((ipInt >> 16) & 0xFF).append(".");
        sb.append((ipInt >> 24) & 0xFF);
        return sb.toString();
    }

    /**
     * 获取当前可连接Wifi列表
     */
    public static List getAvailableNetworks(Context context) {
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List wifiList = null;
        if (wifiManager != null) {
            wifiList = wifiManager.getScanResults();
        }
        return wifiList;
    }

    /**
     * 获取已连接的Wifi路由器的Mac地址
     */
    public static String getConnectedWifiMacAddress(Context context) {
        String connectedWifiMacAddress = null;
        WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
        List wifiList;

        if (wifiManager != null) {
            wifiList = wifiManager.getScanResults();
            WifiInfo info = wifiManager.getConnectionInfo();
            if (wifiList != null && info != null) {
                for (int i = 0; i < wifiList.size(); i++) {
                    ScanResult result = wifiList.get(i);
                    if (info.getBSSID().equals(result.BSSID)) {
                        connectedWifiMacAddress = result.BSSID;
                    }
                }
            }
        }
        return connectedWifiMacAddress;
    }
}

参考:
http://panxq0809.iteye.com/blog/666663
http://orgcent.com/android-wifi-mac-ip-address/
http://orgcent.com/ip-address-integer-convert/

你可能感兴趣的:(android,路由器,手机,IP地址,Mac地址)