|
@@ -15,6 +15,7 @@ import java.io.FileReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.InputStreamReader;
|
|
import java.io.LineNumberReader;
|
|
import java.io.LineNumberReader;
|
|
import java.io.Reader;
|
|
import java.io.Reader;
|
|
|
|
+import java.net.Inet4Address;
|
|
import java.net.InetAddress;
|
|
import java.net.InetAddress;
|
|
import java.net.NetworkInterface;
|
|
import java.net.NetworkInterface;
|
|
import java.net.SocketException;
|
|
import java.net.SocketException;
|
|
@@ -23,35 +24,55 @@ import java.util.Enumeration;
|
|
public final class MacUtil {
|
|
public final class MacUtil {
|
|
|
|
|
|
public static String getMac(Context context) {
|
|
public static String getMac(Context context) {
|
|
- String strMac;
|
|
|
|
-
|
|
|
|
- if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
|
|
|
|
- Log.d("=====", "6.0以下");
|
|
|
|
- strMac = getLocalMacAddressFromWifiInfo(context);
|
|
|
|
- } else if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
|
|
|
|
- Log.d("=====", "6.0以上7.0以下");
|
|
|
|
- strMac = getMacAddress(context);
|
|
|
|
- } else {
|
|
|
|
- Log.d("=====", "7.0以上");
|
|
|
|
- if (!TextUtils.isEmpty(getMacAddress())) {
|
|
|
|
- Log.d("=====", "7.0以上1");
|
|
|
|
- strMac = getMacAddress();
|
|
|
|
- } else if (!TextUtils.isEmpty(getMachineHardwareAddress())) {
|
|
|
|
- Log.d("=====", "7.0以上2");
|
|
|
|
- strMac = getMachineHardwareAddress();
|
|
|
|
- } else {
|
|
|
|
- Log.d("=====", "7.0以上3");
|
|
|
|
- strMac = getLocalMacAddressFromBusybox();
|
|
|
|
|
|
+ byte[] mac = null;
|
|
|
|
+ StringBuffer sb = new StringBuffer();
|
|
|
|
+ try
|
|
|
|
+ {
|
|
|
|
+ Enumeration<NetworkInterface> netInterfaces = NetworkInterface.getNetworkInterfaces();
|
|
|
|
+ while (netInterfaces.hasMoreElements())
|
|
|
|
+ {
|
|
|
|
+ NetworkInterface ni = netInterfaces.nextElement();
|
|
|
|
+ Enumeration<InetAddress> address = ni.getInetAddresses();
|
|
|
|
+ while (address.hasMoreElements())
|
|
|
|
+ {
|
|
|
|
+ InetAddress ip = address.nextElement();
|
|
|
|
+ if (ip.isAnyLocalAddress() || !(ip instanceof Inet4Address)
|
|
|
|
+ || ip.isLoopbackAddress())
|
|
|
|
+ continue;
|
|
|
|
+ if (ip.isSiteLocalAddress())
|
|
|
|
+ mac = ni.getHardwareAddress();
|
|
|
|
+ else if (!ip.isLinkLocalAddress())
|
|
|
|
+ {
|
|
|
|
+ mac = ni.getHardwareAddress();
|
|
|
|
+ break;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- if (TextUtils.isEmpty(strMac)) {
|
|
|
|
- strMac = "02:00:00:00:00:00";
|
|
|
|
|
|
+ catch (SocketException e)
|
|
|
|
+ {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ if (mac != null)
|
|
|
|
+ {
|
|
|
|
+ for (int i = 0; i < mac.length; i++)
|
|
|
|
+ {
|
|
|
|
+ sb.append(parseByte(mac[i]));
|
|
|
|
+ }
|
|
|
|
+ return sb.substring(0, sb.length() - 1).replace(":", "");
|
|
|
|
+ }
|
|
|
|
+ else
|
|
|
|
+ {
|
|
|
|
+ return "000000FFFFFF";
|
|
}
|
|
}
|
|
|
|
|
|
- return strMac;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+ private static String parseByte(byte b)
|
|
|
|
+ {
|
|
|
|
+ String s = "00" + Integer.toHexString(b) + ":";
|
|
|
|
+ return s.substring(s.length() - 3);
|
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
/**
|
|
* 根据wifi信息获取本地mac
|
|
* 根据wifi信息获取本地mac
|