title: Getting BLE Beacons in C++ Windows 10 Desktop Application
body: <p>Has someone already figured out how to get BLE Beacons into a c++ desktop apps?</p>
  
  <p>I have some code from the following websites to get it done in c#:<br />
  <a href="https://social.msdn.microsoft.com/Forums/windowsserver/en-US/e321cb3c-462a-4b16-b7e4-febdb3d0c7d6/windows-10-pairing-a-ble-device-from-code?forum=wdk#f4d73ef6-a0d7-4f97-a9c8-bd6c52da10fb" rel="nofollow">msdn sozial site</a><br />
  and<br />
  <a href="http://www.codefest.at/post/2015/09/07/Bluetooth-Beacons-Windows-10.aspx" rel="nofollow">codefest.at post.</a> Sorry, it's in german but the code is code</p>
  
  <p>but that'for C# and not c++</p>
  
  <p>I also have the example from MS (msdn.microsoft.com/en-us/library/hh973459.aspx) how to use the WinRL</p>
  
  <p>For now I have the following code:</p>
  
  <pre><code>#include "stdafx.h"
  
  #include &lt;iostream&gt;
  #include &lt;Windows.Foundation.h&gt;
  #include &lt;wrl\wrappers\corewrappers.h&gt;
  #include &lt;wrl\client.h&gt;
  #include &lt;stdio.h&gt;
  #include &lt;../winrt/windows.devices.bluetooth.h&gt;
  #include &lt;../winrt/windows.devices.bluetooth.advertisement.h&gt;
  
  using namespace std;
  using namespace Microsoft::WRL;
  using namespace Microsoft::WRL::Wrappers;
  using namespace ABI::Windows::Foundation;
  using namespace ABI::Windows::Devices::Bluetooth::Advertisement;
  
  /* http://www.codefest.at/post/2015/09/07/Bluetooth-Beacons-Windows-10.aspx
  private async void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs eventArgs)
  {
  _beaconManager.ReceivedAdvertisement(eventArgs);
  }
  
  var watcher = new BluetoothLEAdvertisementWatcher { ScanningMode = BluetoothLEScanningMode.Active };
  watcher.Received += WatcherOnReceived;
  watcher.Stopped += WatcherOnStopped;
  watcher.Start();
  */
  
  // Prints an error string for the provided source code line and HRESULT
  // value and returns the HRESULT value as an int.
  int PrintError(unsigned int line, HRESULT hr)
  {
      wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr);
      return hr;
  }
  
  EventRegistrationToken watcherToken;
  
  int main()
  {
  
      // Initialize the Windows Runtime.
      RoInitializeWrapper initialize(RO_INIT_MULTITHREADED);
      if (FAILED(initialize))
      {
          return PrintError(__LINE__, initialize);
      }
  
      // Get the activation factory for the IBluetoothLEAdvertisementWatcherFactory interface.
      ComPtr&lt;IBluetoothLEAdvertisementWatcherFactory&gt; bleAdvWatcherFactory;
      HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementWatcher).Get(), &amp;bleAdvWatcherFactory);
      if (FAILED(hr))
      {
          return PrintError(__LINE__, hr);
      }
  
      IBluetoothLEAdvertisementWatcher* bleWatcher;
      IBluetoothLEAdvertisementFilter* bleFilter;
  
      hr = bleAdvWatcherFactory-&gt;Create(bleFilter, &amp;bleWatcher);
  
      if (bleWatcher == NULL)
      {
          cout &lt;&lt; "bleWatcher is NULL, err is " &lt;&lt; hex &lt;&lt; hr;
      }
      else
      {
          bleWatcher-&gt;Start();
  
          while (1)
              Sleep(1000);
      }
  
  
      return 0;
      }
  </code></pre>
  
  <p>my problem is that the Watcher Factory complains ( hr = E_INVALIDARG    "One or more arguments are not valid" 0x80070057) that one of the variables is not valid (I suspect the filter because it has no valid content).</p>
  
  <p>And even on the same level of severity, I have no idea how to register the event handler for the incoming beacons.</p>
  
  <p>msdn.microsoft.com/en-us/library/windows/apps/windows.devices.bluetooth.advertisement.bluetoothleadvertisementwatcher.received?cs-save-lang=1&amp;cs-lang=cpp
  is telling me something about the Received event. But I don't have it in my autocomplete from VS and also not by manually looking into the header file.
  The next best thing I have is the "add_Received" but I can't find any documentation about it how to use. And I don't get that much wiser from the header file.</p>
  
  <p>Thanks in advance for tips and tricks or even a working solution.<br />
  Markus</p>