Home > Back-end >  Xamarin Dependency Services does not work with higher Android version
Xamarin Dependency Services does not work with higher Android version

Time:02-08

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);
            
        }
    }
}

CodePudding user response:

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.

  •  Tags:  
  • Related