private async void OnTogglePublisherButtonClickedAsync(object sender, RoutedEventArgs e) { if (_bluetoothLEAdvertisementPublisher.Status != BluetoothLEAdvertisementPublisherStatus.Started) { Beacon beacon = new Beacon(); beacon.ManufacturerId = ManufacturerId; beacon.Code = BeaconCode; beacon.Id1 = BeaconId1; try { beacon.Id2 = UInt16.Parse(BeaconId2); beacon.Id3 = UInt16.Parse(BeaconId3); } catch (Exception) { } beacon.MeasuredPower = -58; System.Diagnostics.Debug.WriteLine("Will try to advertise as beacon " + beacon.ToString()); BluetoothLEAdvertisementDataSection dataSection = BeaconFactory.BeaconToSecondDataSection(beacon); _bluetoothLEAdvertisementPublisher.Advertisement.DataSections.Add(dataSection); try { _bluetoothLEAdvertisementPublisher.Start(); } catch (Exception ex) { MessageDialog messageDialog = new MessageDialog("Failed to start Bluetooth LE Advertisement Publisher: " + ex.Message); await messageDialog.ShowAsync(); } } else { _bluetoothLEAdvertisementPublisher.Stop(); } }
private async void OnTogglePublisherButtonClickedAsync(object sender, RoutedEventArgs e) { if (_bluetoothLEAdvertisementPublisher.Status != BluetoothLEAdvertisementPublisherStatus.Started) { Beacon beacon = new Beacon(); beacon.ManufacturerId = ManufacturerId; beacon.Code = BeaconCode; beacon.Id1 = BeaconId1; try { beacon.Id2 = UInt16.Parse(BeaconId2); beacon.Id3 = UInt16.Parse(BeaconId3); } catch (Exception) { } beacon.MeasuredPower = -58; System.Diagnostics.Debug.WriteLine("Will try to advertise as beacon " + beacon.ToString()); BluetoothLEAdvertisementDataSection dataSection = BeaconFactory.BeaconToSecondDataSection(beacon); _bluetoothLEAdvertisementPublisher.Advertisement.DataSections.Add(dataSection); try { _bluetoothLEAdvertisementPublisher.Start(); } catch (Exception ex) { MessageDialog messageDialog = new MessageDialog("Failed to start Bluetooth LE Advertisement Publisher: " + ex.Message); await messageDialog.ShowAsync(); } } else { _bluetoothLEAdvertisementPublisher.Stop(); } }
private async void OnAdvertisemenetReceivedAsync(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args) { await Windows.ApplicationModel.Core.CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync( Windows.UI.Core.CoreDispatcherPriority.Normal, () => { Beacon beacon = BeaconFactory.BeaconFromBluetoothLEAdvertisementReceivedEventArgs(args); if (beacon != null) { System.Diagnostics.Debug.WriteLine("Received advertisement from beacon " + beacon.ToString()); bool existingBeaconUpdated = false; foreach (Beacon existingBeacon in BeaconCollection) { if (existingBeacon.Update(beacon)) { existingBeaconUpdated = true; beacon.Dispose(); } } if (!existingBeaconUpdated) { BeaconCollection.Add(beacon); } } }); }