Beispiel #1
0
        protected override void OnResume()
        {
            try
            {
                string status = "WIFI NETWORKS: ";
                string SSID = "";
                using (Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)ApplicationContext.GetSystemService(Context.WifiService))
                {
                    if (wifi != null)
                    {
                        if (wifi.IsWifiEnabled)
                        {
                            using (WifiInfo wifiInfo = wifi.ConnectionInfo)
                            {
                                if (wifiInfo != null)
                                {
                                    using (NetworkInfo.DetailedState state = WifiInfo.GetDetailedStateOf(wifiInfo.SupplicantState))
                                    {
                                        if (state == NetworkInfo.DetailedState.Connected || state == NetworkInfo.DetailedState.ObtainingIpaddr)
                                        {
                                            SSID = wifiInfo.SSID;
                                        }
                                    }
                                }
                            }
                        }
                        else
                        {
                            wifi.SetWifiEnabled(true);
                        }

                using (ConnectivityManager cm = (ConnectivityManager)GetSystemService(Context.ConnectivityService))
                {
                    if (cm != null)
                    {
                        using (NetworkInfo activeNetwork = cm.ActiveNetworkInfo)
                        {
                            if (activeNetwork != null && activeNetwork.IsConnected)
                            {
                                SSID = SSID.Replace("\"", "");
                                status = ": CONNECTED TO INTERNET. Select a network from the list below to modify or establish a connection.";
                                status = SSID + status;
                            }
                            else
                            {
                                if (activeNetwork == null)
                                {
                                    status = "WIFI NETWORKS: ";
                                }
                                else
                                {
                                    if (!string.IsNullOrEmpty(SSID))
                                    {
                                        status = " OBTAINING IP ADDRESS...";
                                        status = SSID + status;
                                    }
                                    else
                                    {
                                        status = "WIFI NETWORKS: ATTEMPTING CONNECTION...";
                                    }
                                }
                            }
                        }
                    }
                }
                IList\\ wifiScanList = wifi.ScanResults;
                    if (wifiScanList != null)
                    {
                        wifis = new string[wifiScanList.Count];
                        for (int i = 0; i \                        {
                            wifis[i] = ((wifiScanList[i]).ToString());
                        }
                        filtered = new List\\();
                        filtered.Add(status);
                        int counter = 0;
                        foreach (string eachWifi in wifis)
                        {
                            var rx = new string[] { "," };
                            string[] temp = eachWifi.Split(rx, StringSplitOptions.None);
                            string lval = temp[0].Substring(5).Trim();
                            if (!string.IsNullOrEmpty(lval) && lval.Length \>\ 3)
                            {
                                filtered.Add(lval);
                                counter++;
                            }
                        }
                    }
                    }
                }
                ListAdapter = new MyListAdapter(this);
            }
            catch (Exception)
            {
            }

            base.OnResume();
        }
Beispiel #2
0
            private void connectToWifi(string wifiSSID)
            {
                String SSID = "";
                bool connected = false;
                bool hasEverConnected = false;
                int NetworkId = 0;
                try
                {
                    using (Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)context.ApplicationContext.GetSystemService(Context.WifiService))
                    {
                        if (wifi != null && wifi.IsWifiEnabled)
                        {
                            using (WifiInfo wifiInfo = wifi.ConnectionInfo)
                            {
                                if (wifiInfo != null)
                                {
                                    using (NetworkInfo.DetailedState state = WifiInfo.GetDetailedStateOf(wifiInfo.SupplicantState))
                                    {
                                        if (state == NetworkInfo.DetailedState.Connected || state == NetworkInfo.DetailedState.ObtainingIpaddr)
                                        {
                                            SSID = wifiInfo.SSID.Replace("\"", "").Trim();
                                            SSID = SSID.Replace(" ", "");
                                            if (SSID == wifiSSID.Trim().Replace(" ", ""))
                                            {
                                                connected = true;
                                            }
                                        }
                                    }
                                }
                            }

                            IList\\ wifiScanList = wifi.ConfiguredNetworks;
                            for (int i = 0; i \                            {
                                if (((wifiScanList[i]).ToString()).Contains("hasEverConnected: true"))
                                {
                                    String cw = wifiScanList[i].Ssid.Replace("\"", "").Trim();
                                    cw = cw.Replace(" ", "");
                                    if (cw == wifiSSID.Trim().Replace(" ", ""))
                                    {
                                        NetworkId = wifiScanList[i].NetworkId;
                                        hasEverConnected = true;
                                        break;
                                    }
                                }
                            }
                        }
                    }
                }
                catch (Exception e)
                {
                }



                var nxact = new Intent(Application.Context, typeof(ConnectActivity));
                nxact.PutExtra("SSID", wifiSSID);
                nxact.PutExtra("connected", connected);
                nxact.PutExtra("hasEverConnected", hasEverConnected);
                nxact.PutExtra("NetworkId", NetworkId);
                context.StartActivity(nxact);
                activity.Finish();
            }