Xamarin Dependency Services does not work with higher Android version

90
May 01, 2022, at 10:40 AM

I have programmed a Xamarin app that uses a native Android function through Dependency Service. Specifically it is about WifiManager and the method SetWifiEnabled() which is for turning off Wifi. Using Android 7.1 emulator with API 25 everything works and I can turn off the wifi. With emulator with Android version 11 and API 30 the wifi is not turned off which I assume that the method SetWifiEnabled() does not work. The wifi is not turned off. Does anyone know the reason for this ? Thanks a lot

Here is the Code from Dependency Service:

[assembly: Dependency(typeof(GetWifimanager))]
namespace WifiSwitch.Droid
{
    public class GetWifimanager : IWifiManager
    {
        [Obsolete]
        public bool CheckWifi()
        {
            var wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);
            return wifiManager != null && wifiManager.SetWifiEnabled(false);
            
        }
    }
}
Answer 1

according to the docs

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.

Rent Charter Buses Company
READ ALSO
Android Tv Nvidia Shield Pro crash after upgrading to SHIELD Experience Upgrade 9.0

Android Tv Nvidia Shield Pro crash after upgrading to SHIELD Experience Upgrade 9.0

I am facing an issue on my Android Tv app since Nvidia Shield Pro upgrade their firmware to SHIELD Experience Upgrade 90

76
What are all possible permissions taken by apps?

What are all possible permissions taken by apps?

There are two types of app permissions in case of android apps

140
how to use bottomNavigationBar while using ListView.builder

how to use bottomNavigationBar while using ListView.builder

I want to use bottomNavigationBar with listview, I tried each one of them separately and they work fine, but when I use them together, the bottomNavigationBar doesn't work, you can press the icons but nothing happens

110
How can i pause media player when the device is locekd

How can i pause media player when the device is locekd

Hello right now when I'm playing the media player and if the device is locked it still continue playing the audio I want that when the screen is off the media player to pause also when the app is running in the background ( onPause )

94