|
| 1 | +//********************************************************* |
| 2 | +// |
| 3 | +// Copyright (c) Microsoft. All rights reserved. |
| 4 | +// This code is licensed under the MIT License (MIT). |
| 5 | +// THIS CODE IS PROVIDED *AS IS* WITHOUT WARRANTY OF |
| 6 | +// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING ANY |
| 7 | +// IMPLIED WARRANTIES OF FITNESS FOR A PARTICULAR |
| 8 | +// PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. |
| 9 | +// |
| 10 | +//********************************************************* |
| 11 | + |
| 12 | +#include "pch.h" |
| 13 | +#include "BackgroundTasks.h" |
| 14 | +#include "AdvertisementWatcherTask.g.cpp" |
| 15 | +#include "AdvertisementPublisherTask.g.cpp" |
| 16 | +#include "SampleConfiguration.h" |
| 17 | +#include <chrono> |
| 18 | +#include <sstream> |
| 19 | + |
| 20 | +namespace winrt |
| 21 | +{ |
| 22 | + using namespace winrt::Windows::ApplicationModel::Background; |
| 23 | + using namespace winrt::Windows::Devices::Bluetooth; |
| 24 | + using namespace winrt::Windows::Devices::Bluetooth::Background; |
| 25 | + using namespace winrt::Windows::Devices::Bluetooth::Advertisement; |
| 26 | + using namespace winrt::Windows::Foundation; |
| 27 | + using namespace winrt::Windows::Foundation::Collections; |
| 28 | + using namespace winrt::Windows::Globalization::DateTimeFormatting; |
| 29 | + using namespace winrt::Windows::Storage; |
| 30 | + using namespace winrt::Windows::Storage::Streams; |
| 31 | +} |
| 32 | + |
| 33 | +namespace winrt::SDKTemplate::implementation |
| 34 | +{ |
| 35 | + std::wstring FormatOptionalInt16(IReference<int16_t> const& value) |
| 36 | + { |
| 37 | + if (value) |
| 38 | + { |
| 39 | + return std::to_wstring(value.Value()); |
| 40 | + } |
| 41 | + else |
| 42 | + { |
| 43 | + return L"none"; |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + std::wstring FormatOptionalTimeSpanMs(IReference<TimeSpan> const& span) |
| 48 | + { |
| 49 | + if (span) |
| 50 | + { |
| 51 | + return std::to_wstring(std::chrono::duration_cast<std::chrono::milliseconds>(span.Value()).count()); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + return L"none"; |
| 56 | + } |
| 57 | + } |
| 58 | + |
| 59 | + /// <summary> |
| 60 | + /// The entry point of a background task. |
| 61 | + /// </summary> |
| 62 | + /// <param name="taskInstance">The current background task instance.</param> |
| 63 | + void AdvertisementWatcherTask::Run(IBackgroundTaskInstance const& taskInstance) |
| 64 | + { |
| 65 | + watcherTaskInstance = taskInstance; |
| 66 | + |
| 67 | + auto details = taskInstance.TriggerDetails().as<BluetoothLEAdvertisementWatcherTriggerDetails>(); |
| 68 | + |
| 69 | + // In this example, the background task simply constructs a message communicated |
| 70 | + // to the App. For more interesting applications, a notification can be sent from here instead. |
| 71 | + std::wostringstream ss; |
| 72 | + |
| 73 | + // If the background watcher stopped unexpectedly, an error will be available here. |
| 74 | + BluetoothError error = details.Error(); |
| 75 | + if (error != BluetoothError::Success) |
| 76 | + { |
| 77 | + ss << L"Error: " << to_hstring(error) << L", "; |
| 78 | + } |
| 79 | + |
| 80 | + // The Advertisements property is a list of all advertisement events received |
| 81 | + // since the last task triggered. The list of advertisements here might be valid even if |
| 82 | + // the Error status is not Success since advertisements are stored until this task is triggered |
| 83 | + IVectorView<BluetoothLEAdvertisementReceivedEventArgs> advertisements = details.Advertisements(); |
| 84 | + ss << L"EventCount: " << advertisements.Size() << L", "; |
| 85 | + |
| 86 | + // The signal strength filter configuration of the trigger is returned such that further |
| 87 | + // processing can be performed here using these values if necessary. They are read-only here. |
| 88 | + auto rssiFilter = details.SignalStrengthFilter(); |
| 89 | + ss << L"HighDBm: " << FormatOptionalInt16(rssiFilter.InRangeThresholdInDBm()) << L", "; |
| 90 | + ss << L"LowDBm: " << FormatOptionalInt16(rssiFilter.OutOfRangeThresholdInDBm()) << L", "; |
| 91 | + ss << L"Timeout (ms): " << FormatOptionalTimeSpanMs(rssiFilter.OutOfRangeTimeout()) << L", "; |
| 92 | + ss << L"Sampling (ms): " << FormatOptionalTimeSpanMs(rssiFilter.SamplingInterval()); |
| 93 | + |
| 94 | + DateTimeFormatter timestampFormatter(L"longtime"); |
| 95 | + |
| 96 | + // Advertisements can contain multiple events that were aggregated, each represented by |
| 97 | + // a BluetoothLEAdvertisementReceivedEventArgs object. |
| 98 | + for (auto const& advertisementEventArgs : advertisements) |
| 99 | + { |
| 100 | + ss << L"\n[" + timestampFormatter.Format(advertisementEventArgs.Timestamp()) + L"] "; |
| 101 | + ss << L"[" + to_hstring(advertisementEventArgs.AdvertisementType()) + L"]: "; |
| 102 | + ss << L"Rssi=" << advertisementEventArgs.RawSignalStrengthInDBm() << L"dBm"; |
| 103 | + if (hstring name = advertisementEventArgs.Advertisement().LocalName(); !name.empty()) |
| 104 | + { |
| 105 | + ss << L", localName=" << name; |
| 106 | + } |
| 107 | + // Check if there are any manufacturer-specific sections. |
| 108 | + // If there is, print the raw data of the first manufacturer section (if there are multiple). |
| 109 | + auto manufacturerData = advertisementEventArgs.Advertisement().ManufacturerData(); |
| 110 | + if (manufacturerData.Size() > 0) |
| 111 | + { |
| 112 | + // Print the first one of the list |
| 113 | + ss << L", manufacturerData=[" << Utilities::FormatManufacturerData(manufacturerData.GetAt(0)) << L"]"; |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + // Store the message in a local settings indexed by this task's name so that the foreground App |
| 118 | + // can display this message. |
| 119 | + ApplicationData::Current().LocalSettings().Values().Insert(taskInstance.Task().Name(), winrt::box_value(ss.str())); |
| 120 | + } |
| 121 | + |
| 122 | + /// <summary> |
| 123 | + /// The entry point of a background task. |
| 124 | + /// </summary> |
| 125 | + /// <param name="taskInstance">The current background task instance.</param> |
| 126 | + void AdvertisementPublisherTask::Run(IBackgroundTaskInstance const& taskInstance) |
| 127 | + { |
| 128 | + publisherTaskInstance = taskInstance; |
| 129 | + |
| 130 | + auto details = taskInstance.TriggerDetails().as<BluetoothLEAdvertisementPublisherTriggerDetails>(); |
| 131 | + |
| 132 | + // In this example, the background task simply constructs a message communicated |
| 133 | + // to the App. For more interesting applications, a notification can be sent from here instead. |
| 134 | + std::wostringstream ss; |
| 135 | + |
| 136 | + // If the background publisher stopped unexpectedly, an error will be available here. |
| 137 | + BluetoothError error = details.Error(); |
| 138 | + if (error != BluetoothError::Success) |
| 139 | + { |
| 140 | + ss << L"Error: " << to_hstring(error) << L", "; |
| 141 | + } |
| 142 | + |
| 143 | + // The status of the publisher is useful to determine whether the advertisement payload is being serviced |
| 144 | + // It is possible for a publisher to stay in a Waiting state while radio resources are in use. |
| 145 | + BluetoothLEAdvertisementPublisherStatus status = details.Status(); |
| 146 | + ss << L"Publisher status: " + to_hstring(status); |
| 147 | + |
| 148 | + // Store the message in a local settings indexed by this task's name so that the foreground App |
| 149 | + // can display this message. |
| 150 | + ApplicationData::Current().LocalSettings().Values().Insert(taskInstance.Task().Name(), winrt::box_value(ss.str())); |
| 151 | + } |
| 152 | +} |
| 153 | + |
0 commit comments