-2

I am using this code to get MAC ADDRESS and display it in my app. The code works fine for all devices except most latest devices and ANDROID BOX.

it shows null for ANDROID BOX and other latest device.

Here is code:

   public static String getWifiMacAddress() {
    try {
        String interfaceName = "wlan0";
        List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
        for (NetworkInterface intf : interfaces) {
            if (!intf.getName().equalsIgnoreCase(interfaceName)){
                continue;
            }

            byte[] mac = intf.getHardwareAddress();
            if (mac==null){
                return "";
            }

            StringBuilder buf = new StringBuilder();
            for (byte aMac : mac) {
                buf.append(String.format("%02X:", aMac));
            }
            if (buf.length()>0) {
                buf.deleteCharAt(buf.length() - 1);
            }
            return buf.toString();
        }
    } catch (Exception ex) { } // for now eat exceptions
    return "";
}

I have written these permissions in manifest file

 <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
 <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
  <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
Cœur
  • 37,241
  • 25
  • 195
  • 267
Pir Fahim Shah
  • 10,505
  • 1
  • 82
  • 81

3 Answers3

0

Firstly u will check the permission is granted or not?

WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wifiManager.getConnectionInfo();
 String macAddress = wInfo.getMacAddress();

Also, add below permission in your manifest file

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

Please refer this link for 6.0 marshmalow

Rahul Karande
  • 259
  • 2
  • 9
0

First you will need to add Internet user permission in AndroidManifest.xml.

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

and then Refer this to get mac address: http://robinhenniges.com/en/android6-get-mac-address-programmatically

And if it doesn't works then refer this Android 6.0 changes from this i have concluded that,

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. Note That : you can't get your own MACs even having those permissions. Read carefully, It is said that you can get other devices MACs having those permissions, but not your own.

Akshay Tilekar
  • 1,910
  • 2
  • 12
  • 22
-1

As in 6.0 and above adding permission in Manifest alone wont work. You should have runtime permission and grant it if not granted.

Check this link:

https://stackoverflow.com/a/30549756/3910281

Community
  • 1
  • 1
Karthik CP
  • 1,150
  • 13
  • 24