コード例 #1
0
        public static void ExecuteNetworkCommand(ExecuteNetworkDelegate command, object connManager)
        {
#if Android
            if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
            {
                command();
                return;
            }
            Android.Net.Network             bindNetwork         = null;
            Android.Net.ConnectivityManager connectivityManager = connManager as Android.Net.ConnectivityManager;
            // ReSharper disable once UseNullPropagation
            if (connectivityManager != null)
            {
                Android.Net.Network[] networks = connectivityManager.GetAllNetworks();
                if (networks != null)
                {
                    foreach (Android.Net.Network network in networks)
                    {
                        Android.Net.NetworkInfo         networkInfo         = connectivityManager.GetNetworkInfo(network);
                        Android.Net.NetworkCapabilities networkCapabilities = connectivityManager.GetNetworkCapabilities(network);
                        // HasTransport support started also with Lollipop
                        if (networkInfo != null && networkInfo.IsConnected &&
                            networkCapabilities != null && networkCapabilities.HasTransport(Android.Net.TransportType.Wifi))
                        {
                            bindNetwork = network;
                            break;
                        }
                    }
                }
            }
            if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M)
            {
#pragma warning disable 618
                Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork;
                try
                {
                    Android.Net.ConnectivityManager.SetProcessDefaultNetwork(bindNetwork);
                    command();
                }
                finally
                {
                    Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork);
                }
#pragma warning restore 618
                return;
            }
            Android.Net.Network boundNetwork = connectivityManager?.BoundNetworkForProcess;
            try
            {
                connectivityManager?.BindProcessToNetwork(bindNetwork);
                command();
            }
            finally
            {
                connectivityManager?.BindProcessToNetwork(boundNetwork);
            }
#else
            command();
#endif
        }
コード例 #2
0
        public static void ExecuteNetworkCommand(ExecuteNetworkDelegate command, object connManager, bool checkEthernet = false)
        {
#if Android
            if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.Lollipop)
            {
                command();
                return;
            }
            Android.Net.Network             bindNetwork         = null;
            Android.Net.ConnectivityManager connectivityManager = connManager as Android.Net.ConnectivityManager;
            // ReSharper disable once UseNullPropagation
            if (connectivityManager != null)
            {
                Android.Net.Network[] networks = connectivityManager.GetAllNetworks();
                if (networks != null)
                {
                    foreach (Android.Net.Network network in networks)
                    {
                        Android.Net.NetworkInfo         networkInfo         = connectivityManager.GetNetworkInfo(network);
                        Android.Net.NetworkCapabilities networkCapabilities = connectivityManager.GetNetworkCapabilities(network);
                        // HasTransport support started also with Lollipop
                        if (networkInfo != null && networkInfo.IsConnected && networkCapabilities != null)
                        {
                            bool linkValid = false;
                            bool autoIp    = false;
                            Android.Net.LinkProperties linkProperties = connectivityManager.GetLinkProperties(network);
                            foreach (Android.Net.LinkAddress linkAddress in linkProperties.LinkAddresses)
                            {
                                if (linkAddress.Address is Java.Net.Inet4Address inet4Address)
                                {
                                    if (inet4Address.IsSiteLocalAddress || inet4Address.IsLinkLocalAddress)
                                    {
                                        linkValid = true;
                                        autoIp    = inet4Address.IsLinkLocalAddress;
                                        break;
                                    }
                                }
                            }

                            if (linkValid)
                            {
                                if (networkCapabilities.HasTransport(Android.Net.TransportType.Wifi))
                                {
                                    bindNetwork = network;
                                }
                                if (checkEthernet && networkCapabilities.HasTransport(Android.Net.TransportType.Ethernet))
                                {
                                    if (autoIp)
                                    {
                                        // prefer Ethernet auto ip
                                        bindNetwork = network;
                                        break;
                                    }

                                    if (bindNetwork == null)
                                    {
                                        // prefer Wifi
                                        bindNetwork = network;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            if (Android.OS.Build.VERSION.SdkInt < Android.OS.BuildVersionCodes.M)
            {
#pragma warning disable 618
                Android.Net.Network defaultNetwork = Android.Net.ConnectivityManager.ProcessDefaultNetwork;
                try
                {
                    Android.Net.ConnectivityManager.SetProcessDefaultNetwork(bindNetwork);
                    command();
                }
                finally
                {
                    Android.Net.ConnectivityManager.SetProcessDefaultNetwork(defaultNetwork);
                }
#pragma warning restore 618
                return;
            }
            Android.Net.Network boundNetwork = connectivityManager?.BoundNetworkForProcess;
            try
            {
                connectivityManager?.BindProcessToNetwork(bindNetwork);
                command();
            }
            finally
            {
                connectivityManager?.BindProcessToNetwork(boundNetwork);
            }
#else
            command();
#endif
        }