Android6 and above get MAC address

To provide users with stricter data protection, starting with Android 6.0 (Marshmallow), Android removed programmatic access to the device's local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() method and the BluetoothAdapter.getAddress() method now return the constant value 02:00:00:00:00:00.

 

Now, to access hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scanning, your app must have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission.

 

refer  Android 6.0 Changes

 

Note: Don't foolishly think that adding the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission can access the MAC address of the mobile phone where your application is located. The above is to scan the hardware identifiers of other nearby devices.

 

Current Alternatives

public 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 res = new StringBuilder();
            for (byte b : macBytes) {
                res.append(String.format("%02X:",b));
            }

            if (res.length() > 0) {
                res.deleteCharAt(res.length() - 1);
            }

            return res.toString();
        }
    } catch (Exception ex) {
        Log.w("MacAddr", "exception during retrieving MAC address: " + ex.getMessage());
    }

    return "02:00:00:00:00:00";
}

 

The feasible condition of this scheme is to ensure that the WLAN (WiFi) is in an open state, and it is not a necessary condition to be able to access the network.

 

refer Android 6.0 - You CAN NO longer access the Mac-Address? You can !

 

 

 

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326305520&siteId=291194637