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 :-
- Open Settings.
- Tap on Network and Internet
- Tap on the gear icon associated with the wifi network connected
- Tap on Mac Address Type(Android 10) or Privacy(Starting from Android 11 devices)
- Select Phone MAC.
- 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.
}
}
}