微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

用 C++ 制作一个 BLE DLL

如何解决用 C++ 制作一个 BLE DLL

自从我尝试创建一个 DLL 以允许我与 BluetoothLE 中的设备进行通信以来,已经有几个星期了。事实上,我正在开发一个用 C++ 编码的软件,它允许通过加密狗与以太网、USB 和 BluetoothLE 中的工业设备进行通信。我想摆脱加密狗并使用 PC 的 BluetoothLE。

我从以下 Microsoft 项目开始:

https://github.com/microsoft/Windows-universal-samples/tree/master/Samples/BluetoothLE/cppwinrt

现在我尝试从头开始制作自己的项目,以掌握所有必要的库和项目配置。我也想摆脱所有的图形部分

然而,我无处可去,开始失去希望......

我以前从未使用过 WinRT。

我从做一个通用 Windows 项目开始。

这是我的 .h :

    class BLE_discovery_dll
{
private:
    static BLE_discovery_dll* pinstance_;

    std::vector<Windows::Devices::Enumeration::Deviceinformation> UnkNownDevices;
    Windows::Devices::Enumeration::DeviceWatcher deviceWatcher{ nullptr };
    event_token deviceWatcherAddedToken;
    event_token deviceWatcherUpdatedToken;
    event_token deviceWatcherRemovedToken;
    event_token deviceWatcherEnumerationCompletedToken;
    event_token deviceWatcherStoppedToken;

    void StartBleDeviceWatcher();
    void StopBleDeviceWatcher();

    fire_and_forget DeviceWatcher_Added(Windows::Devices::Enumeration::DeviceWatcher sender,Windows::Devices::Enumeration::Deviceinformation deviceInfo);
    fire_and_forget DeviceWatcher_Updated(Windows::Devices::Enumeration::DeviceWatcher sender,Windows::Devices::Enumeration::DeviceinformationUpdate deviceInfoUpdate);
    fire_and_forget DeviceWatcher_Removed(Windows::Devices::Enumeration::DeviceWatcher sender,Windows::Devices::Enumeration::DeviceinformationUpdate deviceInfoUpdate);
    fire_and_forget DeviceWatcher_EnumerationCompleted(Windows::Devices::Enumeration::DeviceWatcher sender,Windows::Foundation::IInspectable const&);
    fire_and_forget DeviceWatcher_Stopped(Windows::Devices::Enumeration::DeviceWatcher sender,Windows::Foundation::IInspectable const&);

    std::vector<Windows::Devices::Enumeration::Deviceinformation>::iterator FindUnkNownDevices(hstring const& id);
    void addToDeviceCollection(Deviceinformation deviceInfo);
    void removetoDeviceCollection(DeviceinformationUpdate deviceInfoUpdate);
    BLEDevice* findindeviceCollection(hstring const& id);

protected:
    BLE_discovery_dll()
    {
    }
    ~BLE_discovery_dll() {}

public:
    BLE_discovery_dll(BLE_discovery_dll& other) = delete;
    void operator=(const BLE_discovery_dll&) = delete;
    static BLE_discovery_dll* GetInstance();

    void ScanEvent();
};

和我的 .cpp

    #include "pch.h"
#include "BLE_discovery_dll.h"

using namespace winrt;
using namespace Windows::Devices::Enumeration;
using namespace Windows::Foundation;
using namespace Windows::UI::Core;
using namespace Windows::UI::Xaml;
using namespace Windows::UI::Xaml::Navigation;

BLE_discovery_dll::BLE_discovery_dll()
{
}

BLE_discovery_dll::~BLE_discovery_dll()
{
}

BLE_discovery_dll* BLE_discovery_dll::GetInstance()
{
    if (pinstance_ == nullptr)
    {
        pinstance_ = new BLE_discovery_dll();
    }
    return pinstance_;
}

/// <summary>
///     Event when scan is requested
/// </summary>
void BLE_discovery_dll::ScanEvent()
{
    if (deviceWatcher == nullptr)
    {
        StartBleDeviceWatcher();
    }
    else
    {
        StopBleDeviceWatcher();
    }
}

/// <summary>
///     Starts a device watcher that looks for all nearby Bluetooth devices (paired or unpaired). 
///     Attaches event handlers to populate the device collection.
/// </summary>
void BLE_discovery_dll::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,&BLE_discovery_dll::DeviceWatcher_Added);
    deviceWatcherUpdatedToken = deviceWatcher.Updated(&deviceWatcher,&BLE_discovery_dll::DeviceWatcher_Updated);
    deviceWatcherRemovedToken = deviceWatcher.Removed(&deviceWatcher,&BLE_discovery_dll::DeviceWatcher_Removed);
    deviceWatcherEnumerationCompletedToken = deviceWatcher.EnumerationCompleted(&deviceWatcher,&BLE_discovery_dll::DeviceWatcher_EnumerationCompleted);
    deviceWatcherStoppedToken = deviceWatcher.Stopped(&deviceWatcher,&BLE_discovery_dll::DeviceWatcher_Stopped);

    deviceWatcher.Start();
}

/// <summary>
///     Stops watching for all nearby Bluetooth devices.
/// </summary>
void BLE_discovery_dll::StopBleDeviceWatcher()
{
    if (deviceWatcher != nullptr)
    {
        // Unregister the event handlers.
        deviceWatcher.Added(deviceWatcherAddedToken);
        deviceWatcher.Updated(deviceWatcherUpdatedToken);
        deviceWatcher.Removed(deviceWatcherRemovedToken);
        deviceWatcher.EnumerationCompleted(deviceWatcherEnumerationCompletedToken);
        deviceWatcher.Stopped(deviceWatcherStoppedToken);

        // Stop the watcher.
        deviceWatcher.Stop();
        deviceWatcher = nullptr;
    }
}

/// <summary>
///     
/// </summary>
fire_and_forget BLE_discovery_dll::DeviceWatcher_Added(DeviceWatcher sender,Deviceinformation deviceInfo)
{
    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
    if (sender == deviceWatcher)
    {
        // Make sure device isn't already present in the list.
        if (findindeviceCollection(deviceInfo.Id()) == nullptr)
        {
            if (!deviceInfo.Name().empty())
            {
                // If device has a friendly name display it immediately.
                //IMPLEMENT addToDeviceCollection(deviceInfo);
            }
            else
            {
                // Add it to a list in case the name gets updated later. 
                //IMPLEMENT UnkNownDevices.Add(deviceInfo);
            }
        }
    }
}

fire_and_forget BLE_discovery_dll::DeviceWatcher_Updated(DeviceWatcher sender,DeviceinformationUpdate deviceInfoUpdate)
{
    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
    if (sender == deviceWatcher)
    {
        BLEDevice* bleDevice = findindeviceCollection(deviceInfoUpdate.Id());
        if (bleDevice != nullptr)
        {
            // Device is already being displayed - update UX.
            bleDevice->Update(deviceInfoUpdate);
            return;
        }

        auto deviceInfo = FindUnkNownDevices(deviceInfoUpdate.Id());
        if (deviceInfo != UnkNownDevices.end())
        {
            deviceInfo->Update(deviceInfoUpdate);
            // If device has been updated with a friendly name it's no longer unkNown.
            if (!deviceInfo->Name().empty())
            {
                //IMPLEMENT addToDeviceCollection(deviceInfo);
                UnkNownDevices.erase(deviceInfo);
            }
        }
    }
}

fire_and_forget BLE_discovery_dll::DeviceWatcher_Removed(DeviceWatcher sender,DeviceinformationUpdate deviceInfoUpdate)
{
    // Protect against race condition if the task runs after the app stopped the deviceWatcher.
    if (sender == deviceWatcher)
    {
        // Find the corresponding Deviceinformation in the collection and remove it.
        BLEDevice* bleDevice = findindeviceCollection(deviceInfoUpdate.Id());
        if (bleDevice != nullptr)
        {
            //IMPLEMENT removetoDeviceCollection(deviceInfo);
        }

        auto deviceInfo = FindUnkNownDevices(deviceInfoUpdate.Id());
        if (deviceInfo != UnkNownDevices.end())
        {
            //IMPLEMENT UnkNownDevices.remove(deviceInfo);
        }
    }
}

fire_and_forget BLE_discovery_dll::DeviceWatcher_EnumerationCompleted(DeviceWatcher sender,IInspectable const&)
{
    //IMPLEMENT
}

fire_and_forget BLE_discovery_dll::DeviceWatcher_Stopped(DeviceWatcher sender,IInspectable const&)
{
    //IMPLEMENT
}

/// <summary>
///     
/// </summary>
std::vector<Windows::Devices::Enumeration::Deviceinformation>::iterator BLE_discovery_dll::FindUnkNownDevices(hstring const& id)
{
    return std::find_if(UnkNownDevices.begin(),UnkNownDevices.end(),[&](auto&& bleDeviceInfo)
        {
            return bleDeviceInfo.Id() == id;
        });
}

/// <summary>
///     
/// </summary>
void BLE_discovery_dll::addToDeviceCollection(Deviceinformation deviceInfo)
{
    //IMPLEMENT
}

/// <summary>
///     
/// </summary>
void BLE_discovery_dll::removetoDeviceCollection(DeviceinformationUpdate deviceInfoUpdate)
{
    //IMPLEMENT
}

/// <summary>
///     
/// </summary>
BLEDevice* BLE_discovery_dll::findindeviceCollection(hstring const& id)
{
    //IMPLEMENT
}

首先,在我的 .cpp 文件中,我在注册事件处理程序(deviceWatcher.Added()、deviceWatcher.Updated()...)时收到此错误消息:

没有重载函数的实例与参数列表匹配

然后,当我运行编译时,我会收到如下消息: 'Deviceinformation':未声明的标识符 'DeviceWatcher':未声明的标识符

我不知道该怎么办。感觉做一件很简单的事情都做不下去了……不过,除了微软的那个,我没有找到其他的功能例子。

如果你对我有什么建议,你就是我唯一的希望。

谢谢:)

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。