1

I can't get the mac address can anyone help me please I am newbies in android development please help me manifest.xml

    <receiver
        android:name="com.example.v3rlu.authentification.DeviceAdmin"
        android:permission="android.permission.BIND_DEVICE_ADMIN">
        <meta-data android:name="android.app.device_admin"
            android:resource="@xml/xml" />
        <intent-filter>
            <action android:name="android.app.action.DEVICE_ADMIN_ENABLED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLE_REQUESTED" />
            <action android:name="android.app.action.ACTION_DEVICE_ADMIN_DISABLED" />
        </intent-filter>
    </receiver>

launch activity checkact

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_check);
    mac=(TextView) findViewById(R.id.textView) ;
    DeviceAdminReceiver admin = new DeviceAdminReceiver();
    DevicePolicyManager devicepolicymanager = 
    admin.getManager(getApplicationContext());
    ComponentName name1 = admin.getWho(getApplicationContext());
    if (devicepolicymanager.isAdminActive(name1)){
        mac_address = devicepolicymanager.getWifiMacAddress(name1);}

mac.setText(mac_address);
}}

for the receiverclass I got the code from android site

Abhishek Tripathi
  • 267
  • 1
  • 7
  • 18
  • 2
    Possible duplicate of [How to find MAC address of an Android device programmatically](https://stackoverflow.com/questions/10831578/how-to-find-mac-address-of-an-android-device-programmatically) – Markus Kauppinen Dec 02 '17 at 12:07
  • https://stackoverflow.com/questions/43338359/get-device-mac-adress-in-android-nougat-and-o-programmatically/43339101 – Juan Dec 02 '17 at 13:24

2 Answers2

4

Try this one.

public String getMacAddr() {
    try {
        List<NetworkInterface> 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 "";
}

Add internet permission in manifest

<uses-permission android:name="android.permission.INTERNET"/>
venkatesh kumar
  • 167
  • 2
  • 10
0

Try getting mac address using WifiManager. Try below code.

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

add permission in manifest

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
venkatesh kumar
  • 167
  • 2
  • 10