7

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

Gaurav Arora
  • 8,282
  • 21
  • 88
  • 143
  • You can not get MAC Address if the WiFi is off, in that case you can use DEVICE ID which is again unique for all devices. – user2060383 Feb 19 '13 at 04:31

1 Answers1

14

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.

Alexis
  • 23,545
  • 19
  • 104
  • 143
Siddharth Lele
  • 27,623
  • 15
  • 98
  • 151
  • 2
    I think you should not disable wifi if it is already enabled. It also looks that UPDATE_DEVICE_STATS is not necessary. – Tine M. Jun 27 '13 at 11:36
  • UPDATE_DEVICE_STATS permission is only granted to system apps – euniceadu Nov 16 '13 at 15:16
  • Why do you need to turn the device's WIFI on to get its MAC address? – MikeL Mar 30 '14 at 09:18
  • @MichaelLiberman: When I had to use it the first time round, most tutorials advised that WiFi be turned on. Not necessarily connected to a network, but enabled nonetheless. Also, I got mixed results with WiFi disabled on different devices. So, I just stick to it. Also (_again_), as mentioned before the code, it was untested. I was away from my office PC when I put this in. The logic was always right though. – Siddharth Lele Mar 30 '14 at 13:27
  • @MichaelLiberman: Found an Android Blog (_it was buried deep in my bookmarks somewhere_) which states the same. [Identifying App Installations](http://android-developers.blogspot.in/2011/03/identifying-app-installations.html). Scroll down the section titled **Mac Address**. It essentially says the same. Hope this helps. ;-) – Siddharth Lele Mar 30 '14 at 13:32