2

i am working on Android with Java (Android studio) to create Android app so i want to get Mac Address. so my Question is how i can find Mac address? Mac address will be Unique for each User. User will download the app then he should display the mac address so he can use same mac address to activate the device from website. so i just want to get MAC Address and it will be unique for each user.

2 Answers2

1

Step 1: Add the permissions. In Manifest.xml file add below lines

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

Step 2: Create the method to get the MAC address

public String getMacAddress(){
        try{
            List<NetworkInterface> networkInterfaceList = Collections.list(NetworkInterface.getNetworkInterfaces());
            String stringMac = "";
            for(NetworkInterface networkInterface : networkInterfaceList){
                if(networkInterface.getName().equalsIgnoreCase("wlon0"));
                {
                    for(int i=0;i <networkInterface.getHardwareAddress().length; i++){
                        String stringMacByte = Integer.toHexString(networkInterface.getHardwareAddress()[i]& 0xFF);
                        if(stringMacByte.length()==1){
                            stringMacByte = "0" +stringMacByte;
                        }
                        stringMac += stringMacByte.toUpperCase() + ":";
                    } break;
                }
            }
            return stringMac;
        }catch (SocketException e)
        {
            e.printStackTrace();
        }
        return  "0";
    }

Step 3: Call the method to get the MAC Address

String mobile_mac_addres = getMacAddress();  //call the method that return mac address 
Log.d("MyMacIS",mobile_mac_address);  // this prints the MAC Address
raiyan22
  • 1,043
  • 10
  • 20
  • 1
    these didn't work for android 10, im looking for an advance method – Mohammad Engineer Dec 28 '21 at 18:37
  • 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. – Mohammad Engineer Dec 28 '21 at 18:39
  • 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. – Mohammad Engineer Dec 28 '21 at 18:39
  • 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 – Mohammad Engineer Dec 28 '21 at 18:39
  • this is the documentation from google – Mohammad Engineer Dec 28 '21 at 18:40
0

use this code programatically get macAddress

WifiManager wifiManager = (WifiManager)   getSystemService(Context.WIFI_SERVICE);
    WifiInfo wInfo = wifiManager.getConnectionInfo();
    String macAddress = wInfo.getMacAddress();
Pouria Hemi
  • 706
  • 5
  • 15
  • 30