Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
144 views
in Technique[技术] by (71.8m points)

android - Is there any way to find that Wifi Authentication has failed?

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...