Connect to Wifi in Android Q programmatically
So, the solution for me is compile your app with targetSdkVersion 28.
and for connection to wifi use this function
connectToWifi(String ssid, String key)
it's just a workaround for the moment, waiting Google to publish a fix for this bug, for more information the issue reported to Google : issuetracker.google.com/issues/138335744
public void connectToWifi(String ssid, String key) {
Log.e(TAG, "connection wifi pre Q");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + key + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
Connect to WiFi Access Point programmatically with Android 10
I struggle couple days ago with wifi connection on Andrdoid Q, the link of my question in stackOverflow.
So, the solution is compile your app with targetSdkVersion 28.
and for connection to wifi use this function :
public void connectToWifi(String ssid, String key) {
Log.e(TAG, "connection wifi pre Q");
WifiConfiguration wifiConfig = new WifiConfiguration();
wifiConfig.SSID = "\"" + ssid + "\"";
wifiConfig.preSharedKey = "\"" + key + "\"";
int netId = wifiManager.addNetwork(wifiConfig);
if (netId == -1) netId = getExistingNetworkId(wifiConfig.SSID);
wifiManager.disconnect();
wifiManager.enableNetwork(netId, true);
wifiManager.reconnect();
}
Connect To Wifi Android Q
This is my code :
val wifiManager = applicationContext.getSystemService(WIFI_SERVICE) as WifiManager?
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) {
try {
val wifiConfig = WifiConfiguration()
wifiConfig.SSID = "\"" + yourSsid + "\""
wifiConfig.preSharedKey = "\"" + password + "\""
val netId = wifiManager!!.addNetwork(wifiConfig)
wifiManager.disconnect()
wifiManager.enableNetwork(netId, true)
wifiManager.reconnect()
if (isWifiConnected("\"" + deviceId + "\"")) {
doSomethingHere()
}
} catch (e: Exception) {
e.printStackTrace()
}
} else {
val wifiNetworkSpecifier = WifiNetworkSpecifier.Builder()
.setSsid( yourSsid!! )
.setWpa2Passphrase(password)
.build()
val networkRequest = NetworkRequest.Builder()
.addTransportType(NetworkCapabilities.TRANSPORT_WIFI)
.setNetworkSpecifier(wifiNetworkSpecifier)
.build()
connectivityManager = Boron.instance.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager?
networkCallback = 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)
connectivityManager?.bindProcessToNetwork(network)
}
override fun onLost(network: Network) {
super.onLost(network)
}
}
connectivityManager?.requestNetwork(networkRequest,networkCallback)
}
Then in onDestroy
connectivityManager?.unregisterNetworkCallback(networkCallback)
Permissions needed :
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-feature android:name="android.permission.WRITE_SETTINGS"
android:required="false"/>
Related Topics
Preventing/Catching "Illegalargumentexception: Parameter Must Be a Descendant of This View" Error
How to Change Separator Color in Navigationview
Do Geofences Remain Active in Android After a Device Reboot
Android Sdk Cannot Be Found by Flutter
Android Development: Using Image from Assets in a Webview's HTML
Bitmap, Bitmap.Recycle(), Weakreferences, and Garbage Collection
Android: Rotate Image Without Loading It to Memory
Get the Last Picture Taken by User
How to Draw Outside the Bounds of an Android Canvas
Android Linkify Text - Spannable Text in Single Text View - as Like Twitter Tweet
Write_Secure_Settings Permission Error Even When Added in Manifest
Is Proguard.Cfg Needed for Library Projects
Outlook - Read Another User's Calendar
I Get the Error "Unreachable Statement" Return in Android
Does Setwidth(Int Pixels) Use Dip or Px
Gradle Error:Execution Failed for Task ':App:Processdebuggoogleservices'