Turning on wifi using WifiManager stops to work on Android 10
public boolean setWifiEnabled (boolean enabled)
This method was deprecated in API level 29.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi.Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect.
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
According to documentation, Apps
will not be able to turn Wi-Fi OFF/ON
anymore from Android-10 API level 29
[Until google provides an alternative solution].
For more information see official documentation.
And there is an issue 128554616 has been created in google issuetracker
about this.
Android WifiManager not enabling/disabling WiFi state
This API is no longer supported when targeting Android 10 or higher.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
Instead, you should use the Settings.Panel API to present a system UI allowing users to enable or disable Wi-Fi.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
startActivity(Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY))
}
Is it possible to programmatically enable wifi on Android 10 devices?
No, This is not possible to enable
or disable
Wi-Fi programmatically
from Android-10
API level 29
[Until google provides an alternative solution].
For applications targeting Build.VERSION_CODES.Q or above, this API will always return false and will have no effect.
If apps are targeting an older SDK ( Build.VERSION_CODES.P or below), they can continue to use this API.
There is an issue 128554616 which already has been created in google issuetracker
forum. You can see there for any updated info.
WifiManager not working on API 29 and higher using Android Studio
setWifiEnabled
is deprecated in Android 10+ due to security reasons
This method was deprecated in API level 29.
Starting with Build.VERSION_CODES#Q, applications are not allowed to enable/disable Wi-Fi. Compatibility Note: For applications targeting Build.VERSION_CODES.Q or above, this API will always fail and return false. If apps are targeting an older SDK (Build.VERSION_CODES.P or below), they can continue to use this API.
now your only way is to pass user to Settings GUI, in which Wi-Fi may be enabled manually only
Intent settingsIntent = new Intent(Settings.Panel.ACTION_INTERNET_CONNECTIVITY);
startActivityForResult(settingsIntent);
Android 10 device doesn't start discovery of wifi direct
As per documentation, you need the permission android.permission.ACCESS_FINE_LOCATION
instead of android.permission.ACCESS_COARSE_LOCATION
.
Is the Android Wifi-API really so broken on Android 10+?
Well just a half answer, but it might help anyway. Here is how I get the current SSID of the user (you need to hold the location permission):
fun updateSsid(networkCapabilities: NetworkCapabilities) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
currentSsid = (networkCapabilities.transportInfo as? WifiInfo)?.ssid?.trim('"')
}
}
wifiCallback = when {
Build.VERSION.SDK_INT >= Build.VERSION_CODES.S -> {
object: ConnectivityManager.NetworkCallback(FLAG_INCLUDE_LOCATION_INFO) {
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
updateSsid(networkCapabilities)
}
}
}
Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q -> {
object: ConnectivityManager.NetworkCallback() {
override fun onCapabilitiesChanged(network: Network, networkCapabilities: NetworkCapabilities) {
updateSsid(networkCapabilities)
}
}
}
else -> null
}
wifiCallback?.let {
fragment.lifecycle.addObserver(object: DefaultLifecycleObserver {
override fun onResume(owner: LifecycleOwner) {
connectivityManager.registerNetworkCallback(request, it)
}
override fun onPause(owner: LifecycleOwner) {
connectivityManager.unregisterNetworkCallback(it)
}
})
}
So basically aspects of your code need to check apis for the levels <10, 10, 11 and 12. What a mess.
This might be quiet handy if you want the user to pick the configured wifi:
fun showWifiDialog() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
try {
fragment.startActivity(Intent(android.provider.Settings.Panel.ACTION_WIFI))
} catch (e: Exception) {
// ignored
}
}
}
Android WiFi Network Switching Not Working - Need some ideas
Turns out this has been deprecated in the latest versions of Android.
so it's not possible to force the device over if you're targeting the latest SDK
Related Topics
How to Set Text Color of a Textview Programmatically
Background-Attachment: Fixed Interfering with Background-Size
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
Android Webview Cannot Render Youtube Video Embedded via Iframe
In Android Applications What Is The Role of "R.Java"
How Many Activities Vs Fragments
What Are Best Practices for Using Aes Encryption in Android
Opening a File from Assets Folder in Android
Android: How to Implement First Time Tutorial Like Go Launcher in My App
How to Add Apache Commons Collections in Android Studio (Gradle)
Kotiln: Pass Data from Adapter to Activity
How Open New Activity Clicking an Item in Listview
Android Attaching Multiple Adapters to One Adapter
Read Logs from All Apps on Android from Within an App for Android 4.2+
Send File to Server via Retrofit2 as Object
Error Inflating Class Android.Support.V4.View.Viewpager
Using Android Studio with Vuforia
Could Not Resolve All Dependencies for Configuration ':App:Debugapkcopy'