I want connect with specific WiFi network.Broadcast receiver get called only when we add correct password.And if user add wrong password than I unable to check password is valid or not.Because not getting called any callback method for this. This code is for android version 10 and above
Code for connect with wifi
@RequiresApi(Build.VERSION_CODES.Q)
private fun connectUsingNetworkSuggestion(ssid: String, password: String, securityType: String) {
var wifiManagerSugg = applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager
var wifiNetworkSuggestion:WifiNetworkSuggestion
if(password.isEmpty()){
wifiNetworkSuggestion = WifiNetworkSuggestion.Builder()
.setSsid(ssid)
//.setWpa2Passphrase(password)
.setIsAppInteractionRequired(true)
.setIsUserInteractionRequired(true)
.build()
}else{
wifiNetworkSuggestion = WifiNetworkSuggestion.Builder()
.setSsid(ssid)
.setWpa2Passphrase(password)
.setIsAppInteractionRequired(true)
.setIsUserInteractionRequired(true)
.build()
}
// Optional (Wait for post connection broadcast to one of your suggestions)
val intentFilter =
IntentFilter(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)
//boradcast receiver
registerReceiver(broadcastReceiver, intentFilter)
if(this::lastSuggestedNetwork.isInitialized){
val status = wifiManagerSugg.removeNetworkSuggestions(listOf(lastSuggestedNetwork))
Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")
}
//disconnect previous wifi access point
//wifiManagerSugg.disconnect()
//Thread.sleep(1000)
wifiManagerSugg.removeNetworkSuggestions(listOf())
val suggestionsList = listOf(wifiNetworkSuggestion)
var status = wifiManagerSugg.addNetworkSuggestions(suggestionsList)
Log.i("WifiNetworkSuggestion", "Adding Network suggestions status is $status")
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_ERROR_ADD_DUPLICATE) {
Toast.makeText(this, "Suggestion Update Needed", Toast.LENGTH_LONG).show()
// showToast("Suggestion Update Needed")
status = wifiManagerSugg.removeNetworkSuggestions(suggestionsList)
Log.i("WifiNetworkSuggestion", "Removing Network suggestions status is $status")
//disconnect previous wifi access point
// wifiManagerSugg.disconnect()
//Thread.sleep(1000)
wifiManagerSugg.removeNetworkSuggestions(listOf())
status = wifiManagerSugg.addNetworkSuggestions(suggestionsList)
}
if (status == WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
lastSuggestedNetwork = wifiNetworkSuggestion
lastSuggestedNetworkSSID = ssid
// showToast("Suggestion Added")
Toast.makeText(this, "Suggestion Added", Toast.LENGTH_LONG).show()
}
if (status != WifiManager.STATUS_NETWORK_SUGGESTIONS_SUCCESS) {
// do error handling here
Toast.makeText(this,"Something went wrong",Toast.LENGTH_LONG).show()
}
}
val broadcastReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
if (intent.action.equals(WifiManager.ACTION_WIFI_NETWORK_SUGGESTION_POST_CONNECTION)) {
// dialog.dismiss()
}
}
}
question from:
https://stackoverflow.com/questions/65934570/is-there-any-way-to-find-that-wifi-authentication-has-failed 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…