gpt4 book ai didi

android - 在 Android (API 30) 中以编程方式连接到特定的 wifi 网络

转载 作者:行者123 更新时间:2023-12-02 13:26:38 25 4
gpt4 key购买 nike

宗旨
尝试在没有任何人为干预(用户名和密码除外)的情况下连接或切换到特定的 WiFi 网络。
代码 fragment
MainActivity.kt
类 MainActivity : AppCompatActivity() {

private var lastSuggestedNetwork:WifiNetworkSuggestion? = null
var wifiManager:WifiManager? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

wifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager
val button = findViewById<Button>(R.id.button) // Just a button in the layout file
button.setOnClickListener(View.OnClickListener {
wifiManager!!.disconnect()
connectUsingNetworkSuggestion(ssid = "AndroidWifi", password ="")
wifiManager!!.reconnect()
})
}

private fun connectUsingNetworkSuggestion(ssid: String, password: String) {
val wifiNetworkSuggestion = WifiNetworkSuggestion.Builder()
.setSsid(ssid)
.setWpa2Passphrase(password)
.build()
val intentFilter =
IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION);

val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (!intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
return
}
showToast("Connection Suggestion Succeeded")
}
}


registerReceiver(broadcastReceiver, intentFilter)

lastSuggestedNetwork?.let {
val status = wifiManager!!.removeNetworkSuggestions(listOf(it))
Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")
}
val suggestionsList = listOf(wifiNetworkSuggestion)

var status = wifiManager!!.addNetworkSuggestions(suggestionsList)
Log.i("WifiNetworkSuggestion", "Adding Network suggestions status is $status")
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
showToast("Suggestion Update Needed")
status = wifiManager!!.removeNetworkSuggestions(suggestionsList)
Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")
status = wifiManager!!.addNetworkSuggestions(suggestionsList)
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
lastSuggestedNetwork = wifiNetworkSuggestion
showToast("Suggestion Added")
}
}

private fun showToast(s: String) {
Toast.makeText(applicationContext, s, Toast.LENGTH_LONG).show()
}
}
AndroidManifest.xml 权限
 <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
使用此代码,设备可以连接到 WiFi 网络,但需要手动关闭和打开 WiFi。必须有更好的方法来连接或切换到特定的 WiFi 网络,而无需任何手动操作。
当前流程:
  • 运行应用程序并单击主屏幕上的按钮以连接到 WiFi。
  • 转到设置,禁用和启用 WiFi
  • 设备已连接到所需的 WiFi 网络

  • 输出
    /image/zQ7Bo.png
    Intent :
    摆脱第 2 步/以编程方式执行
    引用:引用: Is it possible to add a network configuration on Android Q?
    为了清楚起见,此代码 fragment 中提到的 ssid 和密码是用于默认 AVD,只需将其更改为任何其他 WiFi 网络的 SSID 和密码,它可以在物理设备上使用。我在 Pixel 3XL 上尝试过同样的问题。

    最佳答案

    您可以使用新的 Settings.Panel 轻轻地要求您的用户打开 wifi。 (API 29+) 或旧的 Settings API。此外,您可以使用 startActivityForResult()检查用户是否确实从设置中打开了 Wifi。

    if(!wifiManager.isWifiEnabled()) {
    if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
    startActivity(new Intent(Settings.Panel.ACTION_WIFI));
    } else {
    // Use a full page activity - if Wifi is critcal for your app
    startActivity(new Intent(Settings.ACTION_WIFI_SETTINGS));
    // Or use the deprecated method
    wifiManager.setWifiEnabled(true)
    }
    }

    关于android - 在 Android (API 30) 中以编程方式连接到特定的 wifi 网络,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63794081/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com