Getting MAC address in Android 6.0

后端 未结 10 2269
醉梦人生
醉梦人生 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: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 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 :

    
        
    
    • 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

提交回复
热议问题