怎么在Android6.0上连接一个无密码wifi

使用网上的一些demo,连接总是会失败,比如下面两段代码都会失败:

       val config = WifiConfiguration()
        config.allowedAuthAlgorithms.clear()
        config.allowedGroupCiphers.clear()
        config.allowedKeyManagement.clear()
        config.allowedPairwiseCiphers.clear()
        config.allowedProtocols.clear()
        config.SSID = "\"" + SSID + "\""
        val tmpConfig = isExsits(ssid)
        if (tmpConfig != null) {
            mWifiManager.removeNetwork(tmpConfig.networkId)
        }
        if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
                //Password为空字符
            config.wepKeys[0] = "\"" + Password + "\""
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
            config.wepTxKeyIndex = 0
        }
        val config = WifiConfiguration()
        config.allowedAuthAlgorithms.clear()
        config.allowedGroupCiphers.clear()
        config.allowedKeyManagement.clear()
        config.allowedPairwiseCiphers.clear()
        config.allowedProtocols.clear()
        config.SSID = "\"" + SSID + "\""
        val tmpConfig = isExsits(ssid)
        if (tmpConfig != null) {
            mWifiManager.removeNetwork(tmpConfig.networkId)
        }
        if (Type == WifiCipherType.WIFICIPHER_NOPASS) {
            config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE)
        }

在manifest中声明user-permission了吗

申明了两个权限: