Windows::Devices::Enumeration::DeviceInformation::CreateWatcher in C++ without WinRT namespace

42 Views Asked by At

I am making Windows app that connects to a BLE device. App is made in C++ (due to allot of legacy code). Overall the app is OK, the only problem I have is scanning for devices nearby.

Using BluetoothLEAdvertisementWatcher is not very stable and seems it has number of know problems. For example it is not scanning and if you open Windows Bluetooth settings and do a scan it starts working. Reading I found that Windows::Devices::Enumeration::DeviceWatcher is better option.

But I cannot get Windows::Devices::Enumeration::DeviceWatcher working in C (I got it all good in C#). Please see the codes bellow:

C#

            string BTLEDeviceWatcherAQSString = "(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

        // Query for extra properties you want returned
        string[] requestedProperties =
            {
            "System.Devices.Aep.Category",
            "System.Devices.Aep.ContainerId",
            "System.Devices.Aep.DeviceAddress",
            "System.Devices.Aep.IsConnected",
            "System.Devices.Aep.IsPaired",
            "System.Devices.Aep.IsPresent",
            "System.Devices.Aep.ProtocolId",
            "System.Devices.Aep.Bluetooth.Le.IsConnectable",
            "System.Devices.Aep.SignalStrength"
        };

        DeviceWatcher deviceWatcher =
                    DeviceInformation.CreateWatcher(
                            BTLEDeviceWatcherAQSString,
                            requestedProperties,
                            DeviceInformationKind.AssociationEndpoint);

works and finds device.

same code C++ using winrt namespace:

    auto requestedProperties = single_threaded_vector<hstring>({ L"System.Devices.Aep.DeviceAddress", L"System.Devices.Aep.IsConnected", L"System.Devices.Aep.Bluetooth.Le.IsConnectable" });

// BT_Code: Example showing paired and non-paired in a single query.
hstring aqsAllBluetoothLEDevices = L"(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")";

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

fails to compile. I have read allot, but nothing can get it working due to the following error:

Error C1197 cannot reference 'C:\Program Files (x86)\Windows Kits\10\References\10.0.22621.0\Windows.Foundation.FoundationContract\4.0.0.0\Windows.Foundation.FoundationContract.winmd' as the program has already referenced 'C:\Program Files (x86)\Windows Kits\10\UnionMetadata\10.0.22621.0\Windows.winmd'

same code without winrt namespace:

    Windows::Foundation::Collections::IVector<Platform::String^>^ atts = ref new Vector<Platform::String^>();
ss->Append(L"System.Devices.Aep.ContainerId");
ss->Append(L"System.Devices.Aep.DeviceAddress");
Windows::Devices::Enumeration::DeviceWatcher^ deviceWatcher =
    Windows::Devices::Enumeration::DeviceInformation::CreateWatcher(
    L"(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")",
    atts
    );

compiles and works, but doesn't find any devices :)

0

There are 0 best solutions below