The Method of Obtaining Mac Address from Android Mobile Phone

Keywords: Mobile Mac Android network shell

In this period of requirement, it is required to obtain a unique code from the system device as the unique identification of the current logged-in user, and finally decided to adopt the mac address.

The first is:

The official method of obtaining mac address is:

/**
     * Getting mac address through WiFiManager
     * @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 is not available, the return is null, in fact, return "02:00:00:00:00:00"

The second method:

Get it by shell command:

**
     * This is used. adb shell Command to get mac Way of Address
     * @return
     */
    public static String getMac() {
        String macSerial = null;
        String str = "";
 
        try {
            Process pp = Runtime.getRuntime().exec("cat /sys/class/net/wlan0/address ");
            InputStreamReader ir = new InputStreamReader(pp.getInputStream());
            LineNumberReader input = new LineNumberReader(ir);
 
            for (; null != str; ) {
                str = input.readLine();
                if (str != null) {
                    macSerial = str.trim();// Blanking
                    break;
                }
            }
        } catch (IOException ex) {
            // Give default values
            ex.printStackTrace();
        }
        return macSerial;
    }

This way, Android version 7.0 or more is not available.

The third method is:

Acquisition according to network interface:

 /**
     * Access through 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 there are many Name s for network interfaces: dummy 0, p2p0, wlan0... Wlan0 is where we need WiFi mac addresses. This method is available in Android 7.0 and above.

Posted by colby.anderson on Sun, 27 Jan 2019 23:03:14 -0800