public OnboardingAccessPoint(string ssid, string password) { // 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 _publisher.Advertisement.LegacySettings.IsEnabled = true; _publisher.Advertisement.LegacySettings.Ssid = ssid; _publisher.Advertisement.LegacySettings.Passphrase = new PasswordCredential { Password = password }; }
private void btnStartAdvertisement_Click(object sender, RoutedEventArgs e) { try { if (_publisher == null) { _publisher = new WiFiDirectAdvertisementPublisher(); } if (chkListener.IsChecked == true) { if (_listener == null) { _listener = new WiFiDirectConnectionListener(); } _listener.ConnectionRequested += OnConnectionRequested; } _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = (chkPreferGroupOwnerMode.IsChecked == true); if (cmbListenState.SelectionBoxItem.ToString().Equals("Normal") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; } else if (cmbListenState.SelectionBoxItem.ToString().Equals("Intensive") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Intensive; } else if (cmbListenState.SelectionBoxItem.ToString().Equals("None") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.None; } _publisher.StatusChanged += OnStatusChanged; _publisher.Start(); rootPage.NotifyUser("Advertisement started, waiting for StatusChanged callback...", NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser("Error starting Advertisement: " + ex.ToString(), NotifyType.ErrorMessage); } }
private async void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs) { if (statusEventArgs.Status == WiFiDirectAdvertisementPublisherStatus.Started) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (sender.Advertisement.LegacySettings.IsEnabled) { // Show the autogenerated passphrase and SSID. if (String.IsNullOrEmpty(txtPassphrase.Text)) { txtPassphrase.Text = _publisher.Advertisement.LegacySettings.Passphrase.Password; } if (String.IsNullOrEmpty(txtSsid.Text)) { txtSsid.Text = _publisher.Advertisement.LegacySettings.Ssid; } } }); } rootPage.NotifyUserFromBackground($"Advertisement: Status: {statusEventArgs.Status}, Error: {statusEventArgs.Error}", NotifyType.StatusMessage); return; }
private void btnStartAdvertisement_Click(object sender, RoutedEventArgs e) { _publisher = new WiFiDirectAdvertisementPublisher(); _publisher.StatusChanged += OnStatusChanged; _listener = new WiFiDirectConnectionListener(); try { // This can raise an exception if the machine does not support WiFi. Sorry. _listener.ConnectionRequested += OnConnectionRequested; } catch (Exception ex) { //rootPage.NotifyUser($"Error preparing Advertisement: {ex}", NotifyType.ErrorMessage); return; } _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = false; //// Legacy settings are meaningful only if IsAutonomousGroupOwnerEnabled is true. //if (_publisher.Advertisement.IsAutonomousGroupOwnerEnabled && chkLegacySetting.IsChecked.Value) //{ // _publisher.Advertisement.LegacySettings.IsEnabled = true; // if (!string.IsNullOrEmpty(txtPassphrase.Text)) // { // Windows.Security.Credentials.PasswordCredential creds = new Windows.Security.Credentials.PasswordCredential // { // Password = txtPassphrase.Text // }; // _publisher.Advertisement.LegacySettings.Passphrase = creds; // } // if (!string.IsNullOrEmpty(txtSsid.Text)) // { // _publisher.Advertisement.LegacySettings.Ssid = txtSsid.Text; // } //} // Add the information elements. foreach (WiFiDirectInformationElement informationElement in _informationElements) { _publisher.Advertisement.InformationElements.Add(informationElement); } _publisher.Start(); if (_publisher.Status == WiFiDirectAdvertisementPublisherStatus.Started) { //btnStartAdvertisement.IsEnabled = false; btnStopAdvertisement.IsEnabled = true; //rootPage.NotifyUser("Advertisement started.", NotifyType.StatusMessage); } else { // rootPage.NotifyUser($"Advertisement failed to start. Status is {_publisher.Status}", NotifyType.ErrorMessage); } }
// Initialize advertizing and if Receiver handle connetion request public void Init(bool IsReceiver) { // Create an instance of Wifi Direct advertiser if (publisher == null) { publisher = new WiFiDirectAdvertisementPublisher(); } // Listen to connection request if receiver if (IsReceiver) { WiFiDirectConnectionListener listener = new WiFiDirectConnectionListener(); listener.ConnectionRequested += (WiFiDirectConnectionListener sender, WiFiDirectConnectionRequestedEventArgs connectionEventArgs) => { // Because HandleConnectionRequestAsync generates a connection dialog window It request UI Thread WiFiDirectConnectionRequest connectionRequest = connectionEventArgs.GetConnectionRequest(); Xamarin.Forms.Device.BeginInvokeOnMainThread(async() => { // Request a connection to given device var id = connectionRequest.DeviceInformation.Id; //connectionRequest.Dispose(); await WifiDirectHandler.ConnectAsync(id, IsReceiver); }); }; } // Start advertisement with Intensive parameter so that WifiDirect stay enabled even if app is in background publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Intensive; publisher.Start(); }
private static void DisposeWiFiPublisher() { wifiPublisher.Stop(); wifiPublisher.StatusChanged -= WifiPublisher_StatusChanged; wifiPublisher = null; wifiConnectionListener.ConnectionRequested -= WifiConnectionListener_ConnectionRequested; wifiConnectionListener = null; }
/// <summary> /// Wi-Fi直连广播者开始广播 /// </summary> public void StartAdvertising() { publisher = new WiFiDirectAdvertisementPublisher(); listener = new WiFiDirectConnectionListener(); publisher.StatusChanged += OnPublisherStatusChanged; publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Intensive; listener.ConnectionRequested += OnConnectionRequested; publisher.Start(); //TextBlock_ConnectedState.Text = "开始广播……"; }
private async void OnPublisherStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { if (args.Status == WiFiDirectAdvertisementPublisherStatus.Aborted) { Debug.WriteLine("因错误终止广播"); publisher.Start(); } else if (args.Status == WiFiDirectAdvertisementPublisherStatus.Started) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { TextBlock_ConnectedState.Text = "开始广播……"; }); } }
public void StartServer() { publisher = new WiFiDirectAdvertisementPublisher(); listener = new WiFiDirectConnectionListener(); publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; listener.ConnectionRequested += OnConnectionRequested; _ConnectionEstablishState = ConnectionEstablishState.Connecting; OnConnectionEstalblishResult(this, _ConnectionEstablishState); publisher.Start(); StartUDP(50001); //TextBlock_ConnectedState.Text = "开始广播……"; }
private static void StartWiFiHotspot() { wifiPublisher = new WiFiDirectAdvertisementPublisher(); wifiConnectionListener = new WiFiDirectConnectionListener(); wifiConnectionListener.ConnectionRequested += WifiConnectionListener_ConnectionRequested; wifiPublisher.Advertisement.IsAutonomousGroupOwnerEnabled = true; wifiPublisher.Advertisement.LegacySettings.Ssid = SSID; wifiPublisher.Advertisement.LegacySettings.IsEnabled = true; wifiPublisher.Advertisement.LegacySettings.Passphrase.Password = PASSWORD; wifiPublisher.StatusChanged += WifiPublisher_StatusChanged; wifiPublisher.Start(); }
public OnboardingAccessPoint(string ssid, string password) { // 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 _publisher.Advertisement.LegacySettings.IsEnabled = true; _publisher.Advertisement.LegacySettings.Ssid = ssid; _publisher.Advertisement.LegacySettings.Passphrase = new PasswordCredential { Password = password }; }
public void Init() { // Create an Advertisement Publisher WiFiDirectAdvertisementPublisher publisher = new WiFiDirectAdvertisementPublisher(); // Turn on Listen state publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; // Register for connection requests WiFiDirectConnectionListener listener = new WiFiDirectConnectionListener(); listener.ConnectionRequested += OnConnectionRequested; // Start the advertiser publisher.Start(); }
void advertise() { if (mAdvertiser == null) { mAdvertiser = new WiFiDirectAdvertisementPublisher(); } if (mConnectionListener == null) { mConnectionListener = new WiFiDirectConnectionListener(); } mConnectionListener.ConnectionRequested += OnConnectionRequested; mAdvertiser.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; mAdvertiser.StatusChanged += OnStatusChanged; mAdvertiser.Start(); }
private void StartPublisher(WlanSetting setting) { _publisher = new WiFiDirectAdvertisementPublisher(); _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = true; _publisher.StatusChanged += OnStatusChanged; _publisher.Advertisement.LegacySettings.IsEnabled = true; var creds = new Windows.Security.Credentials.PasswordCredential(); _publisher.Advertisement.LegacySettings.Ssid = setting.Name; creds.Password = setting.Password; _publisher.Advertisement.LegacySettings.Passphrase = creds; _publisher.Start(); }
private void btnStartAdvertisement_Click(object sender, RoutedEventArgs e) { try { if (_publisher == null) { _publisher = new WiFiDirectAdvertisementPublisher(); } if (chkListener.IsChecked == true) { if (_listener == null) { _listener = new WiFiDirectConnectionListener(); } _listener.ConnectionRequested += OnConnectionRequested; } _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = (chkPreferGroupOwnerMode.IsChecked == true); if (cmbListenState.SelectionBoxItem.ToString().Equals("Normal") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Normal; } else if (cmbListenState.SelectionBoxItem.ToString().Equals("Intensive") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.Intensive; } else if (cmbListenState.SelectionBoxItem.ToString().Equals("None") == true) { _publisher.Advertisement.ListenStateDiscoverability = WiFiDirectAdvertisementListenStateDiscoverability.None; } _publisher.StatusChanged += OnStatusChanged; _publisher.Start(); rootPage.NotifyUser("Advertisement started, waiting for StatusChanged callback...", NotifyType.StatusMessage); } catch (Exception ex) { rootPage.NotifyUser("Error starting Advertisement: " + ex.ToString(), NotifyType.ErrorMessage); } }
public void Start() { // Clean up old state Reset(); // Create WiFiDirectAdvertisementPublisher _publisher = new WiFiDirectAdvertisementPublisher(); // Add event handler for advertisement StatusChanged _publisher.StatusChanged += OnStatusChanged; // Set Advertisement required settings _advertisement = _publisher.Advertisement; // Must set the autonomous group owner (GO) enabled flag // Legacy Wi-Fi Direct advertisement uses a Wi-Fi Direct GO to act as an access point to legacy settings _advertisement.IsAutonomousGroupOwnerEnabled = true; _legacySettings = _advertisement.LegacySettings; // Must enable legacy settings so that non-Wi-Fi Direct peers can connect in legacy mode _legacySettings.IsEnabled = true; // Either specify an SSID, or read the randomly generated one if (_ssidProvided) { _legacySettings.Ssid = _ssid; } else { _ssid = _legacySettings.Ssid; } // Either specify a passphrase, or read the randomly generated one if (_passPhraseProvided) { _legacySettings.Passphrase.Password = _passPhrase; } else { _passPhrase = _legacySettings.Passphrase.Password; } _publisher.Start(); }
public WiFiDirect() { // 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 _publisher.Advertisement.LegacySettings.IsEnabled = true; _publisher.Advertisement.LegacySettings.Passphrase = new PasswordCredential(); try { var nw = new NativeWiFi(); SSID = nw.SSID; Key = nw.Key; } catch { } }
private void Reset() { if (_connectionListener != null) { _connectionListener.ConnectionRequested -= OnConnectionRequested; } if (_publisher != null) { _publisher.StatusChanged -= OnStatusChanged; if (_publisher.Status == WiFiDirectAdvertisementPublisherStatus.Created || _publisher.Status == WiFiDirectAdvertisementPublisherStatus.Started) { _publisher.Stop(); } } _connectionListener = null; _publisher = null; _legacySettings = null; _advertisement = null; _connectedDevices.Clear(); }
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 OnAdvertisementStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { if (args.Status == WiFiDirectAdvertisementPublisherStatus.Started) { _active = true; } else if (args.Status == WiFiDirectAdvertisementPublisherStatus.Stopped) { _active = false; } else if (args.Status == WiFiDirectAdvertisementPublisherStatus.Aborted) { _active = false; if (args.Error == WiFiDirectError.RadioNotAvailable) { // The radio was turned off. } else if (args.Error == WiFiDirectError.ResourceInUse) { // The stack couldn't accept any additional IEs. Need to turn off any other applications which could be advertising } } }
private void OnAdvertisementStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { if (args.Status == WiFiDirectAdvertisementPublisherStatus.Started) { _active = true; } else if (args.Status == WiFiDirectAdvertisementPublisherStatus.Stopped) { _active = false; } else if (args.Status == WiFiDirectAdvertisementPublisherStatus.Aborted) { _active = false; if (args.Error == WiFiDirectError.RadioNotAvailable) { // The radio was turned off. } else if (args.Error == WiFiDirectError.ResourceInUse) { // The stack couldn't accept any additional IEs. Need to turn off any other applications which could be advertising } } }
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 OnStatusChanged(WiFiDirectAdvertisementPublisher publisher, WiFiDirectAdvertisementPublisherStatusChangedEventArgs eventArgs) { try { switch (eventArgs.Status) { case WiFiDirectAdvertisementPublisherStatus.Started: { // Begin listening for connections and notify listener that the advertisement started StartListener(); if (_listener != null) { _listener.OnAdvertisementStarted(); } break; } case WiFiDirectAdvertisementPublisherStatus.Stopped: { // Notify listener that the advertisement is stopped if (_listener != null) { _listener.OnAdvertisementStopped("Advertisement stopped"); } break; } case WiFiDirectAdvertisementPublisherStatus.Aborted: { // Check error and notify listener that the advertisement stopped if (_listener != null) { string message; switch (eventArgs.Error) { case WiFiDirectError.RadioNotAvailable: message = "Advertisement aborted, Wi-Fi radio is turned off"; break; case WiFiDirectError.ResourceInUse: message = "Advertisement aborted, Resource In Use"; break; default: message = "Advertisement aborted, unknown reason"; break; } _listener.OnAdvertisementAborted(message); } break; } } } catch (Exception ex) { if (_listener != null) { _listener.OnAsyncException(ex.ToString()); } return; } }
public WiFiDirectAdvertisementPublisherEvents(WiFiDirectAdvertisementPublisher This) { this.This = This; }
private static void WifiPublisher_StatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { Debug.WriteLine("WiFi Hotspot status: " + args.Status.ToString()); }
void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs) { Debug.WriteLine("Advertisement: Status: " + statusEventArgs.Status.ToString() + " Error: " + statusEventArgs.Error.ToString()); }
private void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs) { rootPage.NotifyUserFromBackground("Advertisement: Status: " + statusEventArgs.Status.ToString() + " Error: " + statusEventArgs.Error.ToString(), NotifyType.StatusMessage); return; }
private void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs) { rootPage.NotifyUserFromBackground("Advertisement: Status: " + statusEventArgs.Status.ToString() + " Error: " + statusEventArgs.Error.ToString(), NotifyType.StatusMessage); return; }
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 async void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs statusEventArgs) { if (statusEventArgs.Status == WiFiDirectAdvertisementPublisherStatus.Started) { await Dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => { if (sender.Advertisement.LegacySettings.IsEnabled) { // Show the autogenerated passphrase and SSID. if (String.IsNullOrEmpty(txtPassphrase.Text)) { txtPassphrase.Text = _publisher.Advertisement.LegacySettings.Passphrase.Password; } if (String.IsNullOrEmpty(txtSsid.Text)) { txtSsid.Text = _publisher.Advertisement.LegacySettings.Ssid; } } }); } rootPage.NotifyUserFromBackground($"Advertisement: Status: {statusEventArgs.Status}, Error: {statusEventArgs.Error}", NotifyType.StatusMessage); return; }
private void btnStartAdvertisement_Click(object sender, RoutedEventArgs e) { _publisher = new WiFiDirectAdvertisementPublisher(); _publisher.StatusChanged += OnStatusChanged; _listener = new WiFiDirectConnectionListener(); if (chkListener.IsChecked.Value) { try { // This can raise an exception if the machine does not support WiFi. Sorry. _listener.ConnectionRequested += OnConnectionRequested; } catch (Exception ex) { rootPage.NotifyUser($"Error preparing Advertisement: {ex}", NotifyType.ErrorMessage); return; } } var discoverability = Utils.GetSelectedItemTag<WiFiDirectAdvertisementListenStateDiscoverability>(cmbListenState); _publisher.Advertisement.ListenStateDiscoverability = discoverability; _publisher.Advertisement.IsAutonomousGroupOwnerEnabled = chkPreferGroupOwnerMode.IsChecked.Value; // Legacy settings are meaningful only if IsAutonomousGroupOwnerEnabled is true. if (_publisher.Advertisement.IsAutonomousGroupOwnerEnabled && chkLegacySetting.IsChecked.Value) { _publisher.Advertisement.LegacySettings.IsEnabled = true; if (!String.IsNullOrEmpty(txtPassphrase.Text)) { var creds = new Windows.Security.Credentials.PasswordCredential(); creds.Password = txtPassphrase.Text; _publisher.Advertisement.LegacySettings.Passphrase = creds; } if (!String.IsNullOrEmpty(txtSsid.Text)) { _publisher.Advertisement.LegacySettings.Ssid = txtSsid.Text; } } // Add the information elements. foreach (WiFiDirectInformationElement informationElement in _informationElements) { _publisher.Advertisement.InformationElements.Add(informationElement); } _publisher.Start(); if (_publisher.Status == WiFiDirectAdvertisementPublisherStatus.Started) { btnStartAdvertisement.IsEnabled = false; btnStopAdvertisement.IsEnabled = true; rootPage.NotifyUser("Advertisement started.", NotifyType.StatusMessage); } else { rootPage.NotifyUser($"Advertisement failed to start. Status is {_publisher.Status}", NotifyType.ErrorMessage); } }
private void OnStatusChanged(WiFiDirectAdvertisementPublisher sender, WiFiDirectAdvertisementPublisherStatusChangedEventArgs args) { }