Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Wi-Fi Mac address in Android Marshmallow

Tags:

android

I am using WiFi MAC address as Unique id, from Marshmallow onwards returning fake MAC address(for security reason). With this my Android application behaves differently. How to get the actual MAC address of the Android device.

I am using the following code-snippet.

WifiManager wmgr = (WifiManager)getSystemService(Context.WIFI_SERVICE);
String wifiId = wmgr.getConnectionInfo().getMacAddress();

Following permissions are added in Manifest file.

<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
like image 658
kaki_hary Avatar asked Oct 13 '15 13:10

kaki_hary


People also ask

How do I find my Wi-Fi MAC address on Android?

Android PhoneOn the Home screen, tap the Menu button and go to Settings. Tap About Phone. Tap Status or Hardware Information (depending on your model of phone). Scroll down to see your WiFi MAC address.

How can I add Wi-Fi MAC address in Mobile?

Go to Wireless->Wireless MAC Filtering page, click the Add New button. Type in the MAC address you want to allow or deny to access the router, and give a description for this item. The status should be Enabled and at last, click the Save button. You need add items in this way one by one.


1 Answers

There is a work-around to get the Mac address in Android 6.0.

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

Source: http://robinhenniges.com/en/android6-get-mac-address-programmatically

like image 158
Rob Anderson Avatar answered Oct 21 '22 11:10

Rob Anderson