Im working on one project and there should be a functionality for setting static IP address (DNS, Netmask, Gateway) for Wifi
if user want it.
My initial and actual solution is an usage of android.provider.Settings.System
class that allows this feature but this solution works successfully only for Android 2.x devices.
It's nice, definitely i'm not on totally zero but it would be nice to get it work also for higher versions of Android OS. It don't know exactly why it doesn't work.
If i use this simple method for checking actual status:
public static final boolean hasStaticIp(Context c) {
try {
return Settings.System.getInt(c.getContentResolver(),
Settings.System.WIFI_USE_STATIC_IP) == 1;
}
catch (SettingNotFoundException e) {
Log.i(TAG, "Settings not found (" + e.geMessage() + ")");
return false;
}
}
it returns true for both Android 2.x and also Android 4.x but in second case, changes are definitely not reflected in Wifi
I found this a little hardcoded solution but it didn't work as expected.
Is there anyone that faced against same problem?
I'll be glad for any working solution and also for rooted devices (maybe some command in linux) since it's easy to check status of whether cellphone is rooted or not.
Thanks in advance.
Now i can say: "I caught it"
I spent many days by looking for better, cleaner and for 100% working solution how actual (now i guess currently only one solution) solution with reflection is. But without result.
So i tried again to use mentioned solution with reflection here:
and suprisingly it works! And now i know what i was missing. So everyone who tried this solution on device with API
greater than 10 (sice Honeycomb) make sure you called:
wifiManager.saveConfiguration();
it's not enough to call only
wifiManager.updateNetwork(wifiConfiguration);
bacause changes (also made via reflection) won't be permanently saved to specific WifiConfiguration.
So now it works as expected and now a little summary:
Android 1.x and 2.x doesn't provide solution for setting static ip address per SSID (only for actual connected network) so simple working solution is to use ContentResolver and write data to System Settings via:
Settings.System.putInt(resolver, "wifi_use_static_ip", 1); // enabling static ip
Settings.System.putInt(resolver, "wifi_use_static_ip", 0); // enabling DHCP
Since Android 3.x it's possible to set static ip address per SSID hence solution for lower versions of Android OS will not working.
Currently there is no API
for this purpose so solution with reflection linked above is only one that actually works.
Don't forget to change setGateway()
for Android 3.x (also mentioned in origin thread)
Finally since if someone want to have application for setting static ip adress for Android 3.x and 4.x proper method takes netmask prefix as int and not full netmask so here is list of available netmasks with their prefixes.
Hope it helps.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With