/// <summary>Working hack function to connect to device, open socket and send data string /// /// </summary> /// <param name="adapter"></param> /// <param name="ssid"></param> /// <param name="password"></param> /// <returns></returns> //private async Task ConnectToNetworkAndSendTest(WiFiAdapter adapter, string ssid, string password) { // //// Should already be scanned // //await adapter.ScanAsync(); // WiFiNetworkReport report = adapter.NetworkReport; // this.log.Info("ConnectToNetworkAndSendTest", () => // string.Format("Timestamp {0} Count {1}", report.Timestamp, report.AvailableNetworks.Count)); // foreach (var net in report.AvailableNetworks) { // if (net.Ssid == ssid) { // PasswordCredential cred = new PasswordCredential() { // Password = password // }; // var result = await adapter.ConnectAsync(net, WiFiReconnectionKind.Automatic, cred); // this.log.Info("ConnectToNetworkAndSendTest", () => // string.Format("Connection result {0}", result.ConnectionStatus)); // if (result.ConnectionStatus == WiFiConnectionStatus.Success) { // // Setup socket and send dummy string // //https://stackoverflow.com/questions/36475752/network-connection-with-uwp-apps // try { // //StreamSocket ss = new StreamSocket(); // ConnectionProfile profile = await this.wifiAdapter.NetworkAdapter.GetConnectedProfileAsync(); // //connectProfile.ProfileName // this.log.Info("ConnectToNetworkAndSendTest", () => // string.Format("Connected to:{0}", profile.ProfileName)); // if (profile.IsWlanConnectionProfile) { // // Only get the SSID - no socket information // //profile.WlanConnectionProfileDetails.GetConnectedSsid(); // } // //foreach (var ep in NetworkInformation.GetInternetConnectionProfile) { // //} // //foreach (var ids in NetworkInformation.GetLanIdentifiers()) { // //foreach (var prf in NetworkInformation.GetConnectionProfiles()) { // using (StreamSocket ss = new StreamSocket()) { // HostName host = new HostName("192.168.4.1"); // IP of the Arduino WIFI // this.log.Info("ConnectToNetworkAndSendTest", () => // string.Format("Connecting socket to {0}", host.DisplayName, host.CanonicalName)); // await ss.ConnectAsync(host, "80"); // StreamSocketInformation i = ss.Information; // this.log.Info("ConnectToNetworkAndSendTest", () => string.Format("Connected to socket Local {0}:{1} Remote {2}:{3} - {4}", // i.LocalAddress, i.LocalPort, // i.RemoteHostName, i.RemotePort, i.RemoteServiceName)); // /* // // This works // Stream outStream = ss.OutputStream.AsStreamForWrite(); // StreamWriter writer = new StreamWriter(outStream); // string data = "This is test from UWP"; // this.log.Info("ConnectToNetworkAndSendTest", "Sending"); // //await writer.WriteLineAsync(data); // var arr = data.ToAsciiByteArray(); // //await writer.WriteAsync(arr, 0, arr.Length); // await outStream.WriteAsync(arr, 0, arr.Length); // this.log.Info("ConnectToNetworkAndSendTest", "Flushing"); // await outStream.FlushAsync(); // this.log.Info("ConnectToNetworkAndSendTest", "Sending"); // await outStream.WriteAsync(arr, 0, arr.Length); // this.log.Info("ConnectToNetworkAndSendTest", "Flushing"); // await outStream.FlushAsync(); // // Hmm do not seem to need the writer // */ // /* // Stream outStream = ss.OutputStream.AsStreamForWrite(); // StreamWriter writer = new StreamWriter(outStream); // string data = "This is test from UWP"; // this.log.Info("ConnectToNetworkAndSendTest", "Sending"); // await writer.WriteLineAsync(data); // this.log.Info("ConnectToNetworkAndSendTest", "Flushing"); // await writer.FlushAsync(); // this.log.Info("ConnectToNetworkAndSendTest", "Finished flush"); // this.log.Info("ConnectToNetworkAndSendTest", "Sending"); // await writer.WriteLineAsync(data); // this.log.Info("ConnectToNetworkAndSendTest", "Flushing"); // await writer.FlushAsync(); // this.log.Info("ConnectToNetworkAndSendTest", "Finished flush"); // */ // // This also works - 10 seconds for first, second instant // // The time is due to establishing the socket first time. Will go to the connection routine // this.writer = new DataWriter(ss.OutputStream); // string data = "This is test from UWP"; // this.log.Info("ConnectToNetworkAndSendTest", "Sending"); // this.writer.UnicodeEncoding = UnicodeEncoding.Utf8; // this.writer.WriteBytes(data.ToAsciiByteArray()); // this.writer.WriteByte(0x0A); // //this.writer.WriteString(data); // await ss.OutputStream.WriteAsync(this.writer.DetachBuffer()); // this.log.Info("ConnectToNetworkAndSendTest", "Finished sending"); // //this.writer.WriteString(data); // this.writer.WriteBytes(data.ToAsciiByteArray()); // this.writer.WriteByte(0x0A); // await ss.OutputStream.WriteAsync(this.writer.DetachBuffer()); // this.log.Info("ConnectToNetworkAndSendTest", "Finished sending"); // } // } // catch (Exception e) { // this.log.Exception(9999, "", e); // } // } // break; // } // } //} #region Debug dump methods private void DumpAdapterInfo(WifiAdapterInfo info) { this.log.Info("DumpAdapterInfo", () => string.Format(" IANA Type: {0}", info.IanaType)); this.log.Info("DumpAdapterInfo", () => string.Format(" Max BPS In: {0}", info.MaxBitsPerSecondIn)); this.log.Info("DumpAdapterInfo", () => string.Format("Max BPS Out: {0}", info.MaxBitsPerSecondOut)); this.log.Info("DumpAdapterInfo", () => string.Format(" Adapter Id: {0}", info.AdapterId.ToString())); }
private void InitAdapterInfo(WiFiAdapter adapter, WifiAdapterInfo info) { this.log.InfoEntry("InitAdapterInfo"); info.AdapterId = adapter.NetworkAdapter.NetworkAdapterId; info.IanaType = adapter.NetworkAdapter.IanaInterfaceType.ToEnum(); info.MaxBitsPerSecondIn = adapter.NetworkAdapter.InboundMaxBitsPerSecond; info.MaxBitsPerSecondOut = adapter.NetworkAdapter.OutboundMaxBitsPerSecond; this.DumpAdapterInfo(info); }
private async Task ScanForNetworks(WiFiAdapter adapter, WifiAdapterInfo info) { try { // TODO - will need a list of available UWP network objects to be able to find fof connection - or use the UWP adapter await adapter.ScanAsync(); networks = wifiAdapter.NetworkReport.AvailableNetworks; info.Networks = new List <WifiNetworkInfo>(); // We will not return those with blank SSID foreach (var net in networks) { if (net.Ssid.Length > 0) { WifiNetworkInfo netInfo = new WifiNetworkInfo() { SSID = net.Ssid, Kind = net.NetworkKind.Convert(), ChanneCenterFrequencyKlhz = net.ChannelCenterFrequencyInKilohertz, BeaconInterval = net.BeaconInterval, MacAddress_BSSID = net.Bssid, RssiInDecibleMilliwatts = net.NetworkRssiInDecibelMilliwatts, PhysicalLayerKind = net.PhyKind.Convert(), AuthenticationType = net.SecuritySettings.NetworkAuthenticationType.Convert(), EncryptionType = net.SecuritySettings.NetworkEncryptionType.Convert(), IsWifiDirect = net.IsWiFiDirect, SignalStrengthInBars = net.SignalBars, UpTime = net.Uptime, }; //this.DumpNetworkInfo(netInfo); info.Networks.Add(netInfo); } } this.log.Info("ScanForNetworks", () => string.Format("Found {0} networks", info.Networks.Count)); } catch (Exception e) { this.log.Exception(9999, "ScanForNetworks", "", e); } }