Android get wifi mac address

How to get the missing Wifi MAC Address in Android Marshmallow and later?

Android developers looking to get the Wifi MAC Address on Android M may have experienced an issue where the standard Android OS API to get the MAC Address returns a fake MAC Address (02:00:00:00:00:00) instead of the real value. The normal way to get the Wifi MAC address is below:

final WifiManager wifiManager = (WifiManager) getApplication().getApplicationContext().getSystemService(Context.WIFI_SERVICE); final String wifiMACaddress = wifiManager.getConnectionInfo().getMacAddress(); 

Stack Overflow is for programming questions. What is your question? If you are trying to provide some sort of FAQ entry, please follow the site instructions, and ask a question, then provide your own answer to that question.

It seems that Mac Address is randomized even you can catch it! developer.android.com/about/versions/marshmallow/…

3 Answers 3

In Android M the MACAddress will be «unreadable» for WiFi and Bluetooth. You can get the WiFi MACAddress with (Android M Preview 2):

public static String getWifiMacAddress() < try < String interfaceName = "wlan0"; Listinterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface intf : interfaces) < if (!intf.getName().equalsIgnoreCase(interfaceName))< continue; >byte[] mac = intf.getHardwareAddress(); if (mac==null) < return ""; >StringBuilder buf = new StringBuilder(); for (byte aMac : mac) < buf.append(String.format("%02X:", aMac)); >if (buf.length()>0) < buf.deleteCharAt(buf.length() - 1); >return buf.toString(); > > catch (Exception ex) < >// for now eat exceptions return ""; > 

Somehow I heared that reading the File from «/sys/class/net/» + networkInterfaceName + «/address»; will not work since Android N will be released and also there can be differences between the different manufacturers like Samsung etc.

Hopefully this code will still work in later Android versions.

EDIT: Also in Android 6 release this works

what permission do you think we need for retrieving the networkInterface ? and how does this approach diff from the official solution of Android developer.android.com/about/versions/marshmallow/…?

there isn’t a solution. To access the hardware identifiers of —>nearby external devices

It seems that Mac Address is randomized even you can catch it! developer.android.com/about/versions/marshmallow/…

It works flawlessly +1 for that, Is it appropriate to use this method.. will it work in any sdk from 1 to upcoming sdk

I would recommend to just use it from api-23 (Android M), for older versions I would use the normal way and from N you should check if it is still working.

The MAC Address can still be grabbed from the path:

"/sys/class/net/" + networkInterfaceName + "/address"; 

Simply doing a file read, or a cat of that file will show the Wifi MAC Address.

Network interface names are usually along the lines of «wlan0» or «eth1»

You can get the MAC address from the IPv6 local address. E.g., the IPv6 address «fe80::1034:56ff:fe78:9abc» corresponds to the MAC address «12-34-56-78-9a-bc». See the code below. Getting the WiFi IPv6 address only requires android.permission.INTERNET.

Читайте также:  Вай фай реле своими руками

See the Wikipedia page IPv6 Address, particularly the note about «local addresses» fe80::/64 and the section about «Modified EUI-64».

/** * Gets an EUI-48 MAC address from an IPv6 link-local address. * E.g., the IPv6 address "fe80::1034:56ff:fe78:9abc" * corresponds to the MAC address "12-34-56-78-9a-bc". * * See the note about "local addresses" fe80::/64 and the section about "Modified EUI-64" in * the Wikipedia article "IPv6 address" at https://en.wikipedia.org/wiki/IPv6_address * * @param ipv6 An Inet6Address object. * @return The EUI-48 MAC address as a byte array, null on error. */ private static byte[] getMacAddressFromIpv6(final Inet6Address ipv6) < byte[] eui48mac = null; if (ipv6 != null) < /* * Make sure that this is an fe80::/64 link-local address. */ final byte[] ipv6Bytes = ipv6.getAddress(); if ((ipv6Bytes != null) && (ipv6Bytes.length == 16) && (ipv6Bytes[0] == (byte) 0xfe) && (ipv6Bytes[1] == (byte) 0x80) && (ipv6Bytes[11] == (byte) 0xff) && (ipv6Bytes[12] == (byte) 0xfe)) < /* * Allocate a byte array for storing the EUI-48 MAC address, then fill it * from the appropriate bytes of the IPv6 address. Invert the 7th bit * of the first byte and discard the "ff:fe" portion of the modified * EUI-64 MAC address. */ eui48mac = new byte[6]; eui48mac[0] = (byte) (ipv6Bytes[8] ^ 0x2); eui48mac[1] = ipv6Bytes[9]; eui48mac[2] = ipv6Bytes[10]; eui48mac[3] = ipv6Bytes[13]; eui48mac[4] = ipv6Bytes[14]; eui48mac[5] = ipv6Bytes[15]; >> return eui48mac; > 

Источник

How to find MAC address of an Android device programmatically

How do i get Mac Id of android device programmatically. I have done with IMIE Code and I know how to check Mac id on device manually but have no idea how to find out programmatically.

8 Answers 8

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo wInfo = wifiManager.getConnectionInfo(); String macAddress = wInfo.getMacAddress(); 

Also, add below permission in your manifest file

To provide users with greater data protection, starting in this release, Android removes programmatic access to the device’s local hardware identifier for apps using the Wi-Fi and Bluetooth APIs. The WifiInfo.getMacAddress() and the BluetoothAdapter.getAddress() methods now return a constant value of 02:00:00:00:00:00.

To access the hardware identifiers of nearby external devices via Bluetooth and Wi-Fi scans, your app must now have the ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permissions.

@AndyB developer.android.com/about/versions/marshmallow/… says that this method WifiInfo.getMacAddress() will return constant value, so to get mac address in Android M you amy use this answer stackoverflow.com/a/32948723/4308151 note that wifi must be on

If you use this code as-is, you might get this error: The WIFI_SERVICE must be looked up on the Application context or memory will leak on devices < Android N. Try changing to .getApplicationContext() [WifiManagerLeak]. The following should work instead: WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);

Читайте также:  Вай фай раздача плохая

See this post where I have submitted Utils.java example to provide pure-java implementations and works without WifiManager. Some android devices may not have wifi available or are using ethernet wiring.

Utils.getMACAddress("wlan0"); Utils.getMACAddress("eth0"); Utils.getIPAddress(true); // IPv4 Utils.getIPAddress(false); // IPv6 

Some devices may have one or more ethernet ports (wired or wireless). This is just a linux jargon naming communication ports. Usuallay Android devices have only one wireless wlan0 port available.

See source code in my post getMACAddress, it loops all network interfaces(device ports) you can print out names or do something else with them. stackoverflow.com/questions/6064510/…

@Hackjustu: This does not require any external libs, altought my original answer is using InetAddressUtils.isIPv4Address(sAddr) function, it was removed from Android SDK. Instead should use isIPv4=sAddr.indexOf(‘:’)

With this code you will be also able to get MacAddress in Android 6.0 also

public static String getMacAddr() < try < List all = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif: all) < if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) < return ""; >StringBuilder res1 = new StringBuilder(); for (byte b: macBytes) < //res1.append(Integer.toHexString(b & 0xFF) + ":"); res1.append(String.format("%02X:", b)); >if (res1.length() > 0) < res1.deleteCharAt(res1.length() - 1); >return res1.toString(); > > catch (Exception ex) <> return "02:00:00:00:00:00"; > 

EDIT 1. This answer got a bug where a byte that in hex form got a single digit, will not appear with a «0» before it. The append to res1 has been changed to take care of it.

It’s Working

 package com.keshav.fetchmacaddress; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import java.net.InetAddress; import java.net.NetworkInterface; import java.net.SocketException; import java.net.UnknownHostException; import java.util.Collections; import java.util.List; public class MainActivity extends AppCompatActivity < @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Log.e("keshav","getMacAddr ->" +getMacAddr()); > public static String getMacAddr() < try < Listall = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface nif : all) < if (!nif.getName().equalsIgnoreCase("wlan0")) continue; byte[] macBytes = nif.getHardwareAddress(); if (macBytes == null) < return ""; >StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) < // res1.append(Integer.toHexString(b & 0xFF) + ":"); res1.append(String.format("%02X:",b)); >if (res1.length() > 0) < res1.deleteCharAt(res1.length() - 1); >return res1.toString(); > > catch (Exception ex) < //handle exception >return ""; > > 

This answer got a bug where a byte that in hex form got a single digit, will not appear with a «0» before it. The append to res1 has been changed to take care of it.

 StringBuilder res1 = new StringBuilder(); for (byte b : macBytes) < // res1.append(Integer.toHexString(b & 0xFF) + ":"); res1.append(String.format("%02X:",b)); >

Recent update from Developer.Android.com

Don’t work with MAC addresses MAC addresses are globally unique, not user-resettable, and survive factory resets. For these reasons, it’s generally not recommended to use MAC address for any form of user identification. Devices running Android 10 (API level 29) and higher report randomized MAC addresses to all apps that aren’t device owner apps.

Between Android 6.0 (API level 23) and Android 9 (API level 28), local device MAC addresses, such as Wi-Fi and Bluetooth, aren’t available via third-party APIs. The WifiInfo.getMacAddress() method and the BluetoothAdapter.getDefaultAdapter().getAddress() method both return 02:00:00:00:00:00.

Additionally, between Android 6.0 and Android 9, you must hold the following permissions to access MAC addresses of nearby external devices available via Bluetooth and Wi-Fi scans:

Method/Property Permissions Required

ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION

Источник

Читайте также:  Раздача инета через wifi

How to get current Wi-Fi mac address in android?

This example demonstrate about How to get current Wi-Fi mac address in android.

Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.

Step 2 − Add the following code to res/layout/activity_main.xml.

In the above code, we have taken text view to show WIFI mac address.

Step 3 − Add the following code to src/MainActivity.java

package com.example.myapplication; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; import android.os.Build; import android.os.Bundle; import android.support.annotation.RequiresApi; import android.support.v7.app.AppCompatActivity; import android.widget.TextView; public class MainActivity extends AppCompatActivity < TextView textView; @RequiresApi(api = Build.VERSION_CODES.N) @Override protected void onCreate(Bundle savedInstanceState) < super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); textView = findViewById(R.id.text); WifiManager wifiMgr = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); textView.setText("" + wifiInfo.getMacAddress()); >@Override protected void onStop() < super.onStop(); >@Override protected void onResume() < super.onResume(); >>

Step 4 − Add the following code to androidManifest.xml

Let’s try to run your application. I assume you have connected your actual Android Mobile device with your computer. To run the app from android studio, open one of your project’s activity files and click Run icon from the toolbar. Select your mobile device as an option and then check your mobile device which will display your default screen –

Click here to download the project code

Источник

MAC address of bluetooth or wifi of an android device

I am writing a program about the communication between android device and PC. Is there any way to get the MAC address of BLUETOOTH or WiFi of an android device, when the Bluetooth or WiFi is turned OFF? If so, how?

However, the MAC address is already burned in the phone’s hardware when leaving the factory, isn’t it? @MichaelPetrotta

I’d make it more clear, I want write a program in the android device to get the bluetooth or wifi’s MAC address(turned off), any idea? @MichaelPetrotta

2 Answers 2

this works for me with wifi on and off i not try the bluetooth

WifiManager wimanager = (WifiManager) getSystemService(Context.WIFI_SERVICE); String address = wimanager.getConnectionInfo().getMacAddress(); Log.d("TOKEN", address); 

Yes, you can get MAC addresses even when Bluetooth / WiFi is OFF.

Getting bluetooth information is as easy as this:

BluetoothAdapter.getDefaultAdapter().getAddress(); // MAC address BluetoothAdapter.getDefaultAdapter().isEnabled(); // true if ON 

No need to use Context , yay!

And to complete the answer.. WiFi state:

final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); wifiManager.getConnectionInfo().getMacAddress(); // MAC address wifiManager.getWifiState() == WifiManager.WIFI_STATE_ENABLED; // true if ON 

Источник

Оцените статью
Adblock
detector