/// <summary> /// Invoked as an event handler when an advertisement is received. /// </summary> /// <param name="watcher">Instance of watcher that triggered the event.</param> /// <param name="eventArgs">Event data containing information about the advertisement event.</param> private async void OnAdvertisementReceived(BluetoothLEAdvertisementWatcher watcher, BluetoothLEAdvertisementReceivedEventArgs eventArgs) { // We can obtain various information about the advertisement we just received by accessing // the properties of the EventArgs class // The timestamp of the event DateTimeOffset timestamp = eventArgs.Timestamp; // The type of advertisement BluetoothLEAdvertisementType advertisementType = eventArgs.AdvertisementType; // The received signal strength indicator (RSSI) Int16 rssi = eventArgs.RawSignalStrengthInDBm; // The local name of the advertising device contained within the payload, if any string localName = eventArgs.Advertisement.LocalName; // Check if there are any manufacturer-specific sections. // If there is, print the raw data of the first manufacturer section (if there are multiple). string manufacturerDataString = ""; var manufacturerSections = eventArgs.Advertisement.ManufacturerData; if (manufacturerSections.Count > 0) { // Only print the first one of the list var manufacturerData = manufacturerSections[0]; var data = new byte[manufacturerData.Data.Length]; using (var reader = DataReader.FromBuffer(manufacturerData.Data)) { reader.ReadBytes(data); } // Print the company ID + the raw data in hex format manufacturerDataString = string.Format("0x{0}: {1}", manufacturerData.CompanyId.ToString("X"), //BitConverter.ToString(data)); System.Text.Encoding.UTF8.GetString(data)); } // Serialize UI update to the main UI thread await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Display these information on the list //ReceivedAdvertisementListBox.Items.Add(string.Format("[{0}]: type={1}, rssi={2}, name={3}, manufacturerData=[{4}]", // timestamp.ToString("hh\\:mm\\:ss\\.fff"), // advertisementType.ToString(), // rssi.ToString(), // localName, // manufacturerDataString)); ReceivedAdvertisementListBox.Items.Add(string.Format("[{0}]: rssi={1}, data='{2}'", timestamp.ToString("hh\\:mm\\:ss\\.fff"), rssi.ToString(), manufacturerDataString)); ReceivedAdvertisementListBox.ScrollIntoView(ReceivedAdvertisementListBox.Items[ReceivedAdvertisementListBox.Items.Count - 1]); }); }
/// <summary> /// Handle background task completion. /// </summary> /// <param name="task">The task that is reporting completion.</param> /// <param name="e">Arguments of the completion report.</param> private async void OnBackgroundTaskCompleted(BackgroundTaskRegistration task, BackgroundTaskCompletedEventArgs eventArgs) { // We get the advertisement(s) processed by the background task if (ApplicationData.Current.LocalSettings.Values.Keys.Contains(taskName)) { string backgroundMessage = (string)ApplicationData.Current.LocalSettings.Values[taskName]; // Serialize UI update to the main UI thread await this.Dispatcher.RunAsync(Windows.UI.Core.CoreDispatcherPriority.Normal, () => { // Display these information on the list ReceivedAdvertisementListBox.Items.Add(backgroundMessage); ReceivedAdvertisementListBox.ScrollIntoView(ReceivedAdvertisementListBox.Items[ReceivedAdvertisementListBox.Items.Count - 1]); }); } }