How to set a static IP for new WiFi configuration?
Solution 1
After more than a year, I give up with setting a static IP (or DHCP, DNS, ...). Simply it's not possible, or, better, it's not allowed (from an arbitrary application).
Someone says:
"You could use NDK - this gives you low-level access to Linux under Android. Warning: don't expect this to be documented or supported. They might even ban you from Android Market (I know I would)"
For those who want to have some expriences with NDK, here is the a link:
http://developer.android.com/tools/sdk/ndk/index.html
Good luck, and give some feedback if you find something interesting!
Solution 2
I think you it should look like:
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_IP,"192.168.1.15");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_DNS1, "192.168.1.1");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.1.1");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_STATIC_NETMASK, "255.255.255.0");
android.provider.Settings.System.putString(getContentResolver(),android.provider.Settings.System.WIFI_USE_STATIC_IP, "1");
And don't forget the manifest:
<uses-permission android:name="android.permission.WRITE_SETTINGS"></uses-permission>
Regarding the WiFi problem that your device had, you can try switching the WIFI on programmatically. This post might be helpful: How to programmatically turn off WiFi on Android device?
Comments
-
Seraphim's almost 2 years
Again stuck on the same problem.
I have found around that we can set static system settings like this:
System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1"); // to define it use static ip's System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP,"192.168.1.15"); System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_NETMASK,"255.255.255.0"); System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_DNS1,"192.168.1.1"); System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY,"192.168.1.1");
But No Success!
I don't understand that when to set these settings?
Should I do it before the wifi configuration creation or after saving the wifi configuration or even before activating it or after it?
However, I have tried the all possible cases from my side and when I check Android WiFi settings, I see it's still on DHCP.
A previous question i.e. How to configue a static IP address, netmask, gateway programmatically on Android 3.x or 4.x has completely ruined my android device and now it can't switch ON its WiFi anymore.
I also tried static IP on my HTC phone and no success, its always in DHCP mode!
Do I need to call a "reconnect" command? If yes, then in which way?
-
Zedot over 11 yearstry setting WIFI_USE_STATIC_IP to 0 in the beginning, and then set it to 1 in the end.
-
Seraphim's over 11 yearsdid it works for you? Maybe it a problem related to my access point. however it's still on dhcp...
-
Zedot over 11 yearsbtw, have you tried connecting manually to the wifi? I mean, going to settings, wifi and setting the ip, mask, gateway, etc. there? If it does not work there, maybe the problem is not on the device...
-
Seraphim's over 11 yearsyes it works... but I think Android do some "dirty" things that are not allowed to developers
-
Seraphim's over 11 yearsregarding WiFi problem on my old device... anytime I try to do something with wifi the system reboots!
-
Seraphim's over 11 yearsare you using this approach successfully?
-
Harpreet about 11 years@Zedot: Are you having success on Android OS ver.3 & above using your this code?
-
Seraphim's about 11 yearsI'm compiling with 2.3, and used in Android 4.0.1: no success!