Open In App

How to Get the MAC of an Android Device Programmatically?

Last Updated : 15 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
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:



Similar Reads

How to Get Current Default Language of Android Device Programmatically?
Smartphones seem to be predicting and showing us results from what we think in our minds. If you think of traveling, you shall soon expect something related to it in your device's feed. This is the next level of technology, where data helps in developing businesses. The software collects personal data like recent searches, device location, etc., to
2 min read
How to Get the Device's IMEI and ESN Programmatically in Android?
Many times while building Android Applications we require a unique identifier to identify the specific mobile users. For identifying that user we use a unique address or identity. For generating that unique identity we can use the android device id. In this article, we will take a look at How to get Device IMEI and ESN Programmatically in Android.
4 min read
How to Get the Device's IMEI/ESN Programmatically in Android using Jetpack Compose?
Android applications many times require the unique identity of the user while developing applications. This unique identity is used to identify the user of the application. Many android apps use the IMEI number as the unique identity of each user. In this article, we will take a look at How to get Device IMEI and ESN Programmatically in android usi
4 min read
How to Get the MAC of an Android Device using NetworkInterface Class?
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 wi
3 min read
How to Find the Screen Resolution of a Device Programmatically in Android?
Screen Resolution refers to the number of pixels on display. A higher resolution means more pixels and more pixels provide the ability to display more visual information. This entity is widely used in applications related to the broadcasting of real-time visuals such as live video, gaming, etc for optimization and frame conversions. The same inform
3 min read
How to Display the List of Sensors Present in an Android Device Programmatically?
All Android devices produced worldwide come with built-in sensors that measure motion, orientation, and various environmental conditions. These sensors generally facilitate Android architecture by providing the data from the sensor for various applications. For example, a temperature sensor provides the device's temperature, information from which
3 min read
How to Vibrate a Device Programmatically in Android?
Hepatic feedback are also considered when it comes to user experience. So in this discussion, it's been discussed various types of haptics or the types of vibration of the device. For example, click haptics or long-press button haptics. There five different types of vibration modes in haptic feedback discussed are: Default vibration of the deviceCl
7 min read
How to Fetch Device ID in Android Programmatically?
The android Device ID is a unique code, string combinations of alphabets and numbers, given to every manufactured android device. This code is used to identify and track each android device present in the world. In Android, the Device ID is typically related to the Google Play Services and is most commonly used in ad personalization. These IDs are
3 min read
Android Device Manager - Find My Device
Android Device Manager (ADM), nowadays known as Find My Device, is a very useful feature offered by Google to help Android users locate, secure, and manage their smartphones and tablets in case they are lost or stolen. This powerful tool offers a range of functionalities that provide peace of mind and ensure the safety of your Android devices. In t
8 min read
How to Get RAM Memory in Android Programmatically?
RAM (Random Access Memory) of a device is a system that is used to store data or information for immediate use by any application that runs on the device. Every electronic device that runs a program as a part of its application has some amount of RAM associated with it. Mobile devices nowadays come with a RAM of 4 to 16 GB depending on the model. T
3 min read