Exemple #1
0
        private static Radio?GetRadiosWiFiOrCellular(RadioKind radioKind)
        {
            var context = Android.App.Application.Context;

            var pkgManager = context.PackageManager;

            if (pkgManager is null)
            {
                // required by #nullable
                return(null);
            }

            string systemFeature;

            if (radioKind == RadioKind.WiFi)
            {
                systemFeature = Android.Content.PM.PackageManager.FeatureWifi;
            }
            else
            {
                systemFeature = Android.Content.PM.PackageManager.FeatureTelephony;
            }

            if (!pkgManager.HasSystemFeature(systemFeature))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): this device doesn't have any interface of this kind");
                }
                return(null);
            }

            var radio = new Radio();

            radio.Kind = radioKind;
            if (radioKind == RadioKind.WiFi)
            {
                radio.Name = "Wi-Fi";                  // name as in UWP
            }
            else
            {
                radio.Name = "Cellular";                  // I have "Cellular 6" (Lumia 532), but maybe "6" doesn't mean anything
            }

            if (!Windows.Extensions.PermissionsHelper.IsDeclaredInManifest(Android.Manifest.Permission.AccessNetworkState))
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): no 'AccessNetworkState' permission, check app Manifest");
                }
                radio.State = RadioState.Unknown;
                return(radio);
            }

            var sysService = context.GetSystemService(Android.Content.Context.ConnectivityService);

            if (sysService is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): GetSystemService returns null, assuming no radio");
                }
                return(null);
            }

            var connManager = (Android.Net.ConnectivityManager)sysService;

            if (connManager is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadiosWiFiOrCellular(" + radioKind.ToString() + "): Android.Net.ConnectivityManager is null, assuming no WiFi radio");
                }
                return(null);
            }

            if (Android.OS.Build.VERSION.SdkInt > Android.OS.BuildVersionCodes.LollipopMr1)
            {
                // since API 23
                var radiostate = GetRadioStateForNet(connManager, radioKind);
                if (radiostate is null || !radiostate.HasValue)
                {
                    // detailed Log entry already done in GetRadioStateForNet
                    return(null);
                }

                if (radiostate.Value)
                {
                    radio.State = RadioState.On;
                }
                else
                {
                    radio.State = RadioState.Off;
                }

                return(radio);
            }
            else
            {
                // for Android API 1 to 28 (deprecated in 29)
#pragma warning disable CS0618 // Type or member is obsolete
                var netInfo = connManager.ActiveNetworkInfo;
                if (netInfo is null)
                {
                    radio.State = RadioState.Off;
                }
                else
                {
                    radio.State = RadioState.Off;
                    if (radioKind == RadioKind.WiFi)
                    {
                        if (netInfo.Type == Android.Net.ConnectivityType.Wifi ||
                            netInfo.Type == Android.Net.ConnectivityType.Wimax)
                        {
                            radio.State = RadioState.On;
                        }
                    }
                    else
                    {
                        if (netInfo.Type == Android.Net.ConnectivityType.Mobile)
                        {
                            radio.State = RadioState.On;
                        }
                    }
#pragma warning restore CS0618 // Type or member is obsolete
                }
                return(radio);
            }
        }
Exemple #2
0
        private static bool?GetRadioStateForNet(Android.Net.ConnectivityManager connManager, RadioKind radioKind)
        {
            var activeNetwork = connManager.ActiveNetwork;

            if (activeNetwork is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Warning))
                {
                    typeof(Radio).Log().Debug("GetRadioStateForNet(): ActiveNetwork is null, assuming no radio");
                }
                return(null);
            }

            var netCaps = connManager.GetNetworkCapabilities(activeNetwork);

            if (netCaps is null)
            {
                if (typeof(Radio).Log().IsEnabled(Microsoft.Extensions.Logging.LogLevel.Debug))
                {
                    typeof(Radio).Log().Debug("GetRadioStateForNet(): GetNetworkCapabilities is null, assuming no radio");
                }
                return(null);
            }

            if (radioKind == RadioKind.WiFi)
            {
                return(netCaps.HasTransport(Android.Net.TransportType.Wifi));
            }
            else
            {
                return(netCaps.HasTransport(Android.Net.TransportType.Cellular));
            }
        }