Creating A Custom Wifi Setup
I was just wondering if it is possible to make a custom Wifi interface within an app, where the user can input his Wifi connection instead of starting an intent which leads to the
Solution 1:
This used to be possible but is now deprecated with API level 29 (android 10). Starting with android 10, you can only add a network programmatically to a suggestion list. The user then gets a notification but isn't automatically connected.
So once you set your targetsdk
in you gradle file to 29 or higher, you can't automatically switch/connect to a wifi for the user.
// This only works with Android 10 and up (targetsdk = 29 and higher):import android.net.wifi.WifiManager
import android.net.wifi.WifiNetworkSuggestion
...
val wifiManager = getSystemService(WIFI_SERVICE) as WifiManager
val networkSuggestion = WifiNetworkSuggestion.Builder()
.setSsid("MyWifi")
.setWpa2Passphrase("123Password")
.build()
val list = arrayListOf(networkSuggestion)
wifiManager.addNetworkSuggestions(list)
However, you can't force a Wi-Fi switch. If the user is already connected to another Wi-Fi, he might not connect to the suggested network. See Wi-Fi suggestion API for further reference.
Up until API level 28 (android 9), this was possible with the WifiManager.
// This code works only up until API level 28 (targetsdk = 28 and lower):import android.net.wifi.WifiConfiguration
import android.net.wifi.WifiManager
...
val wifiManager = getSystemService(WIFI_SERVICE) asWifiManager
val wifiConfiguration =WifiConfiguration()
wifiConfiguration.SSID="\""+"MyWifi"+"\""
wifiConfiguration.preSharedKey ="\""+"123Password"+"\""// Add a wifi network to the system settings
val id = wifiManager.addNetwork(wifiConfiguration)
wifiManager.saveConfiguration()
// Connect
wifiManager.enableNetwork(id, true)
Post a Comment for "Creating A Custom Wifi Setup"