我想获取使用此解决方案并在此处搜索的以太网卡的MAC(用于产品密钥),问题是当所有网络(以太网和wifi)断开连接时,它返回空的MAC地址。我宁愿获得以太网地址,即使它已断开连接也是如此。
谢谢!!
public static void main(String[] args)
{
InetAddress ip;
try {
ip = InetAddress.getLocalHost();
System.out.println("The mac Address of this machine is :" + ip.getHostAddress());
NetworkInterface network = NetworkInterface.getByInetAddress(ip);
byte[] mac = network.getHardwareAddress();
System.out.print("The mac address is : ");
StringBuilder sb = new StringBuilder();
for (int i = 0; i < mac.length; i++){
sb.append(String.format("%02X%s", mac[i],(i< mac.length - 1)?"-":""));
}
System.out.println(sb.toString());
}
catch (UnknownHostException e) {
e.printStackTrace();
}
catch (SocketException e) {
e.printStackTrace();
}
}
}
Run Code Online (Sandbox Code Playgroud) 我写了以下代码来获取MAC地址:
WifiManager wimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
String address = wimanager.getConnectionInfo().getMacAddress();
Log.d("TOKEN", address);
Run Code Online (Sandbox Code Playgroud)
这在我的手机上完美运行,但在Android模拟器中它返回null.这是因为Android模拟器没有MAC地址吗?
我需要打印机器的所有mac地址.建议的方法是使用NetworkInterface.getNetworkInterfaces()并迭代返回的枚举.但是,当某些设备关闭时(NO Ip已配置),则上述方法将不会返回接口.
这是我为测试目的编写的代码:
Enumeration<NetworkInterface> ni = NetworkInterface.getNetworkInterfaces();
while(ni.hasMoreElements()){
NetworkInterface nextElement = ni.nextElement();
byte[] mac …Run Code Online (Sandbox Code Playgroud)