Пример #1
0
        /// <summary>
        /// Event handler for when WiFi scan completes
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Wifi_AvailableNetworksChanged(WiFiAdapter sender, object e)
        {
            Debug.WriteLine("Wifi_AvailableNetworksChanged - get report");

            // Get Report of all scanned WiFi networks
            WiFiNetworkReport report = sender.NetworkReport;

            // Enumerate though networks looking for our network
            foreach (WiFiAvailableNetwork net in report.AvailableNetworks)
            {
                // Show all networks found
                Debug.WriteLine($"Net SSID :{net.Ssid},  BSSID : {net.Bsid},  rssi : {net.NetworkRssiInDecibelMilliwatts.ToString()},  signal : {net.SignalBars.ToString()}");

                // If its our Network then try to connect
                if (net.Ssid == MYSSID)
                {
                    // Disconnect in case we are already connected
                    sender.Disconnect();

                    // Connect to network
                    WiFiConnectionResult result = sender.Connect(net, WiFiReconnectionKind.Automatic, MYPASSWORD);

                    // Display status
                    if (result.ConnectionStatus == WiFiConnectionStatus.Success)
                    {
                        Debug.WriteLine("Connected to Wifi network");
                    }
                    else
                    {
                        Debug.WriteLine($"Error {result.ConnectionStatus.ToString()} connecting o Wifi network");
                    }
                }
            }
        }
Пример #2
0
        private static void WifiAvailableNetworksChanged(WiFiAdapter sender, object e)
        {
            var wifiNetworks = sender.NetworkReport.AvailableNetworks;

            foreach (var net in wifiNetworks)
            {
                Debug.WriteLine($"SSID: {net.Ssid}, strength: {net.SignalBars}");
                if (net.Ssid == MySsid)
                {
                    if (_isConnected)
                    {
                        sender.Disconnect();
                        _isConnected = false;
                        Thread.Sleep(3000);
                    }
                    // Connect to network
                    WiFiConnectionResult result = sender.Connect(net, WiFiReconnectionKind.Automatic, MyPassword);

                    // Display status
                    if (result.ConnectionStatus == WiFiConnectionStatus.Success)
                    {
                        Debug.WriteLine($"Connected to Wifi network {net.Ssid}");
                        _isConnected = true;
                        break;
                    }
                    else
                    {
                        Debug.WriteLine($"Error {result.ConnectionStatus} connecting to Wifi network");
                    }
                }
            }
        }
        public static void SetupAndConnectNetwork()
        {
            wifi = WiFiAdapter.FindAllAdapters()[0];
            WiFiConnectionResult wiFiConnectionResult = wifi.Connect(c_SSID, WiFiReconnectionKind.Automatic, c_AP_PASSWORD);

            NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces();

            if (nis.Length > 0)
            {
                NetworkInterface ni = nis[0];
                ni.EnableAutomaticDns();
                ni.EnableDhcp();

                CheckIP();

                if (!NetworkHelpers.IpAddressAvailable.WaitOne(5000, false))
                {
                    throw new NotSupportedException("ERROR: IP address is not assigned to the network interface.");
                }
            }
            else
            {
                throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one.");
            }
        }
Пример #4
0
        /// <summary>
        /// WiFiスキャンが完了したときのイベント
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void Wifi_AvailableNetworksChanged(WiFiAdapter sender, object e)
        {
            Debug.WriteLine("Wifi_AvailableNetworksChanged - get report");

            // スキャンされたすべてのWiFiネットワークのレポートを取得
            WiFiNetworkReport report = sender.NetworkReport;

            // ネットワークを探して列挙する
            foreach (WiFiAvailableNetwork net in report.AvailableNetworks)
            {
                // 見つかったすべてのネットワークを表示
                Debug.WriteLine($"Net SSID :{net.Ssid},  BSSID : {net.Bsid},  rssi : {net.NetworkRssiInDecibelMilliwatts.ToString()},  signal : {net.SignalBars.ToString()}");

                // 自分のネットワークの場合、接続する
                if (net.Ssid == MYSSID)
                {
                    // すでに接続されている場合は切断します
                    sender.Disconnect();

                    // ネットワークに接続する
                    WiFiConnectionResult result = sender.Connect(net, WiFiReconnectionKind.Automatic, MYPASSWORD);

                    // ステータス表示
                    if (result.ConnectionStatus == WiFiConnectionStatus.Success)
                    {
                        Debug.WriteLine("Connected to Wifi network");
                    }
                    else
                    {
                        Debug.WriteLine($"Error {result.ConnectionStatus} connecting o Wifi network");
                    }
                }
            }
        }
        private void HandleWifiNetworksChanged(WiFiAdapter sender, object e)
        {
            Debug.WriteLine("WiFi networks changed.");

            var currentIpAddres = GetIpAddress();
            var needToConnect   = string.IsNullOrEmpty(currentIpAddres) || (currentIpAddres == "0.0.0.0");


            if (needToConnect)
            {
                Debug.WriteLine("We're not connected to any WiFi network. Connecting.");
                var wiFiConfiguration = Wireless80211Configuration.GetAllWireless80211Configurations()[0];
                var report            = sender.NetworkReport;
                foreach (var network in report.AvailableNetworks)
                {
                    // Show all networks found
                    Debug.WriteLine($"Net SSID :{network.Ssid},  BSSID : {network.Bsid},  rssi : {network.NetworkRssiInDecibelMilliwatts},  signal : {network.SignalBars}");


                    // If its our Network then try to connect
                    if (network.Ssid == wiFiConfiguration.Ssid)
                    {
                        var result = sender.Connect(network, WiFiReconnectionKind.Automatic, wiFiConfiguration.Password);

                        // Display status
                        if (result.ConnectionStatus == WiFiConnectionStatus.Success)
                        {
                            Debug.WriteLine($"Connected to Wifi network {network.Ssid}.");
                        }
                        else
                        {
                            Debug.WriteLine($"Error {result.ConnectionStatus} connecting to Wifi network {network.Ssid}.");
                        }
                    }
                }
            }
            else
            {
                Debug.WriteLine("We're still connected to WiFi. Will do nothing.");
            }
        }