private void OnAdvertisingTextBoxTextChanged(object sender, TextChangedEventArgs e) { if (sender is TextBox) { TextBox textBox = sender as TextBox; string textBoxName = textBox.Name.ToLower(); string text = textBox.Text; if (textBoxName.StartsWith("beaconid1")) { int oldTextLength = text.Length; int oldCaretPosition = textBox.SelectionStart; BeaconId1 = BeaconFactory.FormatUuid(text); int newCaretPosition = oldCaretPosition + (BeaconId1.Length - oldTextLength); if (newCaretPosition > 0 && newCaretPosition <= BeaconId1.Length) { textBox.SelectionStart = newCaretPosition; } } else if (textBoxName.StartsWith("beaconid2")) { BeaconId2 = text; } else if (textBoxName.StartsWith("beaconid3")) { BeaconId3 = text; } } }
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); } } }); }
private void CalculateDistance(int rawSignalStrengthInDBm, int measuredPower) { if (rawSignalStrengthInDBm != 0 && measuredPower != 0) { Distance = Math.Round(BeaconFactory.CalculateDistanceFromRssi(rawSignalStrengthInDBm, measuredPower), 1); } }
/// <summary> /// Constructs the collection for beacons that we detect and the BLE beacon advertisement /// watcher. /// /// Hooks the events of the watcher and sets the watcher filter based on /// the manufacturer ID and beacon code. /// </summary> private void InitializeScanner() { BeaconCollection = new ObservableCollection <Beacon>(); _bluetoothLEAdvertisemenetWatcher = new BluetoothLEAdvertisementWatcher(); _bluetoothLEAdvertisemenetWatcher.Stopped += OnWatcherStoppedAsync; _bluetoothLEAdvertisemenetWatcher.Received += OnAdvertisemenetReceivedAsync; BluetoothLEManufacturerData manufacturerData = BeaconFactory.BeaconManufacturerData(ManufacturerId, BeaconCode); _bluetoothLEAdvertisemenetWatcher.AdvertisementFilter.Advertisement.ManufacturerData.Add(manufacturerData); }
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(); } }
public override string ToString() { return(BeaconFactory.FormatUuid(Id1) + ":" + Id2 + ":" + Id3); }