1、测试版本:Android 10
2、添加权限
3、获取方法
public static String getMacAddress() { try { // 把当前机器上访问网络的接口存入 List集合中 Listall = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) { if (!"wlan0".equalsIgnoreCase(nif.getName())) { continue; } byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null || macBytes.length == 0) { continue; } StringBuilder result = new StringBuilder(); for (byte b : macBytes) { //每隔两个字符加一个: result.append(String.format("%02X:", b)); } if (result.length() > 0) { //删除最后一个: result.deleteCharAt(result.length() - 1); } return result.toString(); } } catch (Exception e) { e.printStackTrace(); } return "Unknown"; }
或者是
public static String macAddress() throws SocketException { String address = null; // 把当前机器上访问网络的接口存入 Enumeration集合中 Enumerationinterfaces = NetworkInterface.getNetworkInterfaces(); Log.d("TEST_BUG", " interfaceName = " + interfaces ); while (interfaces.hasMoreElements()) { NetworkInterface netWork = interfaces.nextElement(); // 如果存在硬件地址并可以使用给定的当前权限访问,则返回该硬件地址(通常是 MAC)。 byte[] by = netWork.getHardwareAddress(); if (by == null || by.length == 0) { continue; } StringBuilder builder = new StringBuilder(); for (byte b : by) { builder.append(String.format("%02X:", b)); } if (builder.length() > 0) { builder.deleteCharAt(builder.length() - 1); } String mac = builder.toString(); Log.d("TEST_BUG", " interfaceName ="+netWork.getName()+", mac="+mac); // 从路由器上在线设备的MAC地址列表,可以印证设备Wifi的 name 是 wlan0 if (netWork.getName().equals("wlan0")) { address = mac; Log.d("TEST_BUG", " interfaceName ="+netWork.getName()+", address="+address); } } return address; }
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)