|
|
@@ -8,6 +8,9 @@ import com.xzjmyk.pm.activity.ui.erp.util.CommonUtil;
|
|
|
import com.xzjmyk.pm.activity.ui.erp.util.StringUtils;
|
|
|
|
|
|
import java.io.FileInputStream;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.net.SocketException;
|
|
|
+import java.util.Enumeration;
|
|
|
|
|
|
/**
|
|
|
* Created by pengminggong on 2016/10/31.
|
|
|
@@ -18,12 +21,13 @@ public class MacAndIDUtil {
|
|
|
public String getMac(Context ct) {
|
|
|
String mac = null;
|
|
|
String macOld = CommonUtil.getSharedPreferences(ct, "macaddress");
|
|
|
- if (StringUtils.isEmpty(macOld)) {
|
|
|
+ if (macisEmpty(macOld)) {
|
|
|
mac = getLocalMacAddress();
|
|
|
- if (StringUtils.isEmpty(mac))
|
|
|
+ if (macisEmpty(mac))
|
|
|
mac = getMacFromWifiInfo(ct);
|
|
|
- boolean isOk = legitimateMac(mac);//判断mac地址是否合法
|
|
|
- if (isOk) {
|
|
|
+ if (macisEmpty(mac))
|
|
|
+ mac = getLocalEthernetMacAddress();
|
|
|
+ if (!macisEmpty(mac)) {
|
|
|
CommonUtil.setSharedPreferences(ct, "macaddress", mac);
|
|
|
} else {
|
|
|
tryOpenWifi(ct);
|
|
|
@@ -32,13 +36,12 @@ public class MacAndIDUtil {
|
|
|
} else {
|
|
|
mac = macOld;
|
|
|
}
|
|
|
-
|
|
|
return mac;
|
|
|
}
|
|
|
|
|
|
|
|
|
- private boolean legitimateMac(String mac) {
|
|
|
- return !StringUtils.isEmpty(mac) && !mac.contains("00:00:00");
|
|
|
+ private boolean macisEmpty(String mac) {
|
|
|
+ return StringUtils.isEmpty(mac) || mac.contains("00:00:00");
|
|
|
}
|
|
|
|
|
|
/*通过wifiInfo获取ip*/
|
|
|
@@ -131,4 +134,65 @@ public class MacAndIDUtil {
|
|
|
}
|
|
|
return softOpenWifi;
|
|
|
}
|
|
|
+
|
|
|
+
|
|
|
+ public static String getLocalEthernetMacAddress() {
|
|
|
+ String mac = null;
|
|
|
+ try {
|
|
|
+ Enumeration localEnumeration = NetworkInterface
|
|
|
+ .getNetworkInterfaces();
|
|
|
+
|
|
|
+ while (localEnumeration.hasMoreElements()) {
|
|
|
+ NetworkInterface localNetworkInterface = (NetworkInterface) localEnumeration
|
|
|
+ .nextElement();
|
|
|
+ String interfaceName = localNetworkInterface.getDisplayName();
|
|
|
+
|
|
|
+ if (interfaceName == null) {
|
|
|
+ continue;
|
|
|
+ }
|
|
|
+
|
|
|
+ if (interfaceName.equals("eth0")) {
|
|
|
+ mac = convertToMac(localNetworkInterface
|
|
|
+ .getHardwareAddress());
|
|
|
+ if (mac != null && mac.startsWith("0:")) {
|
|
|
+ mac = "0" + mac;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ if (interfaceName.equals("wlan0")) {
|
|
|
+ mac = convertToMac(localNetworkInterface
|
|
|
+ .getHardwareAddress());
|
|
|
+ if (mac != null && mac.startsWith("0:")) {
|
|
|
+ mac = "0" + mac;
|
|
|
+ }
|
|
|
+ break;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (SocketException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return mac;
|
|
|
+ }
|
|
|
+
|
|
|
+ private static String convertToMac(byte[] mac) {
|
|
|
+ StringBuilder sb = new StringBuilder();
|
|
|
+ for (int i = 0; i < mac.length; i++) {
|
|
|
+ byte b = mac[i];
|
|
|
+ int value = 0;
|
|
|
+ if (b >= 0 && b <= 16) {
|
|
|
+ value = b;
|
|
|
+ sb.append("0" + Integer.toHexString(value));
|
|
|
+ } else if (b > 16) {
|
|
|
+ value = b;
|
|
|
+ sb.append(Integer.toHexString(value));
|
|
|
+ } else {
|
|
|
+ value = 256 + b;
|
|
|
+ sb.append(Integer.toHexString(value));
|
|
|
+ }
|
|
|
+ if (i != mac.length - 1) {
|
|
|
+ sb.append(":");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return sb.toString();
|
|
|
+ }
|
|
|
}
|