DeviceWatcher如何在WinRT / C ++中工作?

时间:2019-05-09 18:18:15

标签: c++ windows bluetooth-lowenergy c++-winrt

最近,我正在尝试为我的应用程序与设备实现低功耗蓝牙通信。一个应用程序正在Windows 10上运行。就目前而言,我的实现大致如下所示:

void BluetoothScaner::StartBleDeviceWatcher()
{
    // required properties
    auto requestedProperties = single_threaded_vector<hstring>({ L"System.Devices.Aep.DeviceAddress", L"System.Devices.Aep.IsConnected", L"System.Devices.Aep.Bluetooth.Le.IsConnectable" });
    // show paired and not paried devices
    hstring aqsAllBluetoothLEDevices = L"(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

    // create watcher
    deviceWatcher =
        Windows::Devices::Enumeration::DeviceInformation::CreateWatcher( aqsAllBluetoothLEDevices,
                                                                         requestedProperties,
            Windows::Devices::Enumeration::DeviceInformationKind::AssociationEndpoint);

    // register event handlers before starting the watcher.
    deviceWatcherAddedToken = deviceWatcher.Added(&deviceWatcher, &BluetoothScaner::DeviceWatcher_Added);
    deviceWatcherUpdatedToken = deviceWatcher.Updated(&deviceWatcher, &BluetoothScaner::DeviceWatcher_Updated);
    deviceWatcherRemovedToken = deviceWatcher.Removed(&deviceWatcher, &BluetoothScaner::DeviceWatcher_Removed);
    deviceWatcherEnumerationCompletedToken = deviceWatcher.EnumerationCompleted(&deviceWatcher, &BluetoothScaner::DeviceWatcher_EnumerationCompleted);
    deviceWatcherStoppedToken = deviceWatcher.Stopped(&deviceWatcher, &BluetoothScaner::DeviceWatcher_Stopped);

    deviceWatcher.Start();
}

winrt::fire_and_forget BluetoothScaner::DeviceWatcher_Added(Windows::Devices::Enumeration::DeviceWatcher sender, Windows::Devices::Enumeration::DeviceInformation deviceInfo)
{
        cout << "DeviceWatcher_Added";
        cout << deviceInfo.Name().c_str();
        cout << deviceInfo.Id().c_str();
        cout << unbox_value<hstring>deviceInfo.Properties().TryLookup(L"System.Devices.Aep.DeviceAddress").c_str();

    return winrt::fire_and_forget();
}

winrt::fire_and_forget BluetoothScaner::DeviceWatcher_Updated(Windows::Devices::Enumeration::DeviceWatcher sender, Windows::Devices::Enumeration::DeviceInformationUpdate deviceInfoUpdate)
{
        cout << "Updated";
    return winrt::fire_and_forget();
}

winrt::fire_and_forget BluetoothScaner::DeviceWatcher_Removed(Windows::Devices::Enumeration::DeviceWatcher sender, Windows::Devices::Enumeration::DeviceInformationUpdate deviceInfoUpdate)
{
        cout << "Removed";
        cout << deviceInfo.Id().c_str();
        cout << unbox_value<hstring>DeviceInformationUpdate.Properties().TryLookup(L"System.Devices.Aep.DeviceAddress").c_str();
    return winrt::fire_and_forget();
}

winrt::fire_and_forget BluetoothScaner::DeviceWatcher_EnumerationCompleted(Windows::Devices::Enumeration::DeviceWatcher sender, Windows::Foundation::IInspectable const &)
{
    return winrt::fire_and_forget();
}

winrt::fire_and_forget BluetoothScaner::DeviceWatcher_Stopped(Windows::Devices::Enumeration::DeviceWatcher sender, Windows::Foundation::IInspectable const &)
{
    return winrt::fire_and_forget();
}

输出:

DeviceWatcher_Added
ID: 0000022B8D046B7C
Name: 00007FFCD392A14C
Address: 0000022B8D061AFC
---------------------------------------------------------
DeviceWatcher_Updated
---------------------------------------------------------
DeviceWatcher_Added
ID: 0000022B8D04699C
Name: 00007FFCD392A14C
Address: 0000022B8D0620EC
---------------------------------------------------------
DeviceWatcher_Removed
ID: 0000022B8D04761C
---------------------------------------------------------

它基于以下示例:https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/BluetoothLE

我得到带有ID,设备名称和地址的输出,但是对于相同的设备,每次运行应用程序时,这些值都不同。另一个问题与扫描新设备/删除现有设备有关。当我尝试打开设备“ A”时,有时得到输出“ DeviceWatcher_Added ...”,有时却没有。即使删除了设备,删除设备时也会遇到同样的问题,即使关闭设备,有时也没有有关从列表中删除设备的信息。

据此我有几个问题:

  1. 如何获取正确的设备地址?
  2. 如何获取设备名称? (我已经在Windows扫描仪中检查了它(系统添加了新的蓝牙设备...),并且同一设备具有正确的名称字符串)
  3. DeviceWatcher如何工作?我不完全了解何时从列表中添加/删除设备。

环境信息: Windows 10,Visual Studio 2017,WSDK 10.0.17134.0

更新:的确,使用cout解决了获得设备正确名称/地址的问题(问题1和2)。我要澄清的最后一件事是添加/删除/更新事件的工作方式。如果调用了删除事件,则意味着该设备已从当前设备列表中删除,还是我必须自己做?我需要扫描仪来显示可连接的设备,例如。设备“ A”处于打开状态-如果我将其关闭,则该设备将显示在列表中-应该不再显示。

0 个答案:

没有答案