1

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 is a class member declared as:

BluetoothLEAdvertisementWatcher m_watcher;

The event handlers are declared as:

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");
}

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.

Thanks in advance.

EDIT: Changed the title adding ERROR_ACCESS_DENIED.

EDIT2: 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.

4

2 回答 2

2

解决方案资源管理器 -> Package.appxmanifest -> 功能 -> 蓝牙(检查)

于 2018-06-20T19:39:45.487 回答
1

最好设置 m_watcher.ScanningMode = Active; 在调用 Start() 之前;

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

Microsoft 写道,WinRT 必须使用 RO_INIT_MULTITHREADED 标志进行初始化。然而,我们的经验表明,使用其他标志也可以,但所有 WinRT 事件都将同步执行。不适合工作。

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

这是很重要的事情。

于 2018-06-21T07:10:10.527 回答