Getting MAC address in Android 6.0

后端 未结 10 2225
醉梦人生
醉梦人生 2020-11-27 04:48

I\'m developing an app that gets the MAC address of the device, but since Android 6.0 my code doesn\'t work, giving me an incorrect value.

Here\'s my code...

相关标签:
10条回答
  • 2020-11-27 05:19

    Please refer to Android 6.0 Changes.

    To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

    To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.

    0 讨论(0)
  • 2020-11-27 05:19

    I didn't get the above answer to work, but stumbled upon another answer.

    Here is a complete and simple method on getting the IPv6 address and then getting the mac address from it.

    How to get Wi-Fi Mac address in Android Marshmallow

    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";
    }
    

    Tested it already and it works. Many thanks to Rob Anderson!

    0 讨论(0)
  • 2020-11-27 05:22

    The answers are mostly correct, but keep care, that there is a change in android 7. You will need to use the

    DevicePolicyManager and the Method getWifiMacAddress. The official docs has a typo, which means that you shouldnt copy/paste it from there.

    DevicePolicyManager.getWifiMacAddress()
    

    Refs: https://developer.android.com/about/versions/nougat/android-7.0-changes.html

    Get Device mac adress in Android Nougat and O programmatically

    0 讨论(0)
  • 2020-11-27 05:24

    this is the complete 2 ways code of getting it successfully on Marshmallow , just copy past this and it will work !

    //Android 6.0 : Access to mac address from WifiManager forbidden
        private static final String marshmallowMacAddress = "02:00:00:00:00:00";
        private static final String fileAddressMac = "/sys/class/net/wlan0/address";    
    
    public static String recupAdresseMAC(WifiManager wifiMan) {
            WifiInfo wifiInf = wifiMan.getConnectionInfo();
    
            if(wifiInf.getMacAddress().equals(marshmallowMacAddress)){
                String ret = null;
                try {
                    ret= getAdressMacByInterface();
                    if (ret != null){
                        return ret;
                    } else {
                        ret = getAddressMacByFile(wifiMan);
                        return ret;
                    }
                } catch (IOException e) {
                    Log.e("MobileAccess", "Erreur lecture propriete Adresse MAC");
                } catch (Exception e) {
                    Log.e("MobileAcces", "Erreur lecture propriete Adresse MAC ");
                }
            } else{
                return wifiInf.getMacAddress();
            }
            return marshmallowMacAddress;
        }
    
    private static String getAdressMacByInterface(){
            try {
                List<NetworkInterface> all = Collections.list(NetworkInterface.getNetworkInterfaces());
                for (NetworkInterface nif : all) {
                    if (nif.getName().equalsIgnoreCase("wlan0")) {
                        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 e) {
                Log.e("MobileAcces", "Erreur lecture propriete Adresse MAC ");
            }
            return null;
        }
    
    private static String getAddressMacByFile(WifiManager wifiMan) throws Exception {
            String ret;
            int wifiState = wifiMan.getWifiState();
    
            wifiMan.setWifiEnabled(true);
            File fl = new File(fileAddressMac);
            FileInputStream fin = new FileInputStream(fl);
            StringBuilder builder = new StringBuilder();
        int ch;
        while((ch = fin.read()) != -1){
            builder.append((char)ch);
        }
    
        ret = builder.toString();
        fin.close();
    
            boolean enabled = WifiManager.WIFI_STATE_ENABLED == wifiState;
            wifiMan.setWifiEnabled(enabled);
            return ret;
        }
    

    manifest :

    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
        <uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
    
    • Summary : this code will try to get the MAC address first by Interface and if its failed it get it by file system.

    • Note:for file System way, you need to enable WIFI to access the file.

    thnx to Sam's answer here https://stackoverflow.com/a/39288868/3818437

    0 讨论(0)
  • 2020-11-27 05:29

    First you need to add Internet user permission.

    <uses-permission android:name="android.permission.INTERNET" />
    

    Then you can find the mac over the NetworkInterfaces API.

    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";
    }
    
    0 讨论(0)
  • 2020-11-27 05:35

    Use below code to get Mac address in Android 6.0

    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(Integer.toHexString(b & 0xFF) + ":");
                }
    
                if (res1.length() > 0) {
                    res1.deleteCharAt(res1.length() - 1);
                }
                return res1.toString();
            }
        } catch (Exception ex) {
            //handle exception
        }
        return "";
    }
    
    0 讨论(0)
提交回复
热议问题