Search code examples
androidandroid-6.0-marshmallowandroid-wifi

How to get Wifi Mac address or something else which is unique in Android 6.0


Hello Supporters i am working on my app which gets WiFi Mac address from user.This working file until Android 6.0 is come into market. Now i get Mac like 02:00:00 and so on.. I am also using below method to get Wifi mac but it returns same for all devices. I want to register in my app on basis of mac. Now what should i take from android devices that is unique from one another is it Ip address or something else please suggest. Thank you.

public static String getMacAddr() {
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 "";
        }

        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) {
}
return "02:00:00:00:00:00";}

Solution

  • I solve the problem the above thanks to Daniel is correct:

    public static String getMacAddr() {
    try {
    List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
    for (NetworkInterface nif : all) {
        if (!nif.getName().equalsIgnoreCase("wlan0")) continue;  //instead of wlan0 i used eth0
        byte[] macBytes = nif.getHardwareAddress();
        if (macBytes == null) {
            return "";
        }
        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) {
    }`return "02:00:00:00:00:00";}
    

    The issue is in Android Media Box(TV's) we should use "eth0" instead of "wlan0" and in mobile devices above 6 we should use "wlan0". Thank you.