Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate for NetworkInterface.getHardwareAddress()?

I have used this API to pick the mac address of device,

NetworkInterface.getHardwareAddress()

But this is for API level 9 and later, what should i use to pick the mac address for API level 8? froyo device.

May be this a very simple thing, but i tried googling and couldn't find the answer.

like image 915
Khurram Avatar asked Dec 04 '25 15:12

Khurram


2 Answers

WifiInfo.getMacAddress() has been available since API level 1.

WifiManager wifiMan = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiMan.getConnectionInfo();
String macAddr = wifiInf.getMacAddress();

You'll need to add:

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

To your manifest

like image 116
Raghav Sood Avatar answered Dec 07 '25 04:12

Raghav Sood


WifiInfo.getMacAddress() always gives you Wi-Fi MAC address although your active interface may be currently cellular. If the intended purpose is to get the associated hardware address (such as from cellular connection), then MAC should be retrieved from rmnet0 interface and so on (if IP/MAC association is required).

like image 30
Konjengbam Avatar answered Dec 07 '25 03:12

Konjengbam