Java – Programmatically retrieving MAC addresses – Android

Programmatically retrieving MAC addresses – Android… here is a solution to the problem.

Programmatically retrieving MAC addresses – Android

I’m having trouble retrieving the MAC address of my device programmatically, before someone mentions other posts I’ve already read, such as:
How to find MAC address of an Android device programmatically

But I tried using the code in my own application and testing it with simple log.d and found that it returned nothing. “See if this works” message, but nothing else. So I assume the mac address is empty.

Log.d("seeing if this works", macAddress2);

The code I made looks like this:

//Set onclick listener for the Get Mac Address button
        getMac.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
                WifiInfo wInfo = wifiManager.getConnectionInfo();
                String macAddress2 = wInfo.getMacAddress();

macAddress.setText(macAddress2);
            }
        });

Solution

Which Android version are you testing? Latest (10/2015) Android M preview has prevented the app from obtaining hardware identifiers for Wifi and Bluetooth.

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.

There is a workaround by reading the Wifi MAC from /sys/class/net/wlan0/address, but it will also be blocked in Android N claimed by Google .

Related Problems and Solutions