Open noumanzoobiapps opened 2 years ago
Current implementations need you to remove capability internet. To do that, you need to fork this repo. However, I would not recommend that. To be honest, this library is a mess, it would be better to write your own library from scratch using Koltin.
Hope this code might help:
fun connectToWifi(ssid: String?, password: String?) { val specifier = WifiNetworkSpecifier.Builder() .setSsid(ssid!!) .setWpa2Passphrase(password!!) .build() val request = NetworkRequest.Builder() .addTransportType(NetworkCapabilities.TRANSPORT_WIFI) .setNetworkSpecifier(specifier) .build() val connectivityManager = getSystemService(CONNECTIVITY_SERVICE) as ConnectivityManager val networkCallback: NetworkCallback = object : NetworkCallback() { override fun onAvailable(network: Network) { // Connected to network Toast.makeText(this@WifiListActivity, "Connected to network", Toast.LENGTH_SHORT).show() connectivityManager.unregisterNetworkCallback(this) } override fun onUnavailable() { // Unable to connect to network Toast.makeText( this@WifiListActivity, "Unable to connect to network", Toast.LENGTH_SHORT ).show() connectivityManager.unregisterNetworkCallback(this) } } connectivityManager.requestNetwork(request, networkCallback) }
this error occur android 10,11,12 version