23

How do I get the MAC-Address of the network interface of an android device which doesn't have a Wifi-Interface (e.g. the android emulator)? WifiInfo obtained via the WifiManager returns null.

EDIT

To be more clear: I have to communicate with an existing network protocol (not designed by me) on the local network where I have to send the mac address of the communicating interface within the payload during a registration phase.

Tom
  • 5,068
  • 5
  • 29
  • 41
  • The external device won't be able to see the real mac address anyway, since there's a NAT (the PC) in the way, so what is the difference between discovering it and making one (with the locally administrated bit) up? – Chris Stratton May 31 '11 at 22:15
  • This is only the case for emulators. What about android devices with an ethernet interface (wired network). – Tom Jun 01 '11 at 07:41
  • 3
    It turned out, that the protocol does not explicitly require a valid mac address but a unique identifier. An existing implementation just used the mac-address as this identifier which is why I assumed that it's necessary. This is why Alexander Lucas answer about unique identifiers fits best to my situation. However I think the question of getting a mac address for non-wifi devices may be still interesting for others which is why I marked the answer of Chris Stratton as the best. – Tom Jun 01 '11 at 10:58
  • I posted here working solution https://stackoverflow.com/a/47789324/5330408 – Android Developer Dec 13 '17 at 11:23

5 Answers5

25

I'm going to take a leap and assume that you want this MAC address in order to establish a unique identifier for the device. Mac Addresses are not the way to do this.

There's an Android Developer Blog post titled "Identifying App Installations" which covers the topic of generating unique ID's fairly well, including the popular methods, and the pros/cons. It's definitely worth a read. Quite relevant to this post is the following quote:

It may be possible to retrieve a Mac address from a device’s WiFi or Bluetooth hardware. We do not recommend using this as a unique identifier. To start with, not all devices have WiFi. Also, if the WiFi is not turned on, the hardware may not report the Mac address.

The options available to you instead include TelephonyManager.getDeviceId(), android.os.Build.SERIAL, and Settings.Secure.ANDROID_ID, all of which are covered in more detail in the linked post.

Alexander Lucas
  • 22,171
  • 3
  • 46
  • 43
  • Sorry, this doesn't apply to what I need. I have to communicate with an existing protocol (not implemented by me) where I have to send my local mac address within the payload during a registration phase. – Tom May 31 '11 at 21:34
  • 2
    `android.os.Build.SERIAL` is only available in Android 2.3 and later. – derekerdmann May 18 '12 at 18:05
18

Read /sys/class/net/[something]/address as a text file

But it's unlikely to be useful in the way you think.

Chris Stratton
  • 39,853
  • 6
  • 84
  • 117
  • 1
    For me it works if I replace [something] with "eth0". Thanks a lot for sharing this. – Tirtha Oct 09 '12 at 15:20
  • The reason it is "unlikely to be useful" is that Android devices will usually be behind NAT, which makes their actual client MAC address irrelevant - you could provide any random number, persistent across sessions or not at your preference. – Chris Stratton Jun 09 '14 at 17:00
5

See this post where I have submitted Utils.java example to provide pure-java implementations.

Utils.getMACAddress("wlan0");
Utils.getMACAddress("eth0");
Utils.getIPAddress(true); // IPv4
Utils.getIPAddress(false); // IPv6 
Community
  • 1
  • 1
Whome
  • 10,181
  • 6
  • 53
  • 65
  • No!! To get wifi mac address. I have to at least turn on wifi once using this method. When I use `Utils.getMACAddress("wlan0");` after the device is just boot up, it returns a fake address. – Yeung Jan 08 '15 at 09:09
2

What is the network interface you want the MAC address of? If there's no wifi, you certainly can't get the wifi device's MAC address. It represents the physical hardware and if that's not present, it simply doesn't exist.

Jess
  • 42,368
  • 6
  • 37
  • 51
  • 2
    Yes, it's true that a mac address of the wifi interface simply doesn't exist. But the question is how to get the mac address for the interface which eventually sends the message (e.g. that could be a ethernet interface or a virtual interface on the emulator). – Tom May 31 '11 at 21:36
0

To get wifi MAC of android device using adb: adb shell getprop ril.wifi_macaddr

Use the following code in Java to get it programmatically:

Process p = Runtime.getRuntime.exec("adb", "shell", "getprop", "ril.wifi_macaddr")
BufferedReader br = new BufferedReader(new InputStreamReader(p.getInputStream());
String macAddress = br.readLine();
Thejus Krishna
  • 1,755
  • 1
  • 19
  • 28