在C ++ / WinRT中使用BluetoothLEAdvertisementWatcher时为ERROR_ACCESS_DENIED [英] ERROR_ACCESS_DENIED while using BluetoothLEAdvertisementWatcher in C++/WinRT

查看:598
本文介绍了在C ++ / WinRT中使用BluetoothLEAdvertisementWatcher时为ERROR_ACCESS_DENIED的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用BluetoothLEAdvertisementWatcher检测BLE广告数据包。但是,在调用Start()时,观察者对象总是中止,并显示访问被拒绝错误。

I'm trying to use BluetoothLEAdvertisementWatcher to detect BLE advertising packets. However upon calling Start() the watcher object always aborts with 'Access is denied' error.

代码非常简单,如下所示:

Code is pretty simple and is as below:

auto filter = BluetoothLEAdvertisementFilter();
auto advert = BluetoothLEAdvertisement();
advert.LocalName(L"Greeny");
filter.Advertisement(advert);
m_watcher.AdvertisementFilter(filter);

// setup event handlers
m_watcher.Received({ this, &MainPage::OnAdvertisementRecieved });
m_watcher.Stopped({ this, &MainPage::OnAdvertisementStopped });

m_watcher.Start();

m_watcher是声明为的班级成员:

m_watcher is a class member declared as:

BluetoothLEAdvertisementWatcher m_watcher;

事件处理程序声明为:

void MainPage::OnAdvertisementRecieved(BluetoothLEAdvertisementWatcher const& watcher, BluetoothLEAdvertisementReceivedEventArgs const& args)
{
    OutputDebugString(L"Bluetooth advertisement received!\n");
}

void MainPage::OnAdvertisementStopped(Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher const & watcher, Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcherStoppedEventArgs const & args)
{
    OutputDebugString(L"Advertising stopped\n");
}

此代码与C#中SDK中的示例代码非常相似。我正在尝试用我更熟悉的C ++来做同样的事情。

This code is pretty similar to the sample code in the SDK which is in C#. I'm trying to do the same in C++ as I'm more familiar with it.

任何想法都会出问题吗?我尝试删除所有过滤器,也就是在默认状态下使用观察程序。那也给出相同的错误。删除事件处理程序也会导致相同的错误。

Any idea what could be going wrong? I tried removing all the filters, that is, using the watcher in its default state. That too gives the same error. Removing the event handlers also results in the same error.

预先感谢。

编辑:更改标题添加ERROR_ACCESS_DENIED。

Changed the title adding ERROR_ACCESS_DENIED.

EDIT2:发生错误的确切位置是:
onecoreuap\drivers\wdm\bluetooth\user\winrt\ \advertisement\bluetoothleadvertisementwatcher.cpp(1510)\Windows.Devices.Bluetooth.dll!0F479314:(caller:0F47AD40)Exception(2)tid(2c1c)80070005访问被拒绝。

The exact location where the error occurs is: onecoreuap\drivers\wdm\bluetooth\user\winrt\advertisement\bluetoothleadvertisementwatcher.cpp(1510)\Windows.Devices.Bluetooth.dll!0F479314: (caller: 0F47AD40) Exception(2) tid(2c1c) 80070005 Access is denied.

推荐答案

设置m_watcher.ScanningMode = Active为好主意;在调用Start()之前。

It is good idea to set m_watcher.ScanningMode = Active; before calling Start();

此外,如果您开发非UWP(经典)应用程序,则应确保WinRT正确初始化。作为基于COM技术的WinRT,最好在对COM进行任何其他调用之前初始化WinRT。但是,某些基于任何框架的应用程序(例如MFC,Delphi VCL或其他)都可以使用自定义参数在内部初始化COM。在这种情况下,最好从单独的线程调用RoInitialize(RO_INIT_MULTITHREADED)。

Also, if you develop non UWP (classic) application you should be sure that WinRT initialized correctly. As WinRT based on COM technologyy it is good to initialize WinRT before any other call to COM is made. However some applications based on any frameworks (for example, MFC, Delphi VCL or others) may initialize COM internally with custom parameters. In this case it is good idea to call RoInitialize(RO_INIT_MULTITHREADED) from separate thread.

Microsoft写道WinRT必须使用RO_INIT_MULTITHREADED标志进行初始化。但是,我们的经验表明,使用其他标志也可以,但是所有WinRT事件将随后同步执行。

Microsoft write that WinRT must be initialized with RO_INIT_MULTITHREADED flag. However our experience shows that usingother flags also works but all the WinRT events will be executed synchronous then. Not good for work.

最后,如果您开发UWP应用程序,请不要忘记,按照此处所述设置应用程序设备功能: https://docs.microsoft。 com / en-us / uwp / schemas / appxpackage / how-to-specify-device-capabilities-for-bluetoothoth

And finally if you develop UWP application do not forget to set application device capabilities as described there: https://docs.microsoft.com/en-us/uwp/schemas/appxpackage/how-to-specify-device-capabilities-for-bluetooth

这很重要。

这篇关于在C ++ / WinRT中使用BluetoothLEAdvertisementWatcher时为ERROR_ACCESS_DENIED的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆