private async Task OnConnected(WifiListViewItemPresenter network, WiFiConnectionStatus status, CoreDispatcher dispatcher)
        {
            if (status == WiFiConnectionStatus.Success)
            {
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var itemLocation = WifiListViewItems.IndexOf(network);

                    // don't move if index is -1 or 0
                    if (itemLocation > 0)
                    {
                        // make sure first network doesn't also show connected
                        SwitchToItemState(WifiListViewItems[0], WifiInitialState, true);

                        // Show current connected network at top of list in connected state
                        WifiListViewItems.Move(itemLocation, 0);
                    }

                    var item = SwitchToItemState(network, WifiConnectedState, true);
                    if (item != null)
                    {
                        item.IsSelected = true;
                    }
                });

                NetworkConnected?.Invoke(this, new EventArgs());
            }
            else
            {
                var resourceLoader = ResourceLoader.GetForCurrentView();
                network.Message          = resourceLoader.GetString(status.ToString() + "Text");
                network.IsMessageVisible = true;
                SwitchToItemState(network, WifiConnectState, true);
            }
        }
Esempio n. 2
0
        private async Task ConnectToWifiAsync(WiFiAvailableNetwork network, PasswordCredential credential, CoreDispatcher dispatcher)
        {
            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                SwitchToItemState(network, WifiConnectingState, false);
            });

            var didConnect = credential == null?
                             networkPresenter.ConnectToNetwork(network, ConnectAutomatically) :
                                 networkPresenter.ConnectToNetworkWithPassword(network, ConnectAutomatically, credential);

            DataTemplate nextState   = (await didConnect) ? WifiConnectedState : WifiInitialState;
            bool         isConnected = (nextState == WifiConnectedState);

            await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
            {
                var list         = WifiListView.ItemsSource as ObservableCollection <WiFiAvailableNetwork>;
                var itemLocation = list.IndexOf(network);
                if (0 != itemLocation && isConnected)
                {
                    list.Move(itemLocation, 0);
                }
                var item = SwitchToItemState(network, nextState, true);
                if (item != null)
                {
                    item.IsSelected = true;
                }
            });

            if (isConnected)
            {
                NetworkConnected?.Invoke(this, new EventArgs());
            }
        }
Esempio n. 3
0
 private void FSD_NetworkConnected(object sender, NetworkEventArgs e)
 {
     mForcedDisconnect    = false;
     mIntentialDisconnect = false;
     NotificationPosted?.Invoke(this, new NotificationPostedEventArgs(NotificationType.Info, mConnectInfo.ObserverMode ? "Connected to network in observer mode" : "Connected to network"));
     NetworkConnected?.Invoke(this, new NetworkConnectedEventArgs(mConnectInfo));
 }
        /// <summary>
        /// Checks the IP Address.
        /// </summary>
        /// <returns><c>true</c> if IP Address was assigned, <c>false</c> otherwise.</returns>
        /// <param name="networkInterface">Net.</param>
        private static bool UpdateIPAddressFromDHCP(NetworkInterface networkInterface)
        {
            int timeout = 10000; // timeout, in milliseconds to wait for an IP (10s)

            // check to see if the IP address is empty (0.0.0.0). IPAddress.Any is 0.0.0.0.
            if (networkInterface.IPAddress == IPAddress.Any.ToString())
            {
                Debug.Print("No IP Address");

                if (networkInterface.IsDhcpEnabled)
                {
                    Debug.Print("DHCP is enabled, attempting to get an IP Address");

                    // ask for an IP address from DHCP [note this is a static, not sure which network interface it would act on]
                    int sleepInterval    = 10;
                    int maxIntervalCount = timeout / sleepInterval;
                    int count            = 0;

                    while (IPAddress.GetDefaultLocalAddress() == IPAddress.Any && count < maxIntervalCount)
                    {
                        Debug.Print("Sleep while obtaining an IP");
                        Thread.Sleep(sleepInterval);
                        count++;
                    }
                    ;

                    // if we got here, we either timed out or got an address, so let's find out.
                    if (networkInterface.IPAddress == IPAddress.Any.ToString())
                    {
                        Debug.Print("Failed to get an IP Address in the alotted time.");
                        return(false);
                    }

                    Debug.Print("Got an IP Address: " + networkInterface.IPAddress.ToString());

                    if (NetworkConnected != null)
                    {
                        NetworkConnected.Invoke(null, EventArgs.Empty);
                    }
                    return(true);
                }
                else
                {
                    Debug.Print("DHCP is not enabled and no IP address is configured.");
                    return(false);
                }
            }
            else
            {
                Debug.Print("Already have an IP Address: " + networkInterface.IPAddress.ToString());

                if (NetworkConnected != null)
                {
                    NetworkConnected.Invoke(null, EventArgs.Empty);
                }

                return(true);
            }
        }
        private async Task OnConnected(WiFiListViewItemPresenter network, WiFiConnectionStatus status, CoreDispatcher dispatcher)
        {
            if (status == WiFiConnectionStatus.Success)
            {
                await dispatcher.RunAsync(CoreDispatcherPriority.Normal, () =>
                {
                    var itemLocation = WiFiListViewItems.IndexOf(network);

                    // Don't move if index is -1 or 0
                    if (itemLocation > 0)
                    {
                        // Make sure first network doesn't also show connected
                        SwitchToItemState(WiFiListViewItems[0], WiFiInitialState, true);

                        // Show current connected network at top of list in connected state
                        WiFiListViewItems.Move(itemLocation, 0);
                    }

                    network.Message          = string.Empty;
                    network.IsMessageVisible = false;

                    var item = SwitchToItemState(network, WiFiConnectedState, true);
                    if (item != null)
                    {
                        item.IsSelected = true;
                    }
                });

                if (!NetworkPresenter.IsNetworkOpen(network))
                {
                    NetworkConnected?.Invoke(this, new EventArgs());
                }
            }
            else
            {
                // Entering the wrong password may cause connection attempts to timeout
                // Disconnecting the adapter will return it to a non-busy state
                if (status == WiFiConnectionStatus.Timeout)
                {
                    network.Adapter.Disconnect();
                }
                var resourceLoader = ResourceLoader.GetForCurrentView();
                network.Message          = Common.GetLocalizedText(status.ToString() + "Text");
                network.IsMessageVisible = true;
                SwitchToItemState(network, WiFiConnectState, true);
            }
        }
        private void NetworkChange_NetworkAddressChanged(object sender, EventArgs e)
        {
            bool isc = CommonWin32API.InternetGetConnectedState(out lfag, 0);

            if (isc != isconnected)
            {
                isconnected = isc;
                if (isc)
                {
                    NetworkConnected?.Invoke(this, e);
                }
                else
                {
                    NetworkDisconnect?.Invoke(this, e);
                }
            }
        }