private void finallyConnect(string networkPass, string networkSSID)
 {
     try
     {
         using (Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)ApplicationContext.GetSystemService(Context.WifiService))
         {
             if (wifi != null && wifi.IsWifiEnabled)
             {
                 using (WifiConfiguration wifiConfig = new WifiConfiguration())
                 {
                     wifiConfig.Ssid         = "\"" + networkSSID + "\""; //string.Format("\"%s\"", networkSSID);
                     wifiConfig.PreSharedKey = "\"" + networkPass + "\""; // string.Format("\"%s\"", networkPass);
                     int netId = wifi.AddNetwork(wifiConfig);
                     wifi.Disconnect();
                     wifi.EnableNetwork(netId, true);
                     wifi.Reconnect();
                     using (WifiConfiguration conf = new WifiConfiguration())
                     {
                         conf.Ssid         = "\"" + networkSSID + "\"";
                         conf.PreSharedKey = "\"" + networkPass + "\"";
                         wifi.AddNetwork(conf);
                     }
                 }
             }
         }
     }
     catch
     {
     }
 }
        protected override void OnCreate(Bundle bundle)
        {
            base.OnCreate(bundle);
            RequestWindowFeature(WindowFeatures.NoTitle);
            SetContentView(Resource.Layout.nconnect);
            Bundle extras = Intent?.Extras;

            if (extras == null)
            {
                Finish();
                return;
            }
            string SSID             = extras.GetString("SSID", "");
            bool   connected        = extras.GetBoolean("connected", false);
            bool   hasEverConnected = extras.GetBoolean("hasEverConnected", false);
            int    NetworkId        = extras.GetInt("NetworkId", 0);

            if (string.IsNullOrEmpty(SSID))
            {
                Finish();
                return;
            }
            Button   ok           = (Button)FindViewById(Resource.Id.okButton);
            Button   forgetButton = (Button)FindViewById(Resource.Id.forgetButton);
            TextView pass         = (TextView)FindViewById(Resource.Id.textviewPassword);

            if (!hasEverConnected)
            {
                forgetButton.Text = "RETURN";
            }
            if (connected)
            {
                ok.Text = "(CONNECTED)";
                ok.SetBackgroundResource(Resource.Color.black);
                ok.Enabled = false;
            }
            if (hasEverConnected || connected)
            {
                pass.Text    = "***********";
                pass.Enabled = false;
            }
            else
            {
                LoadBClicks();
            }
            ok.Click += delegate
            {
                if (hasEverConnected)
                {
                    try
                    {
                        using (Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)ApplicationContext.GetSystemService(Context.WifiService))
                        {
                            if (wifi != null && wifi.IsWifiEnabled)
                            {
                                wifi.Disconnect();
                                wifi.EnableNetwork(NetworkId, true);
                                wifi.Reconnect();
                            }
                        }
                    }
                    catch { }
                }
                else
                {
                    finallyConnect(pass.Text, SSID);
                }
                Finish();
            };

            forgetButton.Click += delegate
            {
                try
                {
                    using (Android.Net.Wifi.WifiManager wifi = (Android.Net.Wifi.WifiManager)ApplicationContext.GetSystemService(Context.WifiService))
                    {
                        if (wifi != null && wifi.IsWifiEnabled)
                        {
                            if (hasEverConnected)
                            {
                                wifi.RemoveNetwork(NetworkId);
                            }
                        }
                    }
                }
                catch { }
                Finish();
            };
        }