Skip to content

[Solved] Mac Address of Android Device Display 02:00:00:00:00:00

Last Updated on 2022-07-21 by Clay

Problem

Today I am in a limited network environment and need to register the mac address of my mobile device in the WiFi settings so that I can connect to the Internet... But when I confirm the mac address of my mobile device, I was surprised to find that my address was 02:00:00:00:00:00.

At beginning, I laughed and told my colleagues that maybe I was the chosen one... but of course, I quickly found that my phone couldn't connect to the WiFi network. After inquiries, I found that the mysterious number of 02:00:00:00:00:00 was a display result that was blocked to some extent.


Solution

It is recommended to open a new project with Android Studio and edit the following three files (you can find them in the project folder):

  • AndroidManifest.xml
  • MainActivity.java
  • activity_main.xml

The first is to give permission, find AndroidManifest.xml under manifests, edit it and paste the following settings before the <application> tag:

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


接著撰寫指定功能(得到 mac address)的程式碼。

Then write the code for the specified function for getting the mac address.

(Note that the package com.example.test in the first line may need to be rewritten as your own package, because you named the project is different with me.)

package com.example.test;
//package com.example.getmacaddress;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
    String mobile_mac_address;
    TextView macaddress;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        macaddress = (TextView)findViewById(R.id.macaddress);
        mobile_mac_address = getMacAddress();
        Log.d("MyMacIS",mobile_mac_address);
        macaddress.setText(mobile_mac_address);
    }
    public String getMacAddress(){
        try{
            List<NetworkInterface> networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces());
            String stringMac = "";
            for(NetworkInterface networkInterface : networkInterfaceList)
            {
                if(networkInterface.getName().equalsIgnoreCase("wlon0"));
                {
                    for(int i = 0 ;i <networkInterface.getHardwareAddress().length; i++){
                        String stringMacByte = Integer.toHexString(networkInterface.getHardwareAddress()[i]& 0xFF);
                        if(stringMacByte.length() == 1)
                        {
                            stringMacByte = "0" +stringMacByte;
                        }
                        stringMac = stringMac + stringMacByte.toUpperCase() + ":";
                    }
                    break;
                }
            }
            return stringMac;
        }catch (SocketException e)
        {
            e.printStackTrace();
        }
        return  "0";
    }
}



Finally, drag an interface at will (mainly to display the mac address), this file is usually placed under res > layout.

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/macaddress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>


Then run the program, maybe you will see your mac address appear on your mobile device.

By the way, my mobile device showed a wrong mac address, but I went back to the settings and found that the masked field that was originally displayed as 02:00:00:00:00:00 became my real mac address, you can refer to it.


References


Read More

Leave a Reply