How to set a static IP for new WiFi configuration?

18,117

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?

Share:
18,117
Seraphim's
Author by

Seraphim's

On Android and .NET, Azure

Updated on June 24, 2022

Comments

  • Seraphim's
    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
    Zedot over 11 years
    try setting WIFI_USE_STATIC_IP to 0 in the beginning, and then set it to 1 in the end.
  • Seraphim's
    Seraphim's over 11 years
    did it works for you? Maybe it a problem related to my access point. however it's still on dhcp...
  • Zedot
    Zedot over 11 years
    btw, 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
    Seraphim's over 11 years
    yes it works... but I think Android do some "dirty" things that are not allowed to developers
  • Seraphim's
    Seraphim's over 11 years
    regarding WiFi problem on my old device... anytime I try to do something with wifi the system reboots!
  • Seraphim's
    Seraphim's over 11 years
    are you using this approach successfully?
  • Harpreet
    Harpreet about 11 years
    @Zedot: Are you having success on Android OS ver.3 & above using your this code?
  • Seraphim's
    Seraphim's about 11 years
    I'm compiling with 2.3, and used in Android 4.0.1: no success!