|
@@ -1,7 +1,9 @@
|
|
|
package com.wdkl.app.ncs.callingbed.helper;
|
|
|
|
|
|
+import android.annotation.SuppressLint;
|
|
|
import android.content.Context;
|
|
|
import android.net.ConnectivityManager;
|
|
|
+import android.net.wifi.WifiInfo;
|
|
|
import android.net.wifi.WifiManager;
|
|
|
import android.text.TextUtils;
|
|
|
|
|
@@ -12,6 +14,13 @@ import org.greenrobot.eventbus.EventBus;
|
|
|
import java.io.BufferedReader;
|
|
|
import java.io.IOException;
|
|
|
import java.io.InputStreamReader;
|
|
|
+import java.net.Inet4Address;
|
|
|
+import java.net.InetAddress;
|
|
|
+import java.net.NetworkInterface;
|
|
|
+import java.net.SocketException;
|
|
|
+import java.util.Collections;
|
|
|
+import java.util.Enumeration;
|
|
|
+import java.util.List;
|
|
|
import java.util.Timer;
|
|
|
import java.util.TimerTask;
|
|
|
|
|
@@ -311,4 +320,149 @@ public class NetHelper {
|
|
|
|
|
|
return -1;
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 得到MAC
|
|
|
+ *
|
|
|
+ * @return String
|
|
|
+ */
|
|
|
+ public String getMacAddress() {
|
|
|
+ String mac = "";
|
|
|
+ try {
|
|
|
+ mac = getLocalMacAddressFromIp();
|
|
|
+ if (TextUtils.isEmpty(mac)) {
|
|
|
+ mac = getNetworkMac();
|
|
|
+ }
|
|
|
+ if (TextUtils.isEmpty(mac)) {
|
|
|
+ mac = tryGetWifiMac();
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ }
|
|
|
+ return mac;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String tryGetWifiMac() {
|
|
|
+ String mac;
|
|
|
+ WifiManager wm = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
|
|
|
+ WifiInfo wi = wm.getConnectionInfo();
|
|
|
+ if (wi == null || wi.getMacAddress() == null) {
|
|
|
+ mac = null;
|
|
|
+ }
|
|
|
+ if ("02:00:00:00:00:00".equals(wi.getMacAddress().trim())) {
|
|
|
+ mac = null;
|
|
|
+ } else {
|
|
|
+ mac = wi.getMacAddress().trim();
|
|
|
+ }
|
|
|
+ return mac;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过网络接口获取
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static String getNetworkMac() {
|
|
|
+ try {
|
|
|
+ List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
|
|
|
+ for (NetworkInterface nif : all) {
|
|
|
+ // if (!nif.getName().equalsIgnoreCase("wlan0") && !nif.getName().equalsIgnoreCase("eth0") && !nif.getName().equalsIgnoreCase("eth1"))
|
|
|
+ if (!nif.getName().equalsIgnoreCase("eth0") && !nif.getName().equalsIgnoreCase("eth1") && !nif.getName().equalsIgnoreCase("eth2"))
|
|
|
+ continue;
|
|
|
+ byte[] macBytes = nif.getHardwareAddress();
|
|
|
+ if (macBytes == null) {
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+ StringBuilder res1 = new StringBuilder();
|
|
|
+ for (byte b : macBytes) {
|
|
|
+ res1.append(String.format("%02X:", b));
|
|
|
+ }
|
|
|
+ if (res1.length() > 0) {
|
|
|
+ res1.deleteCharAt(res1.length() - 1);
|
|
|
+ }
|
|
|
+ return res1.toString();
|
|
|
+ }
|
|
|
+ } catch (Exception ex) {
|
|
|
+ ex.printStackTrace();
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 根据IP地址获取MAC地址
|
|
|
+ *
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @SuppressLint("NewApi")
|
|
|
+ public String getLocalMacAddressFromIp() {
|
|
|
+ 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ public InetAddress getLocalInetAddress() {
|
|
|
+ InetAddress ip = null;
|
|
|
+ try {
|
|
|
+ // 列举
|
|
|
+ Enumeration<NetworkInterface> en_netInterface = NetworkInterface
|
|
|
+ .getNetworkInterfaces();
|
|
|
+ while (en_netInterface.hasMoreElements()) {// 是否还有元素
|
|
|
+ NetworkInterface ni = en_netInterface
|
|
|
+ .nextElement();// 得到下一个元素
|
|
|
+ Enumeration<InetAddress> 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;
|
|
|
+ }
|
|
|
+
|
|
|
+ public String getLocalIP() {
|
|
|
+ try {
|
|
|
+ for (Enumeration<NetworkInterface> enNetI = NetworkInterface.getNetworkInterfaces(); enNetI.hasMoreElements(); ) {
|
|
|
+ NetworkInterface netI = enNetI.nextElement();
|
|
|
+ for (Enumeration<InetAddress> enumIpAddr = netI.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
|
|
|
+ InetAddress inetAddress = enumIpAddr.nextElement();
|
|
|
+ if (inetAddress instanceof Inet4Address && !inetAddress.isLoopbackAddress()) {
|
|
|
+ return inetAddress.getHostAddress();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ return "";
|
|
|
+ }
|
|
|
}
|