I have checked in Stack Overflow question API for configuring static IP addresses in an Android application.
It works until Android 2.3. However, there is no luck on a higher API level. For example, I put the setting
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_USE_STATIC_IP, "1");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_IP, "192.168.0.100");
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_STATIC_DNS1, "192.168.0.254");
android.provider.Settings.System.putString(getContentResolver(), android.provider.Settings.System.WIFI_STATIC_GATEWAY, "192.168.0.254");
But I go back to check by:
Setting --> Wi-Fi --> Long Press Access Point SSID --> Modify Network --> check Show advanced options
The IP Settings
field is still stated DHCP
but not Static
.
It is true that I can use android.provider.Settings.System.getString()
to get back what I set. It prove that the setting is saved somewhere but the system just ignore it.
The system uses the setting other than android.provider.Settings.System
on Android 3.x and 4.x as the setting is set per Access Point SSID. Can I modify the setting on one SSID just like how it works on Android 2.3?
@Robin
Thanks your solution works fine for me on My Nexus device running on Android M 6.0.1.
I have replaced the
// apply the configuration change boolean result = wm.updateNetwork(wifiConf) != -1; //apply the setting if(result) result = wm.saveConfiguration(); //Save it if(result) wm.reassociate(); // reconnect with the new static IP
with the following
For Android 5.1.0
As a kotlin extension of
WifiConfiguration
, working on Android 5+I realise that there is no API on 3.x or 4.x for those setting per SSID. Therefore, I checked out the source code and found out that the configuration of each SSID is stored in
android.net.wifi.WifiConfiguration
which is gotten fromandroid.net.wifi.WifiManager
.In the below code,
IpAssignment
is an Enum, eitherSTAIC
,DHCP
orNONE
. AndlinkProperties
is the object store IP address, gateway, DNS, etc...linkAddress
is IP address and its netmask as prefixLength (how many bit 1 in netmask).mRoutes
isArrayList
ofRouteInfo
that can indicate gateway.mDnses
isArrayList
ofInetAddress
for DNS.Firstly, get the current configuration using
WifiConfiguration
SSIDAs the
IpAssignment
andlinkProperties
are hidden, the object can be gotten from reflection.The following method can set the declared IP address setting on SSID WifiConfiguration:
After that, I can set setting and update
WifiConfiguration
for this SSID.Edit: Sorry for I don't check for Android 3.x device that have silmilar UI with Android 4.x. In Android 3.x, the gateway is storted in
mGateways
oflinkProperties
.mGateways
isArraylist
of typeInetAddress
. Therefore, following should work in Android 3.x.Edit2: The methods
setIpAddress
,setGateway
,setDNS
should be inputted asInetAddress
type.If you try to use the solution for Android 5.x on 6.x your application will be denied doing this. To do this you proabably need to root your device and make the application the device owner.
I've dug some into the problem and my findings is that code that used to work for Andrdoi 5.x might work if the application is set to be the device owner.
A good example of how this is done is using the example found here:
https://github.com/googlesamples/android-DeviceOwner/
Using adb shell and running the command:
dpm set-device-owner com.example.android.deviceowner/.DeviceOwnerReceiver
will make the application device owner and it is possible to set static IP.
For Android 5.0+ a WIP solution. It does not yet work for some reason. Comments welcome.