Exemple #1
0
        public static IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    var info = manager.GetNetworkInfo(network);

                    if (info?.Type == null)
                    {
                        yield return(ConnectionType.Other);
                    }


                    yield return(GetConnectionType(info.Type));
                }
            }
            else
            {
                foreach (var info in manager.GetAllNetworkInfo())
                {
                    if (info?.Type == null)
                    {
                        yield return(ConnectionType.Other);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
        }
Exemple #2
0
        // TODO: Find better technqiue to ensure internet access
        bool IsNetworkConnected()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)
                                                      Application
                                                      .Context
                                                      .GetSystemService(Context.ConnectivityService);

            var networks = connectivityManager.GetAllNetworks();

            if (networks.Length > 0)
            {
                foreach (var network in networks)
                {
                    var networkInfo = connectivityManager.GetNetworkInfo(network);

                    if (networkInfo != null)
                    {
                        if (networkInfo.IsConnected)
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }
Exemple #3
0
        public static bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
                    foreach (var info in manager.GetAllNetworkInfo())
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }
        internal IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)global::Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    NetworkInfo info = null;
                    try
                    {
                        info = manager.GetNetworkInfo(network);
                    }
                    catch
                    {
                        //there is a possibility, but don't worry about it
                    }

                    if (info == null)
                    {
                        continue;
                    }

                    if (!info.IsAvailable)
                    {
                        yield return(ConnectionType.None);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
            else
            {
#pragma warning disable CS0618 // Type or member is obsolete
                foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                {
                    if (info == null)
                    {
                        continue;
                    }

                    if (!info.IsAvailable)
                    {
                        yield return(ConnectionType.None);
                    }

                    yield return(GetConnectionType(info.Type));
                }
            }
        }
Exemple #5
0
        public void Connect(string leonetId, string leonetPassword)
        {

            NetworkInfo activeConnection = _connectivityManager.ActiveNetworkInfo;

            bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

            if (isOnline)
            {
                // Networkのタイプを表示
                NetworkInfo.State activeState = activeConnection.GetState();
                System.Diagnostics.Debug.WriteLine(activeConnection.TypeName);

                // 全接続を取得して、それぞれの接続状態を確認
                // GetAllNetworks()は5.0以上でそれ以前はgetAllNetworkInfo()を使用
                if (Android.OS.Build.VERSION.SdkInt >= Android.OS.BuildVersionCodes.Lollipop)
                {
                    Network[] allNetworks = _connectivityManager.GetAllNetworks();
                    foreach (var network in allNetworks)
                    {
                        
                        NetworkInfo info = _connectivityManager.GetNetworkInfo(network);
                        var connect = info.IsConnectedOrConnecting ? "cennected" : "disconnected";
                        System.Diagnostics.Debug.WriteLine($"{info.TypeName} is {connect}");

                        if (info.Type == ConnectivityType.Wifi)
                        {
                            Authenticator.SetDefault(new BasicAuthenticator(leonetId, leonetPassword));
                            var leonetUrlString = "http://#GATEWAY#/login.cgi".Replace("#GATEWAY#", GetDefaultGateway());
                            var leonetUrl = new URL(leonetUrlString);
                            Task.Run(() =>
                            {
                                network.OpenConnection(leonetUrl);
                            });
                            
                        }

                    }
                }
                else
                {
                    NetworkInfo[] allNetworks = _connectivityManager.GetAllNetworkInfo();
                    foreach (var item in allNetworks)
                    {
                        var connect = item.IsConnectedOrConnecting ? "cennected" : "disconnected";
                        System.Diagnostics.Debug.WriteLine($"{item.TypeName} is {connect}");
                    }
                }
            }
        }
Exemple #6
0
        public override void OnReceive(Context context, Intent intent)
        {
            if (Android.OS.Build.VERSION.SdkInt < BuildVersionCodes.Lollipop)
            {
                //获得ConnectivityManager对象
                ConnectivityManager connMgr = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

                //获取ConnectivityManager对象对应的NetworkInfo对象
                //获取WIFI连接的信息
                NetworkInfo wifiNetworkInfo = connMgr.GetNetworkInfo(ConnectivityType.Wifi);
                //获取移动数据连接的信息
                NetworkInfo dataNetworkInfo = connMgr.GetNetworkInfo(ConnectivityType.Mobile);
                if (wifiNetworkInfo.IsConnected && dataNetworkInfo.IsConnected)
                {
                    Toast.MakeText(context, "WIFI已连接,移动数据已连接", ToastLength.Long).Show();
                }
                else if (wifiNetworkInfo.IsConnected && !dataNetworkInfo.IsConnected)
                {
                    Toast.MakeText(context, "WIFI已连接,移动数据已断开", ToastLength.Long).Show();
                }
                else if (!wifiNetworkInfo.IsConnected && dataNetworkInfo.IsConnected)
                {
                    Toast.MakeText(context, "WIFI已断开,移动数据已连接", ToastLength.Long).Show();
                }
                else
                {
                    Toast.MakeText(context, "WIFI已断开,移动数据已断开", ToastLength.Long).Show();
                }
                //API大于23时使用下面的方式进行网络监听
            }
            else
            {
                //获得ConnectivityManager对象
                ConnectivityManager connMgr = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

                //获取所有网络连接的信息
                Network[] networks = connMgr.GetAllNetworks();
                //用于存放网络连接信息
                StringBuilder sb = new StringBuilder();
                //通过循环将网络信息逐个取出来
                for (int i = 0; i < networks.Length; i++)
                {
                    //获取ConnectivityManager对象对应的NetworkInfo对象
                    NetworkInfo networkInfo = connMgr.GetNetworkInfo(networks[i]);
                    sb.Append(networkInfo.TypeName + " connect is " + networkInfo.IsConnected);
                }
                Toast.MakeText(context, sb.ToString(), ToastLength.Long).Show();
            }
        }
Exemple #7
0
        public static IEnumerable <ConnectionType> GetConnectionTypes(ConnectivityManager manager)
        {
            //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
            //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
            if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
            {
                foreach (var network in manager.GetAllNetworks())
                {
                    NetworkInfo info = null;
                    try
                    {
                        info = manager.GetNetworkInfo(network);
                    }
                    catch
                    {
                        //there is a possibility, but don't worry about it
                    }

                    if (info == null || !info.IsAvailable)
                    {
                        continue;
                    }

                    yield return(GetConnectionType(info.Type, info.TypeName));
                }
            }
            else
            {
                foreach (var info in manager.GetAllNetworkInfo())
                {
                    if (info == null || !info.IsAvailable)
                    {
                        continue;
                    }

                    yield return(GetConnectionType(info.Type, info.TypeName));
                }
            }
        }
        protected override void OnCreate(Bundle savedInstanceState)
        {
            AppCenter.Start("b17f9c9d-e90c-488f-8c4b-92ef3e305c0d", typeof(Analytics), typeof(Distribute));

            var versionInfo = Application.Context.ApplicationContext?.PackageManager?.GetPackageInfo(Application.Context.ApplicationContext.PackageName, 0);

            //var username = System.Security.Principal.WindowsIdentity.GetCurrent();

            SentryXamarin.Init(o =>
            {
                o.AddXamarinFormsIntegration();
                o.Dsn         = "https://[email protected]/5390642";
                o.Release     = $"TacControl@{versionInfo?.VersionName}:{versionInfo?.LongVersionCode}";
                o.Environment = //username == "Dedmen-PC\\dedmen" ? "Dev" :
                                "Alpha";
            });


            WifiManager wifiMgr = (WifiManager)ApplicationContext.GetSystemService(Context.WifiService);

            wifiLock = wifiMgr.CreateWifiLock(WifiMode.Full, "TacControl-udp");
            wifiLock.SetReferenceCounted(true);
            wifiLock.Acquire();
            castLock = wifiMgr.CreateMulticastLock("TacControl-udp");
            castLock.SetReferenceCounted(true);
            castLock.Acquire();


            ConnectivityManager conMgr = (ConnectivityManager)ApplicationContext.GetSystemService(Context.ConnectivityService);
            var stuff   = conMgr.GetAllNetworks();
            var wifiNet = stuff.FirstOrDefault(x => conMgr.GetNetworkInfo(x).Type == ConnectivityType.Wifi);

            if (wifiNet != null)
            {
                var res  = conMgr.BindProcessToNetwork(wifiNet);
                var info = conMgr.GetNetworkInfo(wifiNet);

                var connInfo = wifiMgr.ConnectionInfo;
            }


            //Networking.ConnectionInfo

            TabLayoutResource = Resource.Layout.Tabbar;
            ToolbarResource   = Resource.Layout.Toolbar;

            base.OnCreate(savedInstanceState);

            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            global::Xamarin.Forms.Forms.Init(this, savedInstanceState);

            Android.Views.Window window = Window;
            window.AddFlags(WindowManagerFlags.KeepScreenOn);
            window.AddFlags(WindowManagerFlags.Fullscreen);

            LoadApplication(new App((action) =>
            {
                TaskCompletionSource <object> tcs = new TaskCompletionSource <object>();
                bool isMain = MainThread.IsMainThread;

                RunOnUiThread(() =>
                {
                    try
                    {
                        action();
                        tcs.SetResult(null);
                    }
                    catch (Exception ex)
                    {
                        tcs.SetException(ex);
                    }
                });

                return(tcs.Task);
            }));
        }
Exemple #9
0
        public static bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var capabilities = manager.GetNetworkCapabilities(network);

                            if (capabilities == null)
                            {
                                continue;
                            }

                            //check to see if it has the internet capability
                            if (!capabilities.HasCapability(NetCapability.Internet))
                            {
                                continue;
                            }

                            //if on 23+ then we can also check validated
                            //Indicates that connectivity on this network was successfully validated.
                            //this means that you can be connected to wifi and has internet
                            if ((int)Android.OS.Build.VERSION.SdkInt >= 23 && !capabilities.HasCapability(NetCapability.Validated))
                            {
                                continue;
                            }

                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Debug.WriteLine("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }
        internal bool GetIsConnected(ConnectivityManager manager)
        {
            try
            {
                //When on API 21+ need to use getAllNetworks, else fall base to GetAllNetworkInfo
                //https://developer.android.com/reference/android/net/ConnectivityManager.html#getAllNetworks()
                if ((int)global::Android.OS.Build.VERSION.SdkInt >= 21)
                {
                    foreach (var network in manager.GetAllNetworks())
                    {
                        try
                        {
                            var capabilities = manager.GetNetworkCapabilities(network);

                            if (capabilities == null)
                            {
                                continue;
                            }

                            //check to see if it has the internet capability
                            if (!capabilities.HasCapability(NetCapability.Internet))
                            {
                                continue;
                            }

                            var info = manager.GetNetworkInfo(network);

                            if (info == null || !info.IsAvailable)
                            {
                                continue;
                            }

                            if (info.IsConnected)
                            {
                                return(true);
                            }
                        }
                        catch
                        {
                            //there is a possibility, but don't worry
                        }
                    }
                }
                else
                {
#pragma warning disable CS0618 // Type or member is obsolete
                    foreach (var info in manager.GetAllNetworkInfo())
#pragma warning restore CS0618 // Type or member is obsolete
                    {
                        if (info == null || !info.IsAvailable)
                        {
                            continue;
                        }

                        if (info.IsConnected)
                        {
                            return(true);
                        }
                    }
                }

                return(false);
            }
            catch (Exception e)
            {
                Logger.Warn("Unable to get connected state - do you have ACCESS_NETWORK_STATE permission? - error: {0}", e);
                return(false);
            }
        }