How to get the android mac address programmatically when the wifi is off

Answer 1: Force open the wifi

Check this link on stackoverflow for details

Answer 2: Get the mac address by network interface

The below code will help get all mack addresses of the device.


private static String getMacAddress(String interfaceName) { try { Enumeration<NetworkInterface> interfaces = NetworkInterface.getNetworkInterfaces(); while (interfaces.hasMoreElements()) { NetworkInterface networkInterface = interfaces.nextElement(); if (TextUtils.equals(networkInterface.getName(), interfaceName)) { byte[] bytes = networkInterface.getHardwareAddress(); StringBuilder builder = new StringBuilder(); for (byte b : bytes) { builder.append(String.format("%02X:", b)); } if (builder.length() > 0) { builder.deleteCharAt(builder.length() - 1); } return builder.toString(); } } return "<EMPTY>"; } catch (SocketException e) { Log.e(Constants.TAG, "Get Mac Address Error", e); return "<ERROR>"; } }

Use wlan0 if you want to get the WIFI MAC address, and eth0 if you want to get the ethernet MAC address.


public static String getEthernetMacAddress() { return getMacAddress("eth0"); } public static String getWifiMacAddress() { return getMacAddress("wlan0"); }