Esempio n. 1
0
        public override void OnReceive(Context context, Intent intent)
        {
            ConnectivityManager conMgr = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (conMgr.GetNetworkInfo(ConnectivityType.Mobile).GetState() == NetworkInfo.State.Connected ||
                conMgr.GetNetworkInfo(ConnectivityType.Wifi).GetState() == NetworkInfo.State.Connected)
            {
                connected = true;
            }
            else
            {
                connected = false;
            }

            System.Timers.Timer timer = new System.Timers.Timer
            {
                Interval  = 100,
                Enabled   = true,
                AutoReset = false,
            };
            timer.Elapsed += (object sender, System.Timers.ElapsedEventArgs e) =>
            {
                ReportConnectionStatus(context);
            };
            timer.Start();
        }
Esempio n. 2
0
        private void GetNetworkInfomation()
        {
            // Get the reference of Connectivity Service
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);

            // Get the network information
            NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;

            if (activeConnection != null)
            {
                txtConnectedValue.Text = activeConnection.IsConnected ? "Yes" : "No";
            }

            NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);

            if (wifiInfo != null)
            {
                txtWifiValue.Text = wifiInfo.IsConnected ? "Yes" : "No";
            }

            NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);

            if (mobileInfo != null)
            {
                txtRoamingValue.Text = (mobileInfo.IsConnected && mobileInfo.IsRoaming) ? "Yes" : "No";
            }
        }
Esempio n. 3
0
        /// <summary>
        /// Check if connected to network
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public bool ConnectedToNetwork(Context context)
        {
            bool connected = false;

            ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (connectivityManager != null)
            {
                bool mobileNetwork = false;
                bool wifiNetwork   = false;
                bool wimaxNetwork  = false;

                bool mobileNetworkConnected = false;
                bool wifiNetworkConnected   = false;
                bool wimaxNetworkConnected  = false;

                NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
                NetworkInfo wifiInfo   = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
                NetworkInfo wimaxInfo  = connectivityManager.GetNetworkInfo(ConnectivityType.Wimax);

                if (mobileInfo != null)
                {
                    mobileNetwork = mobileInfo.IsAvailable;
                    //Console.WriteLine("Is mobile available?  " + mobileNetwork);
                }

                if (wifiInfo != null)
                {
                    wifiNetwork = wifiInfo.IsAvailable;
                    //Console.WriteLine("Is WiFi available?  " + wifiNetwork);
                }

                if (wimaxInfo != null)
                {
                    wimaxNetwork = wimaxInfo.IsAvailable;
                    //Console.WriteLine("Is WiMAX available?  " + wimaxNetwork);
                }

                if (wifiNetwork || mobileNetwork || wimaxNetwork)
                {
                    mobileNetworkConnected = (mobileInfo != null) ? mobileInfo.IsConnectedOrConnecting : false;
                    wifiNetworkConnected   = (wifiInfo != null) ? wifiInfo.IsConnectedOrConnecting : false;
                    wimaxNetworkConnected  = (wimaxInfo != null) ? wimaxInfo.IsConnectedOrConnecting : false;
                }

                connected = (mobileNetworkConnected || wifiNetworkConnected || wimaxNetworkConnected);

                //Console.WriteLine("Is mobile connected?  " + mobileNetworkConnected);
                //Console.WriteLine("Is WiFi connected?  " + wifiNetworkConnected);
                //Console.WriteLine("Is WiMAX connected?  " + wimaxNetworkConnected);
            }

            Console.WriteLine("Is this device connected?  " + connected);

            return(connected);
        }
Esempio n. 4
0
        /// <summary>
        /// Returns true if we have network connectivity
        /// </summary>
        /// <param name="context">Context</param>
        /// <returns>True if there is network connectivity, false if there is no network connectivity</returns>
        public static bool IsOnline(Context context)
        {
            Log.Info("SharpXmppDemo", "IsOnline - Checking if Online");

            ConnectivityManager cm =
                (ConnectivityManager)context.GetSystemService(Android.Content.Context.ConnectivityService);
            NetworkInfo netInfoMobile = cm.GetNetworkInfo(ConnectivityType.Mobile);
            NetworkInfo netInfoWifi   = cm.GetNetworkInfo(ConnectivityType.Wifi);

            return((netInfoMobile != null && netInfoMobile.IsConnectedOrConnecting) ||
                   (netInfoWifi != null && netInfoWifi.IsConnectedOrConnecting));
        }
Esempio n. 5
0
        public bool NetworkAvailable()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
            NetworkInfo         wifiInfo            = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
            NetworkInfo         mobDataInfo         = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);

            if (wifiInfo.IsConnected || mobDataInfo.IsConnected)
            {
                return(true);
            }

            return(false);
        }
Esempio n. 6
0
        private void DetectNetwork()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
            NetworkInfo         activeConnection    = connectivityManager.ActiveNetworkInfo;

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

            Log.Debug(TAG, "IsOnline = {0}", isOnline);

            if (isOnline)
            {
                _isConnectedImage.SetImageResource(Resource.Drawable.green_square);

                // Display the type of connection
                NetworkInfo.State activeState = activeConnection.GetState();
                _connectionType.Text = activeConnection.TypeName;

                // Check for a WiFi connection
                NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
                if (wifiInfo.IsConnected)
                {
                    Log.Debug(TAG, "Wifi connected.");
                    _wifiImage.SetImageResource(Resource.Drawable.green_square);
                }
                else
                {
                    Log.Debug(TAG, "Wifi disconnected.");
                    _wifiImage.SetImageResource(Resource.Drawable.red_square);
                }

                // Check if roaming
                NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
                if (mobileInfo.IsRoaming && mobileInfo.IsConnected)
                {
                    Log.Debug(TAG, "Roaming.");
                    _roamingImage.SetImageResource(Resource.Drawable.green_square);
                }
                else
                {
                    Log.Debug(TAG, "Not roaming.");
                    _roamingImage.SetImageResource(Resource.Drawable.red_square);
                }
            }
            else
            {
                _isConnectedImage.SetImageResource(Resource.Drawable.red_square);
                _wifiImage.SetImageResource(Resource.Drawable.red_square);
                _roamingImage.SetImageResource(Resource.Drawable.red_square);
                _connectionType.Text = "N/A";
            }
        }
Esempio n. 7
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();
            }
        }
Esempio n. 8
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);
        }
Esempio n. 9
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));
                }
            }
        }
        private bool IsNetworkAvailable(Context context)
        {
            ConnectivityManager connectivity = (ConnectivityManager)
                                               context.GetSystemService(Context.ConnectivityService);

            if (connectivity != null)
            {
                NetworkInfo mWifi = connectivity.GetNetworkInfo(ConnectivityType.Wifi);
                if (!mWifi.IsConnected)
                {
                    //turn off wifi...
                    if (LostWifi != null)
                    {
                        LostWifi();
                    }
                }
                else
                {
                    if (GotWifi != null)
                    {
                        GotWifi();
                    }
                }
            }
            return(false);
        }
Esempio n. 11
0
        public static bool IsNetworkAvailable(Context context)
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         mobileNetworkInfo   = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);

            return(mobileNetworkInfo != null && mobileNetworkInfo.IsConnected);
        }
Esempio n. 12
0
        public ConnectionStatus CheckNetworkStatus()
        {
            ConnectivityManager manager =
                (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);

            NetworkInfo activeConnection = manager.ActiveNetworkInfo;

            if (activeConnection != null && activeConnection.IsConnected)
            {
                if (manager.GetNetworkInfo(ConnectivityType.Wifi).IsConnected)
                {
                    NetworkStatus = ConnectionStatus.ReachableViaWiFiNetwork;
                }
                else
                {
                    NetworkStatus = ConnectionStatus.ReachableViaRoamingNetwork;
                }
            }
            else
            {
                NetworkStatus = ConnectionStatus.NotReachable;
            }


            return(NetworkStatus);
        }
Esempio n. 13
0
        public bool IsOnline()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)Android.App.Application.Context.GetSystemService(Context.ConnectivityService);

            NetworkInfo activeConnection = connectivityManager.ActiveNetworkInfo;

            NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);

            return((wifiInfo != null && wifiInfo.IsConnected) || (activeConnection != null && activeConnection.IsConnected));
        }
Esempio n. 14
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);
            }
        }
Esempio n. 15
0
        public bool IsWifiConnected()
        {
            ConnectivityManager manager = ConnectivityManager.FromContext(MainActivity.AppContext);
            NetworkInfo         info    = manager.GetNetworkInfo(ConnectivityType.Wifi);

            if (info != null)
            {
                return(info.IsAvailable);
            }
            return(false);
        }
Esempio n. 16
0
        /// <summary>
        /// 检测网络连接状态
        /// </summary>
        /// <returns><c>true</c> if this instance is connected the specified context; otherwise, <c>false</c>.</returns>
        /// <param name="context">Context.</param>
        public static bool IsConnected(Context context)
        {
            bool netFlag = false;
            ConnectivityManager connManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);

            if (connManager.ActiveNetworkInfo != null)
            {
                var wifiWorkInfo = connManager.GetNetworkInfo(ConnectivityType.Wifi);
                var gprsWorkInfo = connManager.GetNetworkInfo(ConnectivityType.Mobile);
                if (wifiWorkInfo != null && wifiWorkInfo.IsAvailable != false)
                {
                    netFlag = true;
                }
                if (gprsWorkInfo != null && gprsWorkInfo.IsConnected != false)
                {
                    netFlag = true;
                }
            }
            return(netFlag);
        }
Esempio n. 17
0
        private string GetAccessPointBSSID()
        {
            string accessPointBSSID = null;

            if (_connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).IsConnected)
            {
                WifiManager wifiManager = Application.Context.GetSystemService(global::Android.Content.Context.WifiService) as WifiManager;
                accessPointBSSID = wifiManager.ConnectionInfo.BSSID;
            }

            return(accessPointBSSID);
        }
        /// <summary>
        /// Retorna um booleano que indica se o aparelho está conectado em alguma Wifi
        /// </summary>
        /// <param name="context"></param>
        /// <returns></returns>
        public static bool ConectadoNaInternetWifi(Context context)
        {
            ConnectivityManager connManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         netInfo     = connManager.GetNetworkInfo(ConnectivityType.Wifi);

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

            return(false);
        }
Esempio n. 19
0
        public XNetworkType NetworkCheck()
        {
            if (_connectivity != null)
            {
                var active = _connectivity.ActiveNetworkInfo;
                //var t2 = _connectivity.GetAllNetworkInfo();
                //var t3 = _connectivity.GetNetworkInfo(ConnectivityType.Mobile);


                if (active != null)
                {
                    switch (active.Type)
                    {
                    case ConnectivityType.Mobile:
                        return(XNetworkType.Cellular);

                    case ConnectivityType.Wifi:
                        return(XNetworkType.WiFi);
                    }
                }

                /* Included these because active is always null in tests */
                var mobileState = _connectivity.GetNetworkInfo(ConnectivityType.Mobile).GetState();
                if (mobileState == NetworkInfo.State.Connected)
                {
                    return(XNetworkType.Cellular);
                }

                var wifiState = _connectivity.GetNetworkInfo(ConnectivityType.Wifi).GetState();
                if (wifiState == NetworkInfo.State.Connected)
                {
                    return(XNetworkType.WiFi);
                }

                return(XNetworkType.None);
            }

            return(XNetworkType.Unknown);
        }
Esempio n. 20
0
        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));
                }
            }
        }
Esempio n. 21
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}");
                    }
                }
            }
        }
Esempio n. 22
0
        public static string GetNetworkLevel(this Context context)
        {
            try
            {
                ConnectivityManager connectivityManager = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
                var hasWifi = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);

                if (hasWifi != null)
                {
                    var wifiManager = (WifiManager)context.GetSystemService(Context.WifiService);
                    var wifiInfo    = wifiManager.ConnectionInfo;

                    if (wifiInfo != null)
                    {
                        return(string.Format("Wifi {0} mbps", wifiInfo.LinkSpeed));
                    }
                }

                var hasMobileNetwork = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);

                if (hasMobileNetwork != null)
                {
                    var telManager = (TelephonyManager)context.GetSystemService(Context.TelephonyService);

                    var networkInfo = telManager.NetworkType;

                    return(string.Format("Mobile network {0}", networkInfo));
                }
            }
            catch (Exception e)
            {
                return(e.Message);
            }

            return(string.Empty);
        }
Esempio n. 23
0
        /// <summary>
        /// Detects whether connected to a wifi network and can pass data.
        /// </summary>
        /// <returns>true, if network was detected, false otherwise.</returns>
        private Boolean IsNetworkConnectionValid()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)_context.GetSystemService(ConnectivityService);

            NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);

            if (wifiInfo.IsConnected)
            {
                return(true);
            }
            else
            {
                return(false);
            }
        }
        public bool HasConnectionToInternet()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
            NetworkInfo         activeConnection    = connectivityManager.ActiveNetworkInfo;
            bool isOnline = (activeConnection != null) && activeConnection.IsConnected;

            if (isOnline)
            {
                NetworkInfo wifiInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi);
                if (wifiInfo.IsConnected)
                {
                    return(true);
                }
                else
                {
                    NetworkInfo mobileInfo = connectivityManager.GetNetworkInfo(ConnectivityType.Mobile);
                    if (mobileInfo.IsRoaming && mobileInfo.IsConnected)
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
        //Is WIFI Connection

        //@param context Context
        //@return true:wifi is connection

        public static bool IsWifiConnection(Context context)
        {
            Object obj = context.GetSystemService(Context.ConnectivityService);

            if (!(obj.GetType() == typeof(ConnectivityManager)))
            {
                return(false);
            }
            ConnectivityManager manager     = (ConnectivityManager)obj;
            NetworkInfo         networkInfo = manager.GetNetworkInfo(ConnectivityType.Wifi);

            if (networkInfo != null && networkInfo.IsConnected)
            {
                return(true);
            }
            return(false);
        }
Esempio n. 26
0
        public static bool connectivity(Context mContext)
        {
            bool status = false;
            ConnectivityManager connectivityManager = (ConnectivityManager)mContext.GetSystemService(Context.ConnectivityService);
            var activeConnection = connectivityManager.ActiveNetworkInfo;
            var mobileState      = connectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState();

            if (connectivityManager != null)
            {
                if (((activeConnection != null) && activeConnection.IsConnected) || mobileState == NetworkInfo.State.Connected)
                {
                    status = true;
                }
            }

            return(status);
        }
Esempio n. 27
0
        /// <summary>
        /// Internets the connection status.
        /// </summary>
        /// <returns>NetworkStatus.</returns>
        public static NetworkStatus InternetConnectionStatus()
        {
            if (IsNetworkAvailable())
            {
                var wifiState = ConnectivityManager.GetNetworkInfo(ConnectivityType.Wifi).GetState();
                if (wifiState == NetworkInfo.State.Connected)
                {
                    return(NetworkStatus.ReachableViaWiFiNetwork);
                }

                var mobileState = ConnectivityManager.GetNetworkInfo(ConnectivityType.Mobile).GetState();
                if (mobileState == NetworkInfo.State.Connected)
                {
                    return(NetworkStatus.ReachableViaCarrierDataNetwork);
                }
            }

            return(NetworkStatus.NotReachable);
        }
    public override void OnReceive(Context context, Intent intent)
    {
        NetworkInfo info = intent.GetParcelableExtra(WifiManager.ExtraNetworkInfo) as NetworkInfo;

        if (info != null && info.IsConnected)
        {
            ConnectivityManager manager      = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         netInfo      = (NetworkInfo)manager.GetNetworkInfo(ConnectivityType.Wifi);
            AudioManager        audioManager = (AudioManager)context.GetSystemService(Context.AudioService);
            if (netInfo.IsConnectedOrConnecting)
            {
                Toast.MakeText(context, "Wifi is connected", ToastLength.Short).Show();
                audioManager.RingerMode = RingerMode.Silent;
            }
            else
            {
                audioManager.RingerMode = RingerMode.Normal;
            }
        }
    }
Esempio n. 29
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));
                }
            }
        }
Esempio n. 30
0
        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);
            }));
        }