Get Device mac adress in Android Nougat and O programmatically
Asked Answered
M

2

4

I have an application, which required device mac address. I'm getting mac address in Marshmallow and below easily but problem with android nougat and O. So how to find mac.

Molina answered 11/4, 2017 at 6:32 Comment(3)
Is this finding mac address programmatically or manually ?Woodie
programmaticallyMolina
why down vote ?Molina
A
3

Changed since 6.0 and later: https://developer.android.com/about/versions/marshmallow/android-6.0-changes.html#behavior-hardware-id

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.

Behavior Changes in 7.0:

Device owners can access device identifiers. A Device owner can access the Wi-Fi MAC address of a device, using DevicePolicyManagewr.getWifiMacAddress(). If Wi-Fi has never been enabled on the device, this method returns a value of null.

Ailurophile answered 11/4, 2017 at 7:12 Comment(2)
i can find mac in 6 Marshmallow but not 7N.Molina
Modified my answer. Now it should be solved. Use DevicePolicyManager (yes, above is DevicePolicyManagewr with W). There is a typo in the docs.Ailurophile
A
2

After one hour,I just solved the Problem.....

DeviceAdminReceiver admin = new DeviceAdminReceiver();
DevicePolicyManager devicepolicymanager = admin.getManager(getApplicationContext());
ComponentName name1 = admin.getWho(getApplicationContext());
if (devicepolicymanager.isAdminActive(name1)){
            String mac_address = devicepolicymanager.getWifiMacAddress(name1);
            Log.e("macAddress",""+mac_address);
}

References :

1) Android for Work : https://developer.android.com/about/versions/nougat/android-7.0-changes.html

2) DevicePolicyManger : https://developer.android.com/reference/android/app/admin/DevicePolicyManager.html#getWifiMacAddress(android.content.ComponentName)

3) DeviceAdminReceiver: https://developer.android.com/reference/android/app/admin/DeviceAdminReceiver.html#getWho(android.content.Context)

Happy Coding....

Allegro answered 13/5, 2017 at 11:10 Comment(2)
If "isAdminActive" is false, what does this mean ?Wilie
This solution is not working since isAdminActive is returning falseCerecloth

© 2022 - 2024 — McMap. All rights reserved.