在C ++ Windows 10桌面应用程序中获取BLE信标

时间:2016-10-06 11:47:42

标签: c++ bluetooth windows-runtime uwp bluetooth-lowenergy

有人已经知道如何将BLE Beacons变成c ++桌面应用吗?

我从以下网站获得了一些代码,以便在c#中完成: msdn sozial site

codefest.at post.抱歉,它是德语,但代码是代码

但是对于C#而不是c ++

我也有MS的例子(msdn.microsoft.com/en-us/library/hh973459.aspx)如何使用WinRL

现在我有以下代码:

#include "stdafx.h"

#include <iostream>
#include <Windows.Foundation.h>
#include <wrl\wrappers\corewrappers.h>
#include <wrl\client.h>
#include <stdio.h>
#include <../winrt/windows.devices.bluetooth.h>
#include <../winrt/windows.devices.bluetooth.advertisement.h>

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<IBluetoothLEAdvertisementWatcherFactory> bleAdvWatcherFactory;
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementWatcher).Get(), &bleAdvWatcherFactory);
    if (FAILED(hr))
    {
        return PrintError(__LINE__, hr);
    }

    IBluetoothLEAdvertisementWatcher* bleWatcher;
    IBluetoothLEAdvertisementFilter* bleFilter;

    hr = bleAdvWatcherFactory->Create(bleFilter, &bleWatcher);

    if (bleWatcher == NULL)
    {
        cout << "bleWatcher is NULL, err is " << hex << hr;
    }
    else
    {
        bleWatcher->Start();

        while (1)
            Sleep(1000);
    }


    return 0;
    }

我的问题是Watcher Factory抱怨(hr = E_INVALIDARG&#34;一个或多个参数无效&#34; 0x80070057)其中一个变量无效(我怀疑过滤器因为它没有有效内容)。

即使在相同的严重程度,我也不知道如何为传入的信标注册事件处理程序。

msdn.microsoft.com/en-us/library/windows/apps/windows.devices.bluetooth.advertisement.bluetoothleadvertisementwatcher.received?cs-save-lang=1&cs-lang=cpp 告诉我一些关于Received事件的事情。但是我没有在VS的自动完成功能中使用它,也没有通过手动查看头文件。 我的下一个最好的事情是&#34; add_Received&#34;但我找不到任何关于它如何使用的文档。我不会从头文件中获得更明智的信息。

提前感谢提示和技巧,甚至是工作解决方案 马库斯

1 个答案:

答案 0 :(得分:1)

您的代码中几乎没有问题。首先,所有I*类都应该围绕ComPtr<I*>

ComPtr<IBluetoothLEAdvertisementWatcher> bleWatcher;
ComPtr<IBluetoothLEAdvertisementFilter> bleFilter;

在函数bleAdvWatcherFactory->Create(bleFilter, &bleWatcher);中,参数bleFilter未定义,它将导致未定义的行为(在Debug版本中,它可能被初始化为nullptr)。

您可以使用以下方法创建它: Wrappers :: HStringReference

Wrappers::HStringReference class_id_filter(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementFilter);
hr = RoActivateInstance(class_id_filter.Get(), reinterpret_cast<IInspectable**>(bleFilter.GetAddressOf()));

然后你可以打电话:

hr = bleAdvWatcherFactory->Create(bleFilter.Get(), &bleWatcher.GetAddressOf());