Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

Connect to a specified WIFI(WAP) not working

$
0
0

I trying to Connect the Wifi(WAP) ,but its not connecting.Please any one help on this .Here is my code.

    public Task<string> ConnectToWifi(string wifiSSID, string wifiPWD)
    {
        DateTime requestStartTime = DateTime.Now;
        TaskCompletionSource<string> taskCompletionSource = new TaskCompletionSource<string>();

        //Configure the SSID And PWD into WifiConfiguration
        //Please note the quotes. String should contain ssid in quotes
        var formattedSsid = "\"" + wifiSSID + "\""; 
        var formattedPassword = "\"" + wifiPWD + "\""; 
        WifiConfiguration wifiConfig = new WifiConfiguration
        {
            Ssid = formattedSsid,
            PreSharedKey = formattedPassword,
            StatusField = WifiStatus.Enabled,
            Priority = 40
        };
        wifiConfig.AllowedProtocols.Set((int)ProtocolType.Rsn);
        wifiConfig.AllowedProtocols.Set((int)ProtocolType.Wpa);
        wifiConfig.AllowedKeyManagement.Set((int)KeyManagementType.WpaPsk);
        wifiConfig.AllowedPairwiseCiphers.Set((int)PairwiseCipherType.Ccmp);
        wifiConfig.AllowedPairwiseCiphers.Set((int)PairwiseCipherType.Tkip);
        wifiConfig.AllowedGroupCiphers.Set((int)GroupCipherType.Wep40);
        wifiConfig.AllowedGroupCiphers.Set((int)GroupCipherType.Wep104);
        wifiConfig.AllowedGroupCiphers.Set((int)GroupCipherType.Ccmp);
        wifiConfig.AllowedGroupCiphers.Set((int)GroupCipherType.Tkip);
        wifiConfig.AllowedAuthAlgorithms.Set((int)AuthAlgorithmType.Open);

        WifiManager wifiManager = (WifiManager)Android.App.Application.Context.GetSystemService(Context.WifiService);

        //Enable the Wi-fi
        wifiManager.SetWifiEnabled(true);

        //Add the Wifidetails into WifiConfiguration
        var addNetwork = wifiManager.AddNetwork(wifiConfig);
        var network = wifiManager.ConfiguredNetworks.FirstOrDefault(n => n.Ssid == formattedSsid);

        if (network == null)
        {
            Device.StartTimer(TimeSpan.FromSeconds(1), () =>
            {
                taskCompletionSource.SetResult("internal error");
                return false;
            });
        }
        else
        {

            // Make sure network hasn't changed
            wifiManager.UpdateNetwork(wifiConfig);

            //Disconnect the Wi-fi
            wifiManager.Disconnect();

            //Enable the Wi-fi
            wifiManager.EnableNetwork(network.NetworkId, true);

            //ReConnect the Wi-fi
            wifiManager.Reconnect();

            //Get the reponse from the WifiReceiver BroadcastReceiver
            MainActivity.isWifiOnReceived = false;
            MainActivity.mWifiConnectionReceiver.RegisterTaskCompletionSource(taskCompletionSource, formattedSsid);

            //Wifi connection request not responsed means after 20seconds  request has cancelled
            requestStartTime = DateTime.Now;
            Device.StartTimer(TimeSpan.FromSeconds(20), () =>
            {
                MainActivity.UserDisconnected = "";
                var dateDiff = DateTime.Now - requestStartTime;
                if (dateDiff.Seconds == 20 && !MainActivity.isWifiOnReceived)
                {
                    try
                    {
                        wifiManager.RemoveNetwork(network.NetworkId);
                        if (taskCompletionSource != null)
                        {
                            taskCompletionSource?.SetResult("internal error");
                        }
                    }
                    catch
                    {
                        MainActivity.isWifiOnReceived = false;
                    }
                }
                return MainActivity.isWifiOnReceived;
            });
        }
        return taskCompletionSource.Task;
    }

Thanks,
Ramya.


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>