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:
See my comments on question for more information.
Adding any number of duplicate SSID's is possible if you have a unique IP address.
In lay man's terms SSID is nothing but just an identification given to that particular network given to distinguish that network from others for simplicity. What defines that network is it's IP Address.If you have a conflict between an IP address then it may be a real problem.
For some devices(like Samsung) you need to check the to Display open Network connection if available in order to view open connections and for some devices it shows by default.
Ex:-Let's say you have two network's with SSID as Test. Test1 has an open connection available for public use and Test2 is for personal use and is encrypted. Your mobile device will simply connect to a network whose signal strength is good. As the IP address is different there will not be any conflict and you can do your browsing.
In case there's any conflict(same signal strength) then the device will ask the user to select the desired network or search for a new network.
Caution:-Browsing an open network for reasons related to finance leads to severe security threats browse at your own risk.
An IP address serves two principal functions: host or network interface identification and location addressing. Its role has been characterized as follows: "A name indicates what we seek. An address indicates where it is. A route indicates how to get there.
The most simple definition for IP Address I could give is-
IP address act as you house address to which the shipment has to be delivered.If you have two same addresses then to whom will the shipment be delivered. For further details about IP read this
Now basically my answer to your question is :
You can defiantly add multiple setting with ssid but different security level
SMALL DEMO I HAVE PREPARED :
HERE IN ABOVE CODE TRY RUNNING SAME CODE WITH :
once at a time :
OBSERVATION :BOTH THE SETTINGS GET ADDED PROGRAMMATICALLY BUT CAN BE SEEN ONLY ONCE SINCE BOTH ARE OVERLAPPING WITH EACH OTHER..BUT BOTH ARE PRESENT. now question is how do I know both is present? 1. Run above code. 2. once with OPEN as security level and once with WEP 3. once added tab the wifi and there is option of forgot network..click it 4. You will observer that although you deleted it, it's still there :) 5. Hence both the settings are there but get overlapped programmatically.
MY ANALYSIS WITH HOTSPOT :
I turned 2 device as hotspot with same ssid but different security level and i can see 2 separate wifi but the thing is when added programmatically it stay there but overlapping.
NOTE : ADDING MANUALLY OR PROGRAMMATICALLY SAME WIFI SETTING WILL SURLY OVERWRITE EXISTING ONE BUT EVEN WITH 1 SETTING DIFFERENCE IT WORKS.
I reopen wifi and it works phone`s version 4.2.2