public static NetworkConfig GetNetworkConfig() { var nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length <= 0) { return(new NetworkConfig(NetworkInterfaceType.Unknown, string.Empty, double.NaN, string.Empty, string.Empty)); } // get the first interface var ni = nis[0]; if (ni.NetworkInterfaceType != NetworkInterfaceType.Wireless80211) { return(new NetworkConfig(ni.NetworkInterfaceType, string.Empty, double.NaN, ni.IPv4Address, ni.IPv4GatewayAddress)); } var wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; _wifiAdapter.ScanAsync(); _autoResetEvent.WaitOne(); double rssi = double.NaN; foreach (var networkReportAvailableNetwork in _wifiAdapter.NetworkReport.AvailableNetworks) { if (networkReportAvailableNetwork.Ssid == wc.Ssid) { rssi = networkReportAvailableNetwork.NetworkRssiInDecibelMilliwatts; } } return(new NetworkConfig(ni.NetworkInterfaceType, wc.Ssid, rssi, ni.IPv4Address, ni.IPv4GatewayAddress)); }
/// <summary> /// Configure and enable the Wireless station interface /// </summary> /// <param name="ssid"></param> /// <param name="password"></param> /// <returns></returns> public static bool Configure(string ssid, string password) { Wireless80211Configuration wconf = GetConfiguration(); // Set Options for Network Interface // // Enable - Enable the Wireless Station ( Disable to reduce power ) // AutoConnect - Automatically try to connect on boot. // wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable; wconf.Ssid = ssid; wconf.Password = password; if (wconf.Password.Length == 0) { wconf.Authentication = System.Net.NetworkInformation.AuthenticationType.Open; } else { wconf.Authentication = System.Net.NetworkInformation.AuthenticationType.WPA2; } // Save the configuration so on restart it will be running. wconf.SaveConfiguration(); return(false); }
/// <summary> /// Disable the Wireless station interface. /// </summary> public static void Disable() { Wireless80211Configuration wconf = GetConfiguration(); wconf.Options = Wireless80211Configuration.ConfigurationOptions.None; wconf.SaveConfiguration(); }
internal static void WorkingThread() { do { Debug.WriteLine("Waiting for network available..."); Thread.Sleep(500); }while (!NetworkInterface.GetIsNetworkAvailable()); NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Debug.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; // note on checking the 802.11 configuration // on secure devices (like the TI CC3220SF) the password can't be read // so we can't use the code block bellow to automatically set the profile if ((wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) && (wc.Ssid != "" && wc.Password == "")) { // have to update Wi-Fi configuration wc.Ssid = c_SSID; wc.Password = c_AP_PASSWORD; wc.SaveConfiguration(); } else { // Wi-Fi configuration matches } } else { // network interface is Ethernet Debug.WriteLine("Network connection is: Ethernet"); } // check if we have an IP CheckIP(); if (_requiresDateTime) { IpAddressAvailable.WaitOne(); SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
/// <summary> /// Connect to the SSID network and login using the Password /// </summary> private void ConnectNetwork() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // Get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // Network interface is Wi-Fi Console.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; // wc.Ssid = _SSID; // wc.Password = _Password; if (wc.Ssid != _SSID && wc.Password != _Password) { // Updated 9/27/2019 wc.Options = Wireless80211Configuration.ConfigurationOptions.Enable | Wireless80211Configuration.ConfigurationOptions.AutoConnect; wc.Ssid = _SSID; wc.Password = _Password; // Save so when we reboot it will connect automatically wc.SaveConfiguration(); // Uncomment to restart // Power.RebootDevice(); } } else { // Network interface is Ethernet Console.WriteLine("Network connection is: Ethernet"); ni.EnableDhcp(); } // Wait for DHCP to complete WaitIP(); } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
public bool ConnectToNetwork(string wifiSsid, string wifiPassword) { var isOk = false; var networkInterfaces = NetworkInterface.GetAllNetworkInterfaces(); if (networkInterfaces.Length > 0) { // Getting the first interface. Usually there is only one existing. NetworkInterface networkInterface = networkInterfaces[0]; if (networkInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { Debug.WriteLine("Network connection is: Wi-Fi"); var wirelessConfiguration = Wireless80211Configuration.GetAllWireless80211Configurations()[networkInterface.SpecificConfigId]; if (wirelessConfiguration.Ssid != wifiSsid && wirelessConfiguration.Password != wifiPassword) { wirelessConfiguration.Ssid = wifiSsid; wirelessConfiguration.Password = wifiPassword; wirelessConfiguration.SaveConfiguration(); } } else { Debug.WriteLine("Network connection is: Ethernet"); networkInterface.EnableDhcp(); } Debug.WriteLine("Waiting for IP..."); var retryCount = 0; while ((string.IsNullOrEmpty(networkInterface.IPv4Address) || networkInterface.IPv4Address == "0.0.0.0") && retryCount < 60) { retryCount++; Debug.WriteLine("Retrying. Count: " + retryCount); Thread.Sleep(1000); } if (retryCount == 60 || networkInterface.IPv4Address.Equals("0")) { Debug.WriteLine("No IP address has been acquired. Check your SSID and password or router."); } else { isOk = true; Debug.WriteLine("Connected. IP address is " + networkInterface.IPv4Address); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } return(isOk); }
public static void Main() { string SSID = Params.SSID; string PASSWORD = Params.AP_PASSWORD; try { Console.WriteLine("Wireless connect AP test - availability"); NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; // Get firt interface for ESP32 ( Wireless AP ) NetworkInterface wi = NetworkInterface.GetAllNetworkInterfaces()[0]; wi.EnableDhcp(); // Get ID of wireless config ( should be 0 ) uint wirelessConfigId = wi.SpecificConfigId; Wireless80211Configuration wirelessApConfig = Wireless80211Configuration.GetAllWireless80211Configurations()[wirelessConfigId]; // Set up the Access Point detail we want to connect to wirelessApConfig.Ssid = SSID; wirelessApConfig.Password = PASSWORD; wirelessApConfig.Authentication = Wireless80211Configuration.AuthenticationType.WPA2; // Save config so it will be persisted over a reboot wirelessApConfig.SaveConfiguration(); // Currenlty need to reboot if this is first time run while (true) { NetworkInterface wi2 = NetworkInterface.GetAllNetworkInterfaces()[0]; if (wi2.IPv4Address[0] != 0) { break; } Console.WriteLine("No IP yet, not connected"); Thread.Sleep(1000); } Console.WriteLine("Have IP so must be connected"); while (true) { Thread.Sleep(1000); } // User code goes here } catch (Exception ex) { // Do whatever please you with the exception caught } }
/// <summary> /// Configure and enable the Wireless station interface /// </summary> /// <param name="ssid"></param> /// <param name="password"></param> /// <returns></returns> public static bool Configure(string ssid, string password) { // And we have to force connect once here even for a short time var success = WiFiNetworkHelper.ConnectDhcp(ssid, password, token: new CancellationTokenSource(10000).Token); Debug.WriteLine($"Connection is {success}"); Wireless80211Configuration wconf = GetConfiguration(); wconf.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable; wconf.SaveConfiguration(); return(true); }
internal static void WorkingThread() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Console.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) { // have to update Wi-Fi configuration wc.Ssid = c_SSID; wc.Password = c_AP_PASSWORD; wc.SaveConfiguration(); } else { // Wi-Fi configuration matches } } else { // network interface is Ethernet Console.WriteLine("Network connection is: Ethernet"); } // check if we have an IP if (!CheckIP()) { if (ni.IsDhcpEnabled) { ni.RenewDhcpLease(); } } if (_requiresDateTime) { IpAddressAvailable.WaitOne(); //SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
/// <summary> /// Initialise the Wireless parameters and save /// </summary> public static void SetupAndConnectWifi() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { NetworkInterface ni = nis[0]; // get Wifi interface if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != Params.SSID && wc.Password != Params.AP_PASSWORD) { Console.WriteLine("Updating Wifi config"); wc.Ssid = Params.SSID; wc.Password = Params.AP_PASSWORD; wc.SaveConfiguration(); ni.EnableDhcp(); } else { Console.WriteLine("Wifi config ok"); } Console.WriteLine("Wait for IP"); while (true) { NetworkInterface ni2 = NetworkInterface.GetAllNetworkInterfaces()[0]; if (ni2.IPv4Address != null && ni2.IPv4Address.Length > 0) { if (ni2.IPv4Address[0] != '0') { Console.WriteLine("Have IP " + ni2.IPv4Address); break; } } Thread.Sleep(1000); } } else { throw new NotSupportedException("No Wifi"); } } else { throw new NotSupportedException("No Network"); } }
/// <summary> /// This is a helper function to pick up first available network interface and use it for communication. /// </summary> private static void SetupAndConnectNetwork() { // Get the first WiFI Adapter var wifiAdapter = WiFiAdapter.FindAllAdapters()[0]; // Begin network scan. wifiAdapter.ScanAsync(); // While networks are being scan, continue on configuration. If networks were set previously, // board may already be auto-connected, so reconnection is not even needed. var wiFiConfiguration = Wireless80211Configuration.GetAllWireless80211Configurations()[0]; var ipAddress = NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address; var needToConnect = string.IsNullOrEmpty(ipAddress) || (ipAddress == "0.0.0.0"); while (needToConnect) { foreach (var network in wifiAdapter.NetworkReport.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 = wifiAdapter.Connect(network, WiFiReconnectionKind.Automatic, wiFiConfiguration.Password); if (result.ConnectionStatus == WiFiConnectionStatus.Success) { Debug.WriteLine($"Connected to Wifi network {network.Ssid}."); needToConnect = false; } else { Debug.WriteLine($"Error {result.ConnectionStatus} connecting to Wifi network {network.Ssid}."); } } } Thread.Sleep(10000); } ipAddress = NetworkInterface.GetAllNetworkInterfaces()[0].IPv4Address; Debug.WriteLine($"Connected to Wifi network with IP address {ipAddress}"); }
public static void SetupAndConnectNetwork() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface NetworkInterface ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Console.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != c_SSID && wc.Password != c_AP_PASSWORD) { // have to update Wi-Fi configuration wc.Ssid = c_SSID; wc.Password = c_AP_PASSWORD; wc.SaveConfiguration(); } else { // Wi-Fi configuration matches } } else { // network interface is Ethernet Console.WriteLine("Network connection is: Ethernet"); ni.EnableDhcp(); } // wait for DHCP to complete WaitIP(); } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
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."); } }
public static void ConnectToNetwork() { if (_connectionStatus) { return; } var nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface var ni = nis[0]; if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { // network interface is Wi-Fi Logger.Log("Network connection is: Wi-Fi"); var wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; wc.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect; wc.SaveConfiguration(); OnWifiDuringConnection(); } else { throw new NotSupportedException("ERROR: there is no wifi network interface configured."); } // wait for DHCP to complete WaitIp(); OnWifiConnected(); _connectionStatus = true; InitWifiAdpater(); StartWifiStatusWatcher(); } else { throw new NotSupportedException("ERROR: there is no wifi network interface configured."); } }
/// <summary> /// Initialise the Wireless parameters and save /// </summary> public static void SetupAndConnectNetwork() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { NetworkInterface ni = nis[0]; // get Wifi interface if (ni.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]; if (wc.Ssid != Params.SSID && wc.Password != Params.AP_PASSWORD) { Console.WriteLine("Updating Wifi config"); wc.Ssid = Params.SSID; wc.Password = Params.AP_PASSWORD; wc.SaveConfiguration(); ni.EnableDhcp(); } else { Console.WriteLine("Wifi config ok"); } WaitIP(); } else { Console.WriteLine("Not ESP32 "); ni.EnableDhcp(); WaitIP(); } } else { throw new NotSupportedException("No Network"); } }
public static bool IsEnabled() { Wireless80211Configuration wconf = GetConfiguration(); return(!string.IsNullOrEmpty(wconf.Ssid)); }
public static void Main() { // change here the network interface index to be used during the test // usual values // 0: for STM32 boards (only one network adapter) // 2: for ESP32 const int networkInterfaceIndex = 0; // change here the network configurations to be used during the test const string staticIPv4Address = "192.168.1.222"; const string staticIPv4SubnetMask = "255.255.255.0"; const string staticIPv4DNSAdress = "192.168.1.2"; try { NetworkChange.NetworkAvailabilityChanged += NetworkChange_NetworkAvailabilityChanged; NetworkChange.NetworkAddressChanged += NetworkChange_NetworkAddressChanged; DisplayNetworkInterfacesDetails(); // display wireless 802.11 network configurations Wireless80211Configuration[] configs = Wireless80211Configuration.GetAllWireless80211Configurations(); Console.WriteLine(""); Console.WriteLine("====================="); Console.WriteLine("Number of Wireless 802.11 configurations: " + configs.Length.ToString()); int count = 0; foreach (Wireless80211Configuration conf in configs) { Console.WriteLine("Number:" + count.ToString()); count++; Console.WriteLine("ID: " + conf.Id); Console.WriteLine("Authentication: " + conf.Authentication); Console.WriteLine("Encryption: " + conf.Encryption); Console.WriteLine("Radio: " + conf.Radio); Console.WriteLine("SSID: " + conf.Ssid); Console.WriteLine("Password: "******""); } int loopCount = 0; // Loop displaying the current network information for Interfaces available while (true) { loopCount++; // Test code for DHCP and Static adapter configs // Switch every 20 loops between the two if (loopCount % 20 == 0) { Console.WriteLine(""); Console.WriteLine("Switching to DHCP"); Console.WriteLine(""); // switch to DHCP, get test interface NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[networkInterfaceIndex]; ni.EnableAutomaticDns(); ni.EnableDhcp(); } else if (loopCount % 10 == 0) { Console.WriteLine(""); Console.WriteLine("Switching to static IPv4"); Console.WriteLine(""); // switch to static address, get test interface NetworkInterface ni = NetworkInterface.GetAllNetworkInterfaces()[networkInterfaceIndex]; ni.EnableStaticIPv4(staticIPv4Address, staticIPv4SubnetMask, staticIPv4DNSAdress); } Thread.Sleep(1000); } } catch (Exception ex) { // Do whatever please you with the exception caught Console.WriteLine("Exception:" + ex.Message); } }
internal static void WorkingThread() { NetworkInterface[] nis = NetworkInterface.GetAllNetworkInterfaces(); if (nis.Length > 0) { // get the first interface foreach (var n in nis) { if (n.NetworkInterfaceType == NetworkInterfaceType.Ethernet) { Console.WriteLine("Network connection is: Ethernet"); n.EnableAutomaticDns(); n.EnableDhcp(); //n.EnableStaticIPv4("10.10.10.131", "255.255.255.0", "10.10.10.1"); m_NetworkInterfaces_Lan = n; } if (n.NetworkInterfaceType == NetworkInterfaceType.Wireless80211) { if (DisableWifi) { Console.WriteLine("Found Network connection is: Wi-Fi. Disable active"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[n.SpecificConfigId]; Console.WriteLine($"Options:{wc.Options}"); if (wc.Options != Wireless80211Configuration.ConfigurationOptions.None) { Console.WriteLine("Found Network connection is: Wi-Fi. Disabling it."); wc.Options = Wireless80211Configuration.ConfigurationOptions.None; wc.SaveConfiguration(); } n.EnableAutomaticDns(); n.EnableDhcp(); // Reboot nötig nach dem Abschalten von Wifi } else { Console.WriteLine("Network connection is: Wi-Fi"); Wireless80211Configuration wc = Wireless80211Configuration.GetAllWireless80211Configurations()[n.SpecificConfigId]; Console.WriteLine($"Ssid:{wc.Ssid},Encryption:{wc.Encryption},Password:{wc.Password},Authentication:{wc.Authentication}"); Console.WriteLine($"Options:{wc.Options}"); wc.Options = Wireless80211Configuration.ConfigurationOptions.AutoConnect | Wireless80211Configuration.ConfigurationOptions.Enable; wc.Authentication = AuthenticationType.WPA2; wc.Encryption = EncryptionType.WPA2_PSK; wc.Ssid = c_SSID; wc.Password = c_AP_PASSWORD; //wc.Ssid = ""; //wc.Password = ""; wc.SaveConfiguration(); //n.EnableStaticIPv4("192.168.0.250", "255.255.255.0", "192.168.0.1"); n.EnableAutomaticDns(); n.EnableDhcp(); m_NetworkInterfaces_Wifi = n; } } } if (m_NetworkInterfaces_Lan == null && m_NetworkInterfaces_Wifi == null) { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } // check if we have an IP CheckIP(); if (_requiresDateTime) { IpAddressAvailable.WaitOne(); SetDateTime(); } } else { throw new NotSupportedException("ERROR: there is no network interface configured.\r\nOpen the 'Edit Network Configuration' in Device Explorer and configure one."); } }
public static bool IsEnabled() { Wireless80211Configuration wconf = GetConfiguration(); return((wconf.Options & Wireless80211Configuration.ConfigurationOptions.Enable) == Wireless80211Configuration.ConfigurationOptions.Enable); }
/// <summary> /// Get the Wireless station configuration. /// </summary> /// <returns>Wireless80211Configuration object</returns> public static Wireless80211Configuration GetConfiguration() { NetworkInterface ni = GetInterface(); return(Wireless80211Configuration.GetAllWireless80211Configurations()[ni.SpecificConfigId]); }