changed advanced wifi option from the code, but it will not work?

advertisements

I used this code to change wifi Ip setting (Ip Address and gateway)

you can see the full code here : full class

WifiConfiguration wifiConf = null;
    WifiManager manager = (WifiManager) getSystemService(MainActivity.WIFI_SERVICE);
        WifiInfo connectionInfo = manager.getConnectionInfo();
    List<WifiConfiguration> configuredNetworks = manager
            .getConfiguredNetworks();

    wifiConf = GetCurrentWifiConfiguration(manager);

setIpAssignment("STATIC", wifiConf);
setIpAddress(InetAddress.getByName("192.2.2.2"), 24, wifiConf);
setGateway(InetAddress.getByName("192.2.2.22"), wifiConf);
manager.updateNetwork(wifiConf);
manager.saveConfiguration();

functions:

 public static void setIpAssignment(String assign , WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException{
    setEnumField(wifiConf, assign, "ipAssignment");
}

public static void setIpAddress(InetAddress addr, int prefixLength, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
        NoSuchMethodException, ClassNotFoundException, InstantiationException, InvocationTargetException{
    Object linkProperties = getField(wifiConf, "linkProperties");
    if(linkProperties == null)return;
    Class laClass = Class.forName("android.net.LinkAddress");
    Constructor laConstructor = laClass.getConstructor(new Class[]{InetAddress.class, int.class});
    Object linkAddress = laConstructor.newInstance(addr, prefixLength);

    ArrayList mLinkAddresses = (ArrayList)getDeclaredField(linkProperties, "mLinkAddresses");
    mLinkAddresses.clear();
    mLinkAddresses.add(linkAddress);
}

public static void setGateway(InetAddress gateway, WifiConfiguration wifiConf)
        throws SecurityException, IllegalArgumentException, NoSuchFieldException, IllegalAccessException,
        ClassNotFoundException, NoSuchMethodException, InstantiationException, InvocationTargetException {
    Object linkProperties = getField(wifiConf, "linkProperties");
    if(linkProperties == null)return;
    Class routeInfoClass = Class.forName("android.net.RouteInfo");
    Constructor routeInfoConstructor = routeInfoClass.getConstructor(new Class[]{InetAddress.class});
    Object routeInfo = routeInfoConstructor.newInstance(gateway);

    ArrayList mRoutes = (ArrayList)getDeclaredField(linkProperties, "mRoutes");
    mRoutes.clear();
    mRoutes.add(routeInfo);
}

problem is here :

these codes successfully change the ip address and gateway when you check wifi connection advanced option from setting in your android device, but the setting actually isn't set!!!

how I understand this :

first I set a fake Ip address and gateway manually, modify network-> advanced option. and then try to open a website with chrome, no internet access and it worked. then I set the same Ip address and gateway for connected ssid from code but It's not worked and still have internet access.

I used same settings from code and manually but its not worked from code. where I did wrong ?


As I see this you were able to change the settings and get the values changed on settings view, but the actual change on system state on device did not happen.

You most propably don't have privileges to change wifi settings with your program.

Try adding the following:

ACCESS_WIFI_STATE
CHANGE_WIFI_STATE
ACCESS_NETWORK_STATE

See my comments on question for more information.