5

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?

jww
  • 97,681
  • 90
  • 411
  • 885
Seraphim's
  • 12,559
  • 20
  • 88
  • 129
  • Normally the right solution is to leave your Android device alone (DHCP) and instead feed its MAC address into your DHCP server's configuration and have it feed back the "static" data. – mah Sep 12 '12 at 19:32
  • I see I can set a stati IP with android wifi settings... I really don't understand why google make these things so complicated. Anyway, I'm connecting to a very simple wifi microchip that can't handle feeds of mac address – Seraphim's Sep 12 '12 at 19:45

2 Answers2

4

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?

Community
  • 1
  • 1
Zedot
  • 41
  • 3
2

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!

Seraphim's
  • 12,559
  • 20
  • 88
  • 129