Android WiFi connection programmatically

前端 未结 4 567
没有蜡笔的小新
没有蜡笔的小新 2021-01-30 19:16

Do you have any idea how to establish a wifi connection with sending password in my android application?

4条回答
  •  情歌与酒
    2021-01-30 19:28

    Give your ssid and password

        fun connectToWifi(ssid: String, password: String) {
        val connManager = context!!.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI)
        val wifiConfiguration = WifiConfiguration()
        wifiConfiguration.SSID = String.format("\"%s\"", ssid)
        wifiConfiguration.preSharedKey = String.format("\"%s\"", password)
        wifiManager = context!!.getSystemService(Context.WIFI_SERVICE) as WifiManager
        val netId: Int = wifiManager.addNetwork(wifiConfiguration)
        wifiManager.disconnect()
        wifiManager.enableNetwork(netId, true)
        wifiManager.reconnect()
        val config = WifiConfiguration()
        config.SSID == "\"\"" + ssid + "\"\""
        config.preSharedKey == "\"\"" + password + "\"\""
        wifiManager.addNetwork(config)
    }
    

提交回复
热议问题