gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-03 07:12:00 26 4
gpt4 key购买 nike

有人已经想出如何将 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 "One or more arguments are not valid"0x80070057)其中一个变量无效(我怀疑过滤器因为它没有有效内容)。

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

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

提前感谢您提供提示和技巧,甚至是可行的解决方案。
马库斯

最佳答案

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

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

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

您可以使用以下方法创建它:包装器::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());

关于c++ - 在 C++ Windows 10 桌面应用程序中获取 BLE 信标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39895191/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com