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?
You can assign these static IP addresses on the device itself—using, say, Windows' network settings on each computer—or you can do it at the router level. Doing it at the router level is called assigning a DHCP reservation, though many people (and even some routers) still refer to it as a "static IP address."
Right-click on the network adapter you want to assign an IP address and click Properties. Highlight Internet Protocol Version 4 (TCP/IPv4) then click the Properties button. Now change the IP, Subnet mask, Default Gateway, and DNS Server Addresses. When you're finished click OK.
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?
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