package com.bgycc.smartcanteen.utils; import com.blankj.utilcode.util.LogUtils; import java.net.InetAddress; import java.net.NetworkInterface; import java.util.Enumeration; import static com.bgycc.smartcanteen.utils.SmartCanteenUtils.TAG; public class EthernetHelper { public static String getMacAddress() { try { NetworkInterface networkInterface = NetworkInterface.getByName("eth0"); if (networkInterface == null) { return ""; } byte[] data = networkInterface.getHardwareAddress(); return String.format("%02x:%02x:%02x:%02x:%02x:%02x", data[0], data[1], data[2], data[3], data[4], data[5]); } catch (Exception e) { LogUtils.e(TAG, "获取设备MAC地址失败: " + e.getMessage(), e); } return "02:00:00:00:00:00"; } public static String getIpString() { try { NetworkInterface networkInterface = NetworkInterface.getByName("eth0"); if (networkInterface == null) { return ""; } for (Enumeration<InetAddress> enumIpAddr = networkInterface.getInetAddresses(); enumIpAddr.hasMoreElements();) { InetAddress inetAddress = enumIpAddr.nextElement(); if (!inetAddress.isLoopbackAddress()) { String address = inetAddress.getHostAddress(); if(!address.contains("::")) return address; } } } catch (Exception e) { LogUtils.e(TAG, "获取以太网ip地址失败: " + e.getMessage(), e); } return "0.0.0.0"; } }