androidkotlinandroid-wifiandroid-11android-connectivitymanager

Android ConnectivityManager.requestNetwork() timed out when connecting to a hidden network


I'm trying to connect a hide SSID which is SAE over ConnectivityManager. But the result is over time. The application can't connect to SSID. I'm trying to change the SSID to not hide. It works well.

Here is the connect fun

    @RequiresApi(Build.VERSION_CODES.R)
    fun connectSae(ssid: String, password: String) {
        val suggestion = WifiNetworkSpecifier.Builder().setSsid(ssid).setWpa3Passphrase(password)
            .setIsHiddenSsid(if (hide_ssid.equals(true)) true else false).build()
        val networkRequest = NetworkRequest.Builder().addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
            .setNetworkSpecifier(suggestion).build()
        // mCm?.registerNetworkCallback(networkRequest, mNetworkCallback)
        mCm?.requestNetwork(networkRequest, mNetworkCallback)
    }

Here are the intent params

        if (intent.getBooleanExtra("hide_ssid", false)) {
            // am start -n com.example.wifiConnect/.MainActivity -e ssid SH-AP-006_5G -e passwd 12345678 --ez hide_ssid true -e type SAE
            hide_ssid = intent.getBooleanExtra("hide_ssid", false)
            Log.d(TAG, "intent hide_ssid $hide_ssid")
        }
        if (intent.hasExtra("type")) {
            type = intent.getStringExtra("type").toString()
            Log.d(TAG, "intent type $type")
        }

Here are the global variables

    companion object {
        const val TAG = "MainActivity"

        @SuppressLint("StaticFieldLeak")
        lateinit var mContext: Context

        lateinit var mWifiManager: WifiManager
        var mConfig: WifiConfiguration? = null

        // val wifiConfig = mWifiManager.configuredNetworks
        lateinit var mCm: ConnectivityManager
        lateinit var myWifiInfo: WifiInfo

        // val mBroadcastReceiver = WifiScanBroadcase()
        lateinit var scanResult: List<ScanResult>
        var hide_ssid = false
        var type = ""
        var wifi_action = ""
        lateinit var SECURITY_TYPE: String
        lateinit var mNetworkCallback: NetworkCallback
    }

Initiate in onCreate

mContext = getApplicationContext()
        mWifiManager = mContext.getSystemService(WIFI_SERVICE) as WifiManager
        mCm = mContext.getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager
        mNetworkCallback = object : ConnectivityManager.NetworkCallback() {
            override fun onUnavailable() {
                super.onUnavailable()
            }

            override fun onLosing(network: Network, maxMsToLive: Int) {
                super.onLosing(network, maxMsToLive)

            }

            override fun onAvailable(network: Network) {
                super.onAvailable(network)
                mCm?.bindProcessToNetwork(network)
            }

            override fun onLost(network: Network) {
                super.onLost(network)

            }
        }

How to use ConnectivityManager to connect a hide SSID more stable?


Solution

  • I find another way to connect hide wpa3 stably

    setup configure like this

    when (SECURITY_TYPE) {
                "PSK" -> {
                    config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_PSK);
                    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN)
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK)
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP)
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP)
                    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP)
                    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP)
                    config.preSharedKey = "\"" + password + "\""
                    Log.d(TAG, "Config PSK set done")
                }
                "SAE" -> {
                    Log.d(TAG, mWifiManager.isWpa3SaeSupported.toString())
                    Log.d(TAG, mWifiManager.isWpa3SuiteBSupported.toString())
                    Log.d(TAG, mWifiManager.isEnhancedOpenSupported.toString())
                    config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_SAE);
                    config.allowedGroupManagementCiphers.clear();
                    config.allowedSuiteBCiphers.clear();
                    config.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.SAE);
                    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
                    config.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.GCMP_256);
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.GCMP_256);
                    config.preSharedKey = "\"" + password + "\""
                    // requirePmf = true;
                    Log.d(TAG, "Config SAE set done")
                }
                "WEP" -> {
                    val length = password.length
                    if (length == 10 || length == 26 || length == 58 && Regex("[0-9A-Fa-f]*").containsMatchIn(password)) {
                        config.wepKeys[0] = password
                    } else {
                        config.wepKeys[0] = "\"" + password + "\""
                    }
                    config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_WEP);
                    config.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED)
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40)
                    config.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104)
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
                    config.wepTxKeyIndex = 0
                    config.status = WifiConfiguration.Status.ENABLED
                    Log.d(TAG, "Config WEP set done")
                }
                "NONE" -> {
                    config.setSecurityParams(WifiConfiguration.SECURITY_TYPE_OPEN);
                    config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
                    config.wepTxKeyIndex = 0;
                    config.preSharedKey = null;
                    Log.d(TAG, "Config NONE set done")
                }
    

    wifiManager add and enable this network

    val padWifiNetwork = createWifiConfig(ssid, passwd, type)
    
    var netId = mWifiManager.addNetwork(padWifiNetwork)
    if (netId == -1) netId = mWifiManager.configuredNetworks?.let {
                    it.firstOrNull { it.SSID.trim('"') == ssid.trim('"') }?.networkId ?: -1
                }!!
    Log.d(TAG, "netId $netId")
    mWifiManager.enableNetwork(netId, true)
    

    This is work for me