Open In App

How to Get the MAC of an Android Device Programmatically?

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

MAC stands for Media Access Control. The MAC address is also known as the Equipment Id Number. This MAC Address is provided by the Network Interface Card. In this article, we will see step by step from creating a new empty project to How to make an android app to display MAC Address using Java.

Note: All the hardware access has been removed after Android 11, so if you want to follow these steps then you have to choose the lower version of Android 11.

Step by Step Implementation

Step 1: Open an ‘Android Studio’ and click on New Project.

 

Step 2: Select Empty Activity.

 

Step 3: Select Java as the language for your Android app and give it any name you want.

 

Step 4: Add the following Permissions in the AndroidManifest.xml file.

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

AndroidManifest.xml file will look like this

XML
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    package="com.app.getmymac">

    <!-- Permissions -->
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS" />
    <!-- Permissions -->    
    
    <application
        android:allowBackup="true"
        android:dataExtractionRules="@xml/data_extraction_rules"
        android:fullBackupContent="@xml/backup_rules"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.GetMyMAC"
        tools:targetApi="31">
        <activity
            android:name=".MainActivity"
            android:exported="true">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Step 5: Make a simple design to show MAC Address.

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.

XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="You Device's MAC Address "
        android:textSize="20sp"
        android:textColor="@color/black"
        android:layout_centerHorizontal="true"
        android:layout_above="@id/mac_id"
        />

    <TextView
        android:id="@+id/mac_id"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="MAC ID"
        android:textSize="22sp"
        />

</RelativeLayout>

The above code makes the UI like the below.

Step 6: Now we are in the final phase of the project, Import WifiInfo and WifiManager classes using the below lines of code in MainActivity.java  

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;

Step 7: Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file.

Java
import android.annotation.SuppressLint;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    TextView macIdTV;

    @SuppressLint("MissingPermission")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        macIdTV = findViewById(R.id.mac_id);

        WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
        WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
        final String MAC_ADDRESS = wifiInfo.getMacAddress();
        macIdTV.setText(MAC_ADDRESS);
        macIdTV.setVisibility(View.VISIBLE);

    }
}

Now all are set, just run your app and you will be able to see the MAC address of your Android device. Hooray!

Output:



Last Updated : 15 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads