Can anybody tell me how can I set the static IP and gateway programmatically in Android 6?
Settings.System
is not working anymore and goolgle says WIFI_STATIC_IP
was deprecated in API level 17 and Use WifiManger
instead. Unfortunately I cannot find anything about it in WifiManger
and WifiConfiguration
classes.
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.
Solution:
The solution is to add the device as deviceOwner. This will allow for static IP to be set using the 5.x hacks as referenced by Mogi. 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:
will make it ready to be allowed to do its job.
Since there's no official API I have to come up with a solution by modifying samples from the following code snipet and this answer. This solution works on devices from Lollipop upwards.
Helper functions,
To use it,
Finally you need to add those permissions in the manifest,
I have successfully added and connected to open and PSK networks programmatically in my app (tried on devices running 5.1 as well as 6.0). However when I am trying this for enterprise network it does not work. I see that addNetwork() succeeds (returns positive net id) but when I look under Settings->Wi-Fi I don't see the SSID like I do for the other SSIDs I added. Anyone know why this would be? If I search the WiFiConfiguration list programmatically it does find the SSID. Here is the code I use: