How to Get the MAC of an Android Device using NetworkInterface Class?
Last Updated :
28 Apr, 2025
The MAC (Media Access Control) address in Android is a unique identifier assigned to the Wi-Fi module of the device. It is used to identify devices on a network and helps in network communication and security. The general method for getting the MAC address of the android device is using the Wifi Manager class in android studio. Getting mac using wifi manager class is given here. But, to provide users with greater data protection android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi APIs. The WifiInfo.getMacAddress() methods now return a constant value of 02:00:00:00:00:00.
In Android M (Marshmallow) the MAC address is unreadable for WiFi. You can get the WiFi MAC Address with Android M Preview 2 (Android M Preview 2 is the second developer preview release of the Android M (Marshmallow) operating system).
Implementation using Network Interface Class
Step 1: Create a New Project
To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.
Step 2: Add the following Permissions in the AndroidManifest.xml file
XML
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"
tools:ignore="ProtectedPermissions"/>
<!-- Permissions -->
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">
<!-- Permissions -->
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name = "android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.LOCAL_MAC_ADDRESS"
tools:ignore="ProtectedPermissions"/>
<!-- 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.NetworkInterfaceGFG"
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>
<meta-data
android:name="android.app.lib_name"
android:value="" />
</activity>
</application>
</manifest>
Step 3: Make a 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">
<androidx.appcompat.widget.AppCompatButton
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Get WIFI MAC"
android:backgroundTint="#44D10D"
android:layout_centerInParent="true"/>
<TextView
android:id="@+id/Mac_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAC HERE"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:textStyle="bold"
android:textColor="@color/black"/>
</RelativeLayout>
The above code looks like the below image in the user interface:
UI
Step 4:
Now we are in the last phase of the project, import NetworkInterface, Collections, and List classes using the below lines of code in MainActivity.java.
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
Step 5: Working with the MainActivity.java file
Go to the MainActivity.java file and refer to the following code. Below is the code for the MainActivity.java file. Comments are added inside the code to understand the code in more detail.
Java
package com.example.networkinterfacegfg;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView MAC_TV;
AppCompatButton button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
MAC_TV = findViewById(R.id.Mac_TV);
button.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
MAC_TV.setText(getWIFIMAC());
MAC_TV.setVisibility(View.VISIBLE);
}
});
}
public static String getWIFIMAC() {
try {
String interfaceName = "wlan0";
List<NetworkInterface> interfaces = Collections.list(NetworkInterface.getNetworkInterfaces());
for (NetworkInterface inter_face : interfaces) {
if (!inter_face.getName().equalsIgnoreCase(interfaceName)){
continue;
}
byte[] mac = inter_face.getHardwareAddress();
if (mac==null){
return "";
}
StringBuilder buffer = new StringBuilder();
for (byte aMac : mac) {
buffer.append(String.format("%02X:", aMac));
}
if (buffer.length()>0) {
buffer.deleteCharAt(buffer.length() - 1);
}
return buffer.toString();
}
} catch (Exception ignored) { }
return "";
}
}
Now all are set, just run your app and you will be able to see the Wi-Fi MAC address of your Android device.
Output:
Output
Similar Reads
Java Tutorial Java is a high-level, object-oriented programming language used to build web apps, mobile applications, and enterprise software systems. It is known for its Write Once, Run Anywhere capability, which means code written in Java can run on any device that supports the Java Virtual Machine (JVM).Java s
10 min read
Java Interview Questions and Answers Java is one of the most popular programming languages in the world, known for its versatility, portability, and wide range of applications. Java is the most used language in top companies such as Uber, Airbnb, Google, Netflix, Instagram, Spotify, Amazon, and many more because of its features and per
15+ min read
Java OOP(Object Oriented Programming) Concepts Java Object-Oriented Programming (OOPs) is a fundamental concept in Java that every developer must understand. It allows developers to structure code using classes and objects, making it more modular, reusable, and scalable.The core idea of OOPs is to bind data and the functions that operate on it,
13 min read
Arrays in Java Arrays in Java are one of the most fundamental data structures that allow us to store multiple values of the same type in a single variable. They are useful for storing and managing collections of data. Arrays in Java are objects, which makes them work differently from arrays in C/C++ in terms of me
15+ min read
Inheritance in Java Java Inheritance is a fundamental concept in OOP(Object-Oriented Programming). It is the mechanism in Java by which one class is allowed to inherit the features(fields and methods) of another class. In Java, Inheritance means creating new classes based on existing ones. A class that inherits from an
13 min read
Collections in Java Any group of individual objects that are represented as a single unit is known as a Java Collection of Objects. In Java, a separate framework named the "Collection Framework" has been defined in JDK 1.2 which holds all the Java Collection Classes and Interface in it. In Java, the Collection interfac
15+ min read
Java Exception Handling Exception handling in Java allows developers to manage runtime errors effectively by using mechanisms like try-catch block, finally block, throwing Exceptions, Custom Exception handling, etc. An Exception is an unwanted or unexpected event that occurs during the execution of a program, i.e., at runt
10 min read
Java Programs - Java Programming Examples In this article, we will learn and prepare for Interviews using Java Programming Examples. From basic Java programs like the Fibonacci series, Prime numbers, Factorial numbers, and Palindrome numbers to advanced Java programs.Java is one of the most popular programming languages today because of its
8 min read
Java Interface An Interface in Java programming language is defined as an abstract type used to specify the behaviour of a class. An interface in Java is a blueprint of a behaviour. A Java interface contains static constants and abstract methods. Key Properties of Interface:The interface in Java is a mechanism to
12 min read
Polymorphism in Java Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read