Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting the MAC address of the device- when wifi is off

I am finding the MAC address of the Android Device using the following code:

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress());

But in this case I am unable to get the MAC address when the Wifi is off. How can I get the MAC address of the Android Device even when WIFI is off.

Thanks

like image 791
Gaurav Arora Avatar asked Feb 19 '13 04:02

Gaurav Arora


People also ask

How to find the MAC address of your Wi-Fi network adapter?

A Command Prompt window will appear to enter commands. All the results related to the network configuration of our computer will appear. What we should look for is a section called the physical address of our Wifi network adapter. This will be the MAC of our Wi-Fi network adapter.

How do I Find my IP address and MAC address on Android?

Tap the “i” icon to the right of any Wi-Fi connection. You’ll see the IP address and other network details here. To find your MAC address, head to Settings > General > About. Scroll down a bit and you’ll see your MAC address listed as “Wi-Fi Address.”

Is there a way to find the MAC address of another computer?

However, it only works within the small group of computers on a local area network (LAN), not across the internet. There's another method used to find the MAC address of the computer you're currently using, which involves using the ipconfig /all command in Windows.

What is the MAC address of a device?

MAC stands for Media Access Control, and, long story short, the MAC address is a code used to identify a particular computer or device on various types of networks, such as Wi-Fi, ethernet, Bluetooth and many more. Thanks! How can I get my device's MAC address if I no longer have the device?


1 Answers

Why not enable the Wifi momentarily until you get the MAC address and then disable it once you are done getting the MAC address?

Of course, doing this is if getting the MAC address is absolutely important.

UNTESTED CODE

WifiManager wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

if(wifiManager.isWifiEnabled()) {
    // WIFI ALREADY ENABLED. GRAB THE MAC ADDRESS HERE
    WifiInfo info = wifiManager.getConnectionInfo();
    String address = info.getMacAddress();
} else {
    // ENABLE THE WIFI FIRST
    wifiManager.setWifiEnabled(true);

    // WIFI IS NOW ENABLED. GRAB THE MAC ADDRESS HERE
    WifiInfo info = wifiManager.getConnectionInfo();
    String address = info.getMacAddress();
}

You will need these permission setup in the Manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"></uses-permission>
<uses-permission android:name="android.permission.UPDATE_DEVICE_STATS"></uses-permission>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"></uses-permission>

I am not entirely sure if the UPDATE_DEVICE_STATS permission is necessary in this case. Please try it out before deciding to keep it.

like image 139
Siddharth Lele Avatar answered Sep 28 '22 03:09

Siddharth Lele