Several methods of obtaining Mac address by Android mobile phone

  • 2021-11-02 02:29:18
  • OfStack

The most commonly used method is obtained through WiFiManager:


/**
 *  Pass WiFiManager Get mac Address 
 * @param context
 * @return
 */
 private static String tryGetWifiMac(Context context) {
 WifiManager wm = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
 WifiInfo wi = wm.getConnectionInfo();
 if (wi == null || wi.getMacAddress() == null) {
  return null;
 }
 if ("02:00:00:00:00:00".equals(wi.getMacAddress().trim())) {
  return null;
 } else {
  return wi.getMacAddress().trim();
 }
 }

This method Android 7.0 can't be obtained, but returns null, which actually returns "02:00: 00:00: 00:00"

Obtain from local IP:


/**
 *  According to IP Address acquisition MAC Address 
 * @return
 */
 private static String getLocalMacAddressFromIp() {
 String strMacAddr = null;
 try {
  // Obtain IpD Address 
  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;
 }
/**
 *  Get mobile device local IP
 * @return
 */
 private static InetAddress getLocalInetAddress() {
 InetAddress ip = null;
 try {
  // Enumerate 
  Enumeration<NetworkInterface> en_netInterface = NetworkInterface.getNetworkInterfaces();
  while (en_netInterface.hasMoreElements()) {// Are there any more elements 
  NetworkInterface ni = (NetworkInterface) en_netInterface.nextElement();// Get the following 1 Elements 
  Enumeration<InetAddress> en_ip = ni.getInetAddresses();// Get 1 A ip Enumeration of addresses 
  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;
 }

This method is available in Android 7.0 and below.

Obtain from the network interface:


 /**
 *  Get through the network interface 
 * @return
 */
 private static String getNewMac() {
 try {
  List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
  for (NetworkInterface nif : all) {
  if (!nif.getName().equalsIgnoreCase("wlan0")) 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;
 }

Note that Name of network interface has many similarities: dummy0, p2p0, wlan0... where wlan0 is the address we need WiFi mac. This method is available in Android 7.0 and below.

Summarize


Related articles: