android 获取mac地址(复制即用)

*

*根据wifi信息获取本地mac

* @param context

* @return

public static StringgetLocalMacAddressFromWifiInfo(Context context) {

WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);

    WifiInfo winfo = wifi.getConnectionInfo();

    String mac = winfo.getMacAddress();

    return mac;

}

public static StringgetMac(Context context) {

String strMac =null;

    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {

Log.e("=====", "6.0以下");

        Toast.makeText(context, "6.0以下", Toast.LENGTH_SHORT).show();

        strMac =getLocalMacAddressFromWifiInfo(context);

        return strMac;

    }else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N

            && Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

Log.e("=====", "6.0以上7.0以下");

        Toast.makeText(context, "6.0以上7.0以下", Toast.LENGTH_SHORT).show();

        strMac =getMacAddress();

        return strMac;

    }else

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {

Log.e("=====", "7.0以上");

        if (!TextUtils.isEmpty(getMacAddress())) {

Log.e("=====", "7.0以上1");

            Toast.makeText(context, "7.0以上1", Toast.LENGTH_SHORT).show();

            strMac =getMacAddress();

            return strMac;

        }else if (!TextUtils.isEmpty(getMachineHardwareAddress())) {

Log.e("=====", "7.0以上2");

            Toast.makeText(context, "7.0以上2", Toast.LENGTH_SHORT).show();

            strMac =getMachineHardwareAddress();

            return strMac;

        }else {

Log.e("=====", "7.0以上3");

            Toast.makeText(context, "7.0以上3", Toast.LENGTH_SHORT).show();

            strMac =getLocalMacAddressFromBusybox();

            return strMac;

        }

}

return "02:00:00:00:00:00";

}

*

*根据IP地址获取MAC地址

*

* @return

public static StringgetMacAddress() {

String strMacAddr =null;

    try {

// 获得IpD地址

        InetAddress ip =getLocalInetAddress();

        byte[] b = NetworkInterface.getByInetAddress(ip)

.getHardwareAddress();

        StringBuffer buffer =new StringBuffer();

        for (int i =0; i < b.length; i++) {

if (i !=0) {

buffer.append(':');

            }

String str = Integer.toHexString(b[i] &0xFF);

            buffer.append(str.length() ==1 ?0 + str : str);

        }

strMacAddr = buffer.toString().toUpperCase();

    }catch (Exception e) {

}

return strMacAddr;

}

*

*获取移动设备本地IP

*

* @return

private static InetAddressgetLocalInetAddress() {

InetAddress ip =null;

    try {

// 列举

        Enumeration en_netInterface = NetworkInterface

.getNetworkInterfaces();

        while (en_netInterface.hasMoreElements()) {// 是否还有元素

            NetworkInterface ni = (NetworkInterface) en_netInterface

.nextElement();// 得到下一个元素

            Enumeration en_ip = ni.getInetAddresses();// 得到一个ip地址的列举

            while (en_ip.hasMoreElements()) {

ip = en_ip.nextElement();

                if (!ip.isLoopbackAddress()

&& ip.getHostAddress().indexOf(":") == -1)

break;

else

                    ip =null;

            }

if (ip !=null) {

break;

            }

}

}catch (SocketException e) {

e.printStackTrace();

    }

return ip;

}

*

*获取本地IP

*

* @return

private static StringgetLocalIpAddress() {

try {

for (Enumeration en = NetworkInterface

.getNetworkInterfaces(); en.hasMoreElements(); ) {

NetworkInterface intf = en.nextElement();

            for (Enumeration enumIpAddr = intf

.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {

InetAddress inetAddress = enumIpAddr.nextElement();

                if (!inetAddress.isLoopbackAddress()) {

return inetAddress.getHostAddress().toString();

                }

}

}

}catch (SocketException ex) {

ex.printStackTrace();

    }

return null;

}

*

* android7.0及以上 (2)扫描各个网络接口获取mac地址

*

*

*获取设备HardwareAddress地址

*

* @return

public static StringgetMachineHardwareAddress() {

Enumeration interfaces =null;

    try {

interfaces = NetworkInterface.getNetworkInterfaces();

    }catch (SocketException e) {

e.printStackTrace();

    }

String hardWareAddress =null;

    NetworkInterface iF =null;

    if (interfaces ==null) {

return null;

    }

while (interfaces.hasMoreElements()) {

iF = interfaces.nextElement();

        try {

hardWareAddress =bytesToString(iF.getHardwareAddress());

            if (hardWareAddress !=null)

break;

        }catch (SocketException e) {

e.printStackTrace();

        }

}

return hardWareAddress;

}

**

* byte转为String

*

* @param bytes

* @return

private static StringbytesToString(byte[] bytes) {

if (bytes ==null || bytes.length ==0) {

return null;

    }

StringBuilder buf =new StringBuilder();

    for (byte b : bytes) {

buf.append(String.format("%02X:", b));

    }

if (buf.length() >0) {

buf.deleteCharAt(buf.length() -1);

    }

return buf.toString();

}

*

* android7.0及以上 (3)通过busybox获取本地存储的mac地址

*

*

*根据busybox获取本地Mac

*

* @return

public static StringgetLocalMacAddressFromBusybox() {

String result ="";

    String Mac ="";

    result =callCmd("busybox ifconfig", "HWaddr");

    // 如果返回的result == null,则说明网络不可取

    if (result ==null) {

return "网络异常";

    }

// 对该行数据进行解析

    // 例如:eth0 Link encap:Ethernet HWaddr 00:16:E8:3E:DF:67

    if (result.length() >0 && result.contains("HWaddr") ==true) {

Mac = result.substring(result.indexOf("HWaddr") +6,

                result.length() -1);

        result = Mac;

    }

return result;

}

private static StringcallCmd(String cmd, String filter) {

String result ="";

    String line ="";

    try {

Process proc = Runtime.getRuntime().exec(cmd);

        InputStreamReader is =new InputStreamReader(proc.getInputStream());

        BufferedReader br =new BufferedReader(is);

        while ((line = br.readLine()) !=null

                && line.contains(filter) ==false) {

result += line;

        }

result = line;

    }catch (Exception e) {

e.printStackTrace();

    }

return result;

}

推荐阅读更多精彩内容