创建自定义的wifi设置 [英] Creating a custom wifi setup
问题描述
我只是想知道是否可以在应用程序中创建自定义Wifi界面,用户可以在其中输入他的Wifi连接,而不必启动导致android wifi设置的意图.我正在对此进行研究,但找不到有关在应用程序中进行自定义wifi设置的任何有用输入.
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 android wifi settings. I was researching this but couldn't find any useful input regarding making a custom wifi setup within the app.
startActivity( new Intent( Settings.ACTION_WIFI_SETTINGS ) );
这是不想要的...我想创建自己的wifi设置界面,用户可以在其中设置wifi配置文件,并使手机从应用程序内连接到网络.
This is not wanted... I want to crate my own wifi setup interface where the user can setup a wifi Profile and let the phone connect to a network from within an app.
感谢任何想法和帮助
推荐答案
这曾经是可能的,但现在已被API级别29(android 10)弃用.从android 10开始,您只能以编程方式将网络添加到建议列表.然后,用户会收到通知,但不会自动连接.因此,一旦在gradle文件中将 targetsdk
设置为29或更高版本,就无法为用户自动切换/连接到wifi.
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)
但是,您不能强制使用Wi-Fi开关.如果用户已经连接到另一个Wi-Fi,则他可能未连接到建议的网络.有关更多参考,请参见 Wi-Fi建议API .
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.
直到API级别28(android 9),使用WifiManager .
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) as WifiManager
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)
这篇关于创建自定义的wifi设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!