private void btnAddIe_Click(object sender, RoutedEventArgs e)
        {
            WiFiDirectInformationElement informationElement = new WiFiDirectInformationElement();

            // Information element blob
            DataWriter dataWriter = new DataWriter();

            dataWriter.UnicodeEncoding = UnicodeEncoding.Utf8;
            dataWriter.ByteOrder       = ByteOrder.LittleEndian;
            dataWriter.WriteUInt32(dataWriter.MeasureString(txtInformationElement.Text));
            dataWriter.WriteString(txtInformationElement.Text);
            informationElement.Value = dataWriter.DetachBuffer();

            // Organizational unit identifier (OUI)
            informationElement.Oui = CryptographicBuffer.CreateFromByteArray(Globals.CustomOui);

            // OUI Type
            informationElement.OuiType = Globals.CustomOuiType;

            // Save this information element so we can add it when we advertise.
            _informationElements.Add(informationElement);

            txtInformationElement.Text = "";
            rootPage.NotifyUser("IE added successfully", NotifyType.StatusMessage);
        }
        private void btnAddIe_Click(object sender, RoutedEventArgs e)
        {
            if (_publisher == null)
            {
                _publisher = new WiFiDirectAdvertisementPublisher();
            }

            if (txtInformationElement.Text == "")
            {
                rootPage.NotifyUser("Please specifiy an IE", NotifyType.ErrorMessage);
                return;
            }

            WiFiDirectInformationElement IE = new WiFiDirectInformationElement();

            // IE blob
            DataWriter dataWriter = new DataWriter();

            dataWriter.UnicodeEncoding = UnicodeEncoding.Utf8;
            dataWriter.ByteOrder       = ByteOrder.LittleEndian;
            dataWriter.WriteUInt32(dataWriter.MeasureString(txtInformationElement.Text));
            dataWriter.WriteString(txtInformationElement.Text);
            IE.Value = dataWriter.DetachBuffer();

            // OUI
            DataWriter dataWriterOUI = new DataWriter();

            dataWriterOUI.WriteBytes(Globals.CustomOui);
            IE.Oui = dataWriterOUI.DetachBuffer();

            // OUI Type
            IE.OuiType = Globals.CustomOuiType;

            _publisher.Advertisement.InformationElements.Add(IE);
            txtInformationElement.Text = "";

            rootPage.NotifyUser("IE added successfully", NotifyType.StatusMessage);
        }
        private void btnIe_Click(object sender, RoutedEventArgs e)
        {
            var discoveredDevice = (DiscoveredDevice)lvDiscoveredDevices.SelectedItem;

            IList <WiFiDirectInformationElement> informationElements = null;

            try
            {
                informationElements = WiFiDirectInformationElement.CreateFromDeviceInformation(discoveredDevice.DeviceInfo);
            }
            catch (Exception ex)
            {
                rootPage.NotifyUser("No Information element found: " + ex.Message, NotifyType.ErrorMessage);
            }

            if (informationElements != null)
            {
                StringWriter message = new StringWriter();

                foreach (WiFiDirectInformationElement informationElement in informationElements)
                {
                    string ouiName = CryptographicBuffer.EncodeToHexString(informationElement.Oui);
                    string value   = string.Empty;
                    Byte[] bOui    = informationElement.Oui.ToArray();

                    if (bOui.SequenceEqual(Globals.MsftOui))
                    {
                        // The format of Microsoft information elements is documented here:
                        // https://msdn.microsoft.com/en-us/library/dn392651.aspx
                        // with errata here:
                        // https://msdn.microsoft.com/en-us/library/mt242386.aspx
                        ouiName += " (Microsoft)";
                    }
                    else if (bOui.SequenceEqual(Globals.WfaOui))
                    {
                        ouiName += " (WFA)";
                    }
                    else if (bOui.SequenceEqual(Globals.CustomOui))
                    {
                        ouiName += " (Custom)";

                        if (informationElement.OuiType == Globals.CustomOuiType)
                        {
                            DataReader dataReader = DataReader.FromBuffer(informationElement.Value);
                            dataReader.UnicodeEncoding = UnicodeEncoding.Utf8;
                            dataReader.ByteOrder       = ByteOrder.LittleEndian;

                            // Read the string.
                            try
                            {
                                string data = dataReader.ReadString(dataReader.ReadUInt32());
                                value = $"Data: {data}";
                            }
                            catch (Exception)
                            {
                                value = "(Unable to parse)";
                            }
                        }
                    }

                    message.WriteLine($"OUI {ouiName}, Type {informationElement.OuiType} {value}");
                }

                message.Write($"Information elements found: {informationElements.Count}");

                rootPage.NotifyUser(message.ToString(), NotifyType.StatusMessage);
            }
        }
        private void btnAddIe_Click(object sender, RoutedEventArgs e)
        {
            if (_publisher == null)
            {
                _publisher = new WiFiDirectAdvertisementPublisher();
            }

            if (txtInformationElement.Text == "")
            {
                rootPage.NotifyUser("Please specifiy an IE", NotifyType.ErrorMessage);
                return;
            }

            WiFiDirectInformationElement IE = new WiFiDirectInformationElement();

            // IE blob
            DataWriter dataWriter = new DataWriter();
            dataWriter.UnicodeEncoding = UnicodeEncoding.Utf8;
            dataWriter.ByteOrder = ByteOrder.LittleEndian;
            dataWriter.WriteUInt32(dataWriter.MeasureString(txtInformationElement.Text));
            dataWriter.WriteString(txtInformationElement.Text);
            IE.Value = dataWriter.DetachBuffer();

            // OUI
            DataWriter dataWriterOUI = new DataWriter();
            dataWriterOUI.WriteBytes(Globals.CustomOui);
            IE.Oui = dataWriterOUI.DetachBuffer();

            // OUI Type
            IE.OuiType = Globals.CustomOuiType;

            _publisher.Advertisement.InformationElements.Add(IE);
            txtInformationElement.Text = "";

            rootPage.NotifyUser("IE added successfully", NotifyType.StatusMessage);
        }
        public OnboardingAccessPoint(string ssid, string password, Guid deviceID)
        {
            // Assert password length meets requirements if password is specified.
            // If password is null or empty, treat this as Open network.
            bool WPA2 = !String.IsNullOrEmpty(password);

            if (WPA2)
            {
                Debug.Assert(password.Length <= MAX_WPA2_PASSWORD_LENGTH);
                Debug.Assert(password.Length >= MIN_WPA2_PASSWORD_LENGTH);
            }

            // Begin advertising for legacy clients
            _publisher = new WiFiDirectAdvertisementPublisher();

            // Note: If this flag is not set, the legacy parameters are ignored
            _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = true;

            // Setup Advertisement to use a custom SSID and WPA2 passphrase (null or empty implies Open)
            _publisher.Advertisement.LegacySettings.IsEnabled  = true;
            _publisher.Advertisement.LegacySettings.Ssid       = ssid;
            _publisher.Advertisement.LegacySettings.Passphrase = WPA2 ? new PasswordCredential {
                Password = password
            } : null;

            if (WPA2)
            {
                // If using WPA2, configure this device's WiFi Access Point with the password needed to connect to it
                // and all other information needed by the Microsoft Onboaring Specification
                // After the device is onboarded to an end-user's network this will not be broadcast anymore.

                // The following code generates a Wifi Direct Information Element with the following format:
                //
                //
                //  1Byte   1Byte    3Bytes        1Byte      ... Several Bytes...
                // ---------------------------------------------------------------------------------------
                // | Type | Length |     OUI     | OUITYPE | Data [ETLV1],[ETLV2]...[ETLVn]              |
                // ---------------------------------------------------------------------------------------
                //   DD      num      84 63 d6       00      [01 0A 49 6f 54 20 44 65 76 69 63 65][02....]
                //          bytes                                  | I  o  T     D  e  v  i  c  e|
                //                                                 |   "Device Friendly Name"    |
                //
                // Where each "ETLV" is
                //
                //   1Byte       1Byte     ... Several Bytes...
                // ---------------------------------------------------------------------------------------
                // | ETLV Type | Length |   Data                                                         |
                // ---------------------------------------------------------------------------------------
                //


                // Customize this for your own device
                string DEVICE_FRIENDLY_NAME = "IoT Device: " + ssid;

                IEnumerable <byte> ouiData = MakeStringElement(ETLVType.FRIENDLY_NAME, DEVICE_FRIENDLY_NAME);
                ouiData = ouiData.Concat(MakeTLVElement(ETLVType.DEVICE_TYPE, DEVICE_TYPE));
                ouiData = ouiData.Concat(MakeStringElement(ETLVType.DEVICE_MANUFACTURER, DEVICE_MANUFACTURER_NAME));
                ouiData = ouiData.Concat(MakeStringElement(ETLVType.LANGUAGE_TAG, LANGUAGE_TAG));
                ouiData = ouiData.Concat(MakeGUIDElement(ETLVType.DEVICE_ID, deviceID));
                ouiData = ouiData.Concat(MakeStringElement(ETLVType.PASSWORD, password));
                ouiData = ouiData.Concat(MakeStringElement(ETLVType.UNCLASSIFIED_DEVICE_TYPE, UNCLASSIFIED_DEVICE_TYPE));

                // Create the IoT Device Wi-Fi Beacons Partner Information Element
                var infoElement = new WiFiDirectInformationElement
                {
                    OuiType = OUI_TYPE,
                    Oui     = ByteArrayToBuffer(MICROSOFT_OUI),
                    Value   = ByteArrayToBuffer(ouiData.ToArray())
                };

                // The Maximum OUI Data Length was exceeded.  Trim ouiData String lengths
                Debug.Assert(infoElement.Value.Length < MAX_OUI_DATA_LENGTH);

                // Add the custom Information Elements for publication
                var wifiInfoElements = new List <WiFiDirectInformationElement>
                {
                    infoElement
                };
                _publisher.Advertisement.InformationElements = wifiInfoElements;
            }
        }
        private void btnAddIe_Click(object sender, RoutedEventArgs e)
        {
            WiFiDirectInformationElement informationElement = new WiFiDirectInformationElement();

            // Information element blob
            DataWriter dataWriter = new DataWriter();
            dataWriter.UnicodeEncoding = UnicodeEncoding.Utf8;
            dataWriter.ByteOrder = ByteOrder.LittleEndian;
            dataWriter.WriteUInt32(dataWriter.MeasureString(txtInformationElement.Text));
            dataWriter.WriteString(txtInformationElement.Text);
            informationElement.Value = dataWriter.DetachBuffer();

            // Organizational unit identifier (OUI)
            informationElement.Oui = CryptographicBuffer.CreateFromByteArray(Globals.CustomOui);

            // OUI Type
            informationElement.OuiType = Globals.CustomOuiType;

            // Save this information element so we can add it when we advertise.
            _informationElements.Add(informationElement);

            txtInformationElement.Text = "";
            rootPage.NotifyUser("IE added successfully", NotifyType.StatusMessage);
        }
        private void btnIe_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
        {
            if (lvDiscoveredDevices.SelectedItems.Count == 0)
            {
                rootPage.NotifyUser("No device selected, please select one.", NotifyType.ErrorMessage);
                return;
            }

            var selectedDevices = lvDiscoveredDevices.SelectedItems;

            foreach (DiscoveredDevice discoveredDevice in selectedDevices)
            {
                try
                {
                    var iECollection = WiFiDirectInformationElement.CreateFromDeviceInformation(discoveredDevice.DeviceInfo);

                    if (iECollection.Count == 0)
                    {
                        rootPage.NotifyUser("No Information Elements found", NotifyType.StatusMessage);
                        return;
                    }

                    StringWriter strIeOutput = new StringWriter();
                    String       strIe       = "N/A";

                    foreach (WiFiDirectInformationElement ie in iECollection)
                    {
                        Byte[] bOui = ie.Oui.ToArray();

                        if (bOui.SequenceEqual(Globals.MsftOui))
                        {
                            strIeOutput.Write("Microsoft IE: ");
                        }
                        else if (bOui.SequenceEqual(Globals.WfaOui))
                        {
                            strIeOutput.Write("WFA IE: ");
                        }
                        else if (bOui.SequenceEqual(Globals.CustomOui))
                        {
                            strIeOutput.Write("Custom IE: ");

                            DataReader dataReader = Windows.Storage.Streams.DataReader.FromBuffer(ie.Value);
                            dataReader.UnicodeEncoding = Windows.Storage.Streams.UnicodeEncoding.Utf8;
                            dataReader.ByteOrder       = ByteOrder.LittleEndian;

                            // Read the string.
                            strIe = dataReader.ReadString(dataReader.ReadUInt32());
                        }
                        else
                        {
                            strIeOutput.Write("Unknown IE: ");
                        }

                        strIeOutput.WriteLine(String.Format("OUI Type: {0} OUI: 0x{1} 0x{2} 0x{3} IE Data: {4}\n",
                                                            Convert.ToString(ie.OuiType), Convert.ToString(bOui[0], 16), Convert.ToString(bOui[1], 16),
                                                            Convert.ToString(bOui[2], 16), strIe));

                        strIe = "N/A";
                    }

                    rootPage.NotifyUser(strIeOutput.ToString(), NotifyType.StatusMessage);
                }
                catch (Exception ex)
                {
                    rootPage.NotifyUser("No Information element found: " + ex.Message, NotifyType.ErrorMessage);
                }
            }
        }