public void Connect(Models.WifiDescriptor descriptor, string passPhrase)
        {
            WifiConfiguration wifiConfig = new WifiConfiguration
            {
                Ssid         = $"\"{descriptor.Ssid}\"",
                PreSharedKey = $"\"{passPhrase}\""
            };

            try
            {
                this._wifiManager.AddNetwork(wifiConfig);
            }
            catch (Exception ex)
            {
                throw new WifiException("WifiConnector can not add the new wifi network configuration", ex);
            }

            WifiConfiguration network = null;

            try
            {
                network = this._wifiManager.ConfiguredNetworks
                          .FirstOrDefault(n => n.Ssid == wifiConfig.Ssid);

                if (network == null)
                {
                    throw new WifiException("WifiConnector can not connect to the specified wifi network");
                }
            }
            catch (Exception ex)
            {
                throw new WifiException("WifiConnector can not get the list of configured wifi networks", ex);
            }

            try
            {
                this._wifiManager.Disconnect();

                bool networkEnabled = _wifiManager.EnableNetwork(network.NetworkId, true);

                if (this.VerifyConnectivity(wifiConfig.Ssid, networkEnabled))
                {
                    OnConnected?.Invoke(this, EventArgs.Empty);
                }
                else
                {
                    throw new WifiException("The specified wifi network does not exist");
                }
            }
            catch (Exception ex)
            {
                throw new WifiException("Activating the connection to the configured wifi network failed", ex);
            }
        }
Пример #2
0
        public void Connect(Models.WifiDescriptor descriptor, string passPhrase)
        {
            NEHotspotConfiguration wifiConfig = new NEHotspotConfiguration(descriptor.Ssid, passPhrase, false);

            wifiConfig.JoinOnce = true;

            try
            {
                if (this._wifiManager == null)
                {
                    throw new WifiException("WifiConnector can not access the device WifiManager");
                }

                this._wifiManager.RemoveConfiguration(descriptor.Ssid);

                this._wifiManager.ApplyConfiguration(wifiConfig, error => this.CompletionHandler(error, descriptor.Ssid));
            }
            catch (Exception ex)
            {
                throw new WifiException("WifiConnector can not add the new wifi network configuration", ex);
            }
        }