How to set static IP for new WiFi configuration?

Again stuck in the same problem.

I found around that we can set static system settings as follows:

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 without success!

I don’t understand when to set these settings?

Do I have to do this before creating a wifi configuration or after saving a Wi-Fi configuration or even before or after activating it?

However, I tried all possible cases on my part, and when I check the Android WiFi settings, I see that it is still on DHCP.

The previous question, i.e. How to configure a static IP address, netmask, gateway programmatically on Android 3.x or 4.x , completely ruined my Android device and now it can’t switch ON its WiFi anymore.

I also tried a static IP address on my HTC phone and did not succeed, its always in DHCP mode!

Do I need to call the reconnect command? If so, how?

+5
source share
2 answers

After more than a year, I refuse to install a static IP (or DHCP, DNS, ...). It is simply impossible, or, better, it is not allowed (from an arbitrary application).

Someone says:

“You can use NDK - this gives you low-level access to Linux for Android. Warning: don't expect this to be documented or supported. They may even ban you from the Android Market (I know that I))

For those who want to have some features with NDK, here is the link:

http://developer.android.com/tools/sdk/ndk/index.html

Good luck, and give some feedback if you find something interesting!

+2
source

I think it should look like this to you:

 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 your device had, you can try switching WIFI programmatically. This post may be useful: How to programmatically disable WiFi on an Android device?

+4
source

Source: https://habr.com/ru/post/1433880/


All Articles