Tech Blog

Android, Coding, Programming, Technology

Disable MAC Randomization in Android 10 and above devices

By Kumar gaurav3 min read
← Back to blog

Recently we came across an interesting feature, to set static MAC address for the wifi ssids for Android 10 and above devices.MAC Randomization generates a random MAC address for a wifi ssid for the following conditions mentioned below :-

  • When connecting with that particular network for the first time\=-
  • Forget and rejoin the network.
  • in every 24 hours, if connected to the same network

Randomised MAC keeps us harder to track, by increasing our privacy and security.

However, disable MAC randomization can be used when we use a MAC filter on our router. If our devices use random mac, then it will be very difficult(nearly impossible) to track our devices and use MAC specific settings for our device.

Disable of MAC Randomization can be done only for a particular wifi ssid. Once the device connects to any other network, the configuration of disable MAC randomization is lost for the network or ssid where earlier this restriction was imposed. To disable MAC through system settings, please follow the steps as mentioned below :-

  1. Open Settings.
  2. Tap on Network and Internet
  3. Tap on the gear icon associated with the wifi network connected
  4. Tap on Mac Address Type(Android 10) or Privacy(Starting from Android 11 devices)
  5. Select Phone MAC.
  6. Re-join the network.

Now to disable MAC randomization programmatically in android 10 and above devices, we need to be a platform signed application or rooted device to perform the operation. We can use the code snippet below to disable MAC randomization for a particular network or the active wifi network using the following code snippet –

//Get the wifi manager object
WifiManager lObjWifiManager = (WifiManager)
ExceptionHandlerApplication.getAppContext().getApplicationContext().getSystemService(Context.WIFI_SERVICE); 

//Get the wifi Configuration object for the active wifi network to which device is connected
WifiConfiguration lObjWifiConfigurationObj =  lObjWifiManager.getConfiguredNetworks().get(0);

//Using the reflection method, get the class object for WifiConfiguration class
Class<?> lObjClss = Class.forName("android.net.wifi.WifiConfiguration");

//Using the class object, get the field macRandomizationsetting
Field lObjField = lObjClss.getField("macRandomizationSetting");
lObjField.setAccessible(true);

//Now set the field to 0 for using Mac randomization and 1 for phone Mac (to disable mac Randomization)
lObjField.set(lObjWifiConfigurationObj ,0);//the value can be 0 or 1

//update the network
lObjWifiManager.updateNetwork(lObjWifiConfigurationObj );

//rejoin the network using the following apis
lObjWifiManager.disconnect(); or // lObjWifiManager.reconnect();

To overcome the situation of connecting to another network and losing the disable MAC randomization for the active network, we can always store the wifi ssid or wifi configuration object in Shared Preference or Database of the application. By  implementing a broadcast receiver that listens to Wifi Connectivity Receiver, we can listen to the broadcasts for the network change and can implement our logic to compare the wifi ssid or wifi Configuration object from database or shared Preferences and recall the above logic as per our requirement to impose the restrictions of disable MAC address for particular wifi ssid even if it connected after connecting to another network in between .

For example –

public class WifiConnectivityReceiver extends BaseBroadcastReceiver
{
   @Override
    public void delayedOnReceive(final Context context, final Intent arg1)
    {
              If(arg1.getAction().equalsIgnoreCase(“android.net.conn.
              CONNECTIVITY_CHANGE”))
             {
                  /Implement your logic of identifying the network from the database or Shared Preferences and impose the restriction to disable MAC addresses.
             }
    }
}

Keep reading

You may be interested in

Browse all articles →
Set up work profile on company owned android 11 and above device
June 1, 20223 min read

Set up work profile on company owned android 11 and above device

Android 11 introduces work profiles for company owned devices with improved support for work profiles. If a work profile is created from a setup wizard using QR code or zero touch enrollment, the device is recognized as Work profile on company owned devices. If a work profile is created using any other method except the […]

By Kumar gauravRead More →
Getting Notification related to our application using microphone even though microphone permission is not declared in application’s Manifest file (Android).
March 24, 20224 min read

Getting Notification related to our application using microphone even though microphone permission is not declared in application’s Manifest file (Android).

Recently, we found an interesting bug while working with one of our partners. The issue was that even though our application and the libraries needed for our application were not using the microphone permission,still we were getting notifications related to our application using the microphone.The issue was easily reproducible on their set of devices as […]

By Kumar gauravRead More →
How Angular is Different from AngularJS
September 27, 20227 min read

How Angular is Different from AngularJS

What is Angular? Angular is a popular open-source Typescript framework and free to use. Considered to be a JavaScript-based framework,TypeScript.is its primary programming language. TypeScript is Developed by Microsoft and its superset of JavaScript, which simply means that TypeScript has all the capabilities that JS has and more additional features. Angular is majorly used for […]

By Sachin DevadigaRead More →