Ejemplo n.º 1
0
        public int CheckWIFI()
        {
            WifiManager wifi = (WifiManager)context.GetSystemService(Context.WifiService);

            if (wifi != null)
            {
                if (wifi.WifiState == Android.Net.WifiState.Disabled)
                {
                    wifi.SetWifiEnabled(true);
                }
                if (wifi.WifiState == Android.Net.WifiState.Enabling)
                {
                    return(1);
                }
                else if (wifi.WifiState != Android.Net.WifiState.Enabled)
                {
                    return(0);
                }
            }
            else
            {
                return(0);
            }

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

            if (conn != null)
            {
                NetworkInfo networkInfo = conn.ActiveNetworkInfo;
                if (networkInfo == null)
                {
                    return(0);
                }
                if (networkInfo.GetState() == NetworkInfo.State.Connecting)
                {
                    return(1);
                }
                else if (networkInfo.GetState() != NetworkInfo.State.Connected)
                {
                    return(0);
                }
            }
            else
            {
                return(0);
            }

            return(2);
        }
Ejemplo n.º 2
0
        public static bool IsNetworkConnected(Context context)
        {
            ConnectivityManager conMgr        = (ConnectivityManager)context.GetSystemService(Context.ConnectivityService);
            NetworkInfo         activeNetwork = conMgr.ActiveNetworkInfo;

            return(activeNetwork != null && activeNetwork.GetState() == NetworkInfo.State.Connected);
        }
Ejemplo n.º 3
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";
            }
        }
Ejemplo n.º 4
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}");
                    }
                }
            }
        }
Ejemplo n.º 5
0
        private void DetectNetwork()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
            NetworkInfo         info = connectivityManager.ActiveNetworkInfo;
            bool isOnline            = info.IsConnected;

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

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

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

                // Check for a WiFi connection
                bool isWifi = info.Type == ConnectivityType.Wifi;
                if (isWifi)
                {
                    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
                if (info.IsRoaming)
                {
                    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";
            }
        }
Ejemplo n.º 6
0
        public bool IsConnecting()
        {
            ConnectivityManager connectivity = (ConnectivityManager)this
                                               .GetSystemService(Context.ConnectivityService);

            if (connectivity != null)
            {
                NetworkInfo info = connectivity.ActiveNetworkInfo;
                if (info != null && info.GetState() == NetworkInfo.State.Connected)
                {
                    return(true);
                }
            }
            return(false);
        }
Ejemplo n.º 7
0
        private void DetectNetworkT()
        {
            ConnectivityManager connectivityManager = (ConnectivityManager)GetSystemService(ConnectivityService);
            NetworkInfo         info = connectivityManager.ActiveNetworkInfo;
            bool isOnline            = info.IsConnected;

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

            if (isOnline)
            {
                // Display the type of connectionn
                NetworkInfo.State activeState = info.GetState();


                // Check for a WiFi connection
                bool isWifi = info.Type == ConnectivityType.Wifi;
                if (isWifi)
                {
                    Android.App.AlertDialog.Builder build = new AlertDialog.Builder(this);
                    AlertDialog alertDialog = build.Create();
                    alertDialog.SetTitle("Mensaje");
                    alertDialog.SetIcon(Android.Resource.Drawable.IcDialogAlert);
                    alertDialog.SetMessage("Parece que estas conectado a wifi");
                    alertDialog.SetButton("OK", (s, ev) =>
                    {
                        StartActivity(typeof(SplashScreen));
                    });
                    alertDialog.Show();
                }
                else
                {
                    Log.Debug(TAG, "Wifi disconnected.");
                }

                // Check if roaming
                if (info.IsRoaming)
                {
                    Log.Debug(TAG, "Roaming.");
                }
                else
                {
                    Log.Debug(TAG, "Not roaming.");
                }
            }
            else
            {
            }
        }
Ejemplo n.º 8
0
        public override async void OnReceive(Context context, Intent intent)
        {
            var sharedPref = context.GetSharedPreferences(MainActivity.PrefName, FileCreationMode.Private);

            if (!sharedPref.GetBoolean(MainActivity.ifAutoLoginPrefKey, false))
            {
                return;
            }
            if (working)
            {
                return;
            }
            working = true;

            NetworkInfo info = (NetworkInfo)intent.GetParcelableExtra(WifiManager.ExtraNetworkInfo);

            System.Diagnostics.Debug.WriteLine($"{info.GetState()}/{info.GetDetailedState()}");
            if (info.IsConnected)
            {
                WifiInfo wifiInfo = (WifiInfo)intent.GetParcelableExtra(WifiManager.ExtraWifiInfo);
                if (wifiInfo != null && wifiInfo.SSID.Contains(Shared.ScutStudentClient.wifiSsid))
                {
                    Shared.ScutStudentClient client = new Shared.ScutStudentClient();
                    await client.TryGetStatus();

                    if (client.Status == Shared.ScutStudentClientStatus.NeedLogin)
                    {
                        string userName = sharedPref.GetString(MainActivity.usernamePrefKey, string.Empty);
                        string password = sharedPref.GetString(MainActivity.passwordPrefKey, string.Empty);
                        Notification.Builder builder = new Notification.Builder(context);
                        int notificationId;
                        try
                        {
                            await client.Login(userName, password);

                            sharedPref.Edit().PutString(MainActivity.wlanAcIpPrefKey, client.WlanAcIp).Apply();
                            builder.SetContentTitle("自动登录scut-student成功")
                            .SetContentText($"账户:{userName}")
                            .SetSmallIcon(Resource.Drawable.ic_wifi_lock_white_24dp);
                            notificationId = succeedNotificationId;
                        }
                        catch (Exception e)
                        {
                            builder.SetContentTitle("自动登录scut-student失败")
                            .SetContentText(e.Message)
                            .SetSmallIcon(Resource.Drawable.ic_perm_scan_wifi_white_24dp);
                            notificationId = failedNotificationId;
                        }

                        var resultIntent = new Intent(context, typeof(MainActivity));
                        TaskStackBuilder stackBuilder = TaskStackBuilder.Create(context);
                        stackBuilder.AddParentStack(Java.Lang.Class.FromType(typeof(MainActivity)));
                        stackBuilder.AddNextIntent(resultIntent);
                        PendingIntent resultPendingIntent = stackBuilder.GetPendingIntent(0, PendingIntentFlags.UpdateCurrent);
                        builder.SetContentIntent(resultPendingIntent);
                        Notification        notification        = builder.Build();
                        NotificationManager notificationManager =
                            context.GetSystemService(Context.NotificationService) as NotificationManager;
                        notificationManager.Notify(notificationId, notification);
                    }
                }
            }
            working = false;
        }
        public override void OnReceive(Context context, Intent intent)
        {
            if (intent == null || intent.Extras == null)
            {
                return;
            }

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

            NetworkInfo networkInfo = manager.ActiveNetworkInfo;

            if (networkInfo != null)
            {
                NetworkInfo.State connectionStatus = networkInfo.GetState();

                if (connectionStatus == NetworkInfo.State.Connected)
                {
                    NetworkStatus = Status.CONNECTED;

                    NetworkType = networkInfo.Type;

                    NetworkDescription = networkInfo.TypeName;
                }
                else if (connectionStatus == NetworkInfo.State.Disconnected)
                {
                    NetworkStatus = Status.NOT_CONNECTED;

                    NetworkType = ConnectivityType.Dummy;

                    NetworkDescription = "";
                }
                else if (connectionStatus == NetworkInfo.State.Connecting)
                {
                    NetworkStatus = Status.IS_CONNECTING;

                    NetworkType = ConnectivityType.Dummy;

                    NetworkDescription = "";
                }
                else if (connectionStatus == NetworkInfo.State.Disconnecting)
                {
                    NetworkStatus = Status.IS_DISCONNECTING;

                    NetworkType = ConnectivityType.Dummy;

                    NetworkDescription = "";
                }
                else if (connectionStatus == NetworkInfo.State.Suspended)
                {
                    NetworkStatus = Status.SUSPENDED;

                    NetworkType = ConnectivityType.Dummy;

                    NetworkDescription = "";
                }
                else if (connectionStatus == NetworkInfo.State.Unknown)
                {
                    NetworkStatus = Status.UNKNOWN;

                    NetworkType = ConnectivityType.Dummy;

                    NetworkDescription = "";
                }
            }
            else
            {
                NetworkStatus = Status.NOT_AVAILABLE;

                NetworkType = ConnectivityType.Dummy;

                NetworkDescription = "";
            }

            NotifyStateToAll();

            CurrentNetworkType        = NetworkType;
            CurrentNetworkDescription = NetworkDescription;
        }