尝试为 Windows C++ 创建 Gatt 客户端应用程序,该应用程序在建立连接时不会失败

Mar*_*mán 6 c++ bluetooth hresult bluetooth-lowenergy bluetooth-gatt

我正在使用用于 C++的 Windows api Gatt Client BLE,我的目标是连接两个设备(但在这种情况下我将只尝试一个)并在不关闭设备的情况下不断读取和写入数据。我所有的设备都有一个特定的服务,其中包含一个读取特性和一个写入特性。

如何测试:

使用 Visual Studio 2017 (v141) 和 Windows SDK 版本:10.0.18362.0,创建一个新的控制台 (.exe) 解决方案,将 Project -> Properties 中的 Platform 更改为 Win32 并转到 Project -> Properties -> C/C++ ->命令行并添加以下选项:

/std:c++17 /await 
Run Code Online (Sandbox Code Playgroud)

然后将以下代码复制到一个文件中(您可以将所有代码复制到同一个 .cpp 文件中):

#pragma once
#include <SDKDDKVer.h>
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#include <iostream>
#include <queue>
#include <map>
#include <mutex>
#include <condition_variable>
#include <string>

#include <winrt/Windows.Foundation.Collections.h>
#include <winrt/Windows.Web.Syndication.h>

#include "winrt/Windows.Devices.Bluetooth.h"
#include "winrt/Windows.Devices.Bluetooth.GenericAttributeProfile.h"
#include "winrt/Windows.Devices.Enumeration.h"

#include "winrt/Windows.Storage.Streams.h"

#pragma comment(lib, "windowsapp")


using namespace std;

using namespace winrt;
using namespace Windows::Foundation;
using namespace Windows::Foundation::Collections;
using namespace Windows::Web::Syndication;

using namespace Windows::Devices::Bluetooth;
using namespace Windows::Devices::Bluetooth::GenericAttributeProfile;
using namespace Windows::Devices::Enumeration;

using namespace Windows::Storage::Streams;

#pragma region STRUCS AND ENUMS

#define LOG_ERROR(e) cout << e << endl;

union to_guid
{
    uint8_t buf[16];
    guid guid;
};

const uint8_t BYTE_ORDER[] = { 3, 2, 1, 0, 5, 4, 7, 6, 8, 9, 10, 11, 12, 13, 14, 15 };

guid make_guid(const wchar_t* value)
{
    to_guid to_guid;
    memset(&to_guid, 0, sizeof(to_guid));
    int offset = 0;
    for (unsigned int i = 0; i < wcslen(value); i++) {
        if (value[i] >= '0' && value[i] <= '9')
        {
            uint8_t digit = value[i] - '0';
            to_guid.buf[BYTE_ORDER[offset / 2]] += offset % 2 == 0 ? digit << 4 : digit;
            offset++;
        }
        else if (value[i] >= 'A' && value[i] <= 'F')
        {
            uint8_t digit = 10 + value[i] - 'A';
            to_guid.buf[BYTE_ORDER[offset / 2]] += offset % 2 == 0 ? digit << 4 : digit;
            offset++;
        }
        else if (value[i] >= 'a' && value[i] <= 'f')
        {
            uint8_t digit = 10 + value[i] - 'a';
            to_guid.buf[BYTE_ORDER[offset / 2]] += offset % 2 == 0 ? digit << 4 : digit;
            offset++;
        }
        else
        {
            // skip char
        }
    }

    return to_guid.guid;
}


mutex subscribeLock;
condition_variable subscribeSignal;

mutex _mutexWrite;
condition_variable signalWrite;

struct DeviceCacheEntry {
    BluetoothLEDevice device = nullptr;
    GattDeviceService service = nullptr;
    GattCharacteristic characteristic = nullptr;
};
map<wstring, DeviceCacheEntry> cache;

struct Subscription {
    GattCharacteristic::ValueChanged_revoker revoker;
};

struct BLEDeviceData {
    wstring id;
    wstring name;
    bool isConnectable = false;
    Subscription* subscription = NULL;
};
vector<BLEDeviceData> deviceList{};

mutex deviceListLock;
condition_variable deviceListSignal;

#pragma endregion

#pragma region CACHE FUNCTIONS

//Call this function to get a device from cache or async if it wasn't found
IAsyncOperation<BluetoothLEDevice> getDevice(wchar_t* deviceId) {
    if (cache.count(wstring(deviceId)) && cache[wstring(deviceId)].device)
        co_return cache[wstring(deviceId)].device;
    BluetoothLEDevice result = co_await BluetoothLEDevice::FromIdAsync(deviceId);
    if (result == nullptr) {
        LOG_ERROR("Failed to connect to device.")
            co_return nullptr;
    }
    else {
        DeviceCacheEntry d;
        d.device = result;
        if (!cache.count(wstring(deviceId))) {
            cache.insert({ wstring(deviceId), d });
        }
        else {
            cache[wstring(deviceId)] = d;
        }
        co_return cache[wstring(deviceId)].device;
    }
}

//Call this function to get a service from cache or async if it wasn't found
IAsyncOperation<GattDeviceService> getService(wchar_t* deviceId, wchar_t* serviceId) {
    if (cache.count(wstring(deviceId)) && cache[wstring(deviceId)].service)
        co_return cache[wstring(deviceId)].service;
    auto device = co_await getDevice(deviceId);
    if (device == nullptr)
        co_return nullptr;
    GattDeviceServicesResult result = co_await device.GetGattServicesForUuidAsync(make_guid(serviceId), BluetoothCacheMode::Cached);
    if (result.Status() != GattCommunicationStatus::Success) {
        LOG_ERROR("Failed getting services. Status: " << (int)result.Status())
            co_return nullptr;
    }
    else if (result.Services().Size() == 0) {
        LOG_ERROR("No service found with uuid")
            co_return nullptr;
    }
    else {
        if (cache.count(wstring(deviceId))) {
            cache[wstring(deviceId)].service = result.Services().GetAt(0);
        }
        co_return cache[wstring(deviceId)].service;
    }
}

//Call this function to get a characteristic from cache or async if it wasn't found
IAsyncOperation<GattCharacteristic> getCharacteristic(wchar_t* deviceId, wchar_t* serviceId, wchar_t* characteristicId) {
    try {
        if (cache.count(wstring(deviceId)) && cache[wstring(deviceId)].characteristic)
            co_return cache[wstring(deviceId)].characteristic;
        auto service = co_await getService(deviceId, serviceId);
        if (service == nullptr)
            co_return nullptr;
        GattCharacteristicsResult result = co_await service.GetCharacteristicsForUuidAsync(make_guid(characteristicId), BluetoothCacheMode::Cached);
        if (result.Status() != GattCommunicationStatus::Success) {
            LOG_ERROR("Error scanning characteristics from service. Status: " << (int)result.Status())
                co_return nullptr;
        }
        else if (result.Characteristics().Size() == 0) {
            LOG_ERROR("No characteristic found with uuid")
                co_return nullptr;
        }
        else {
            if (cache.count(wstring(deviceId))) {
                cache[wstring(deviceId)].characteristic = result.Characteristics().GetAt(0);
            }
            co_return cache[wstring(deviceId)].characteristic;
        }
    }
    catch (...) {
        LOG_ERROR("Exception while trying to get characteristic")
    }
}

#pragma endregion

#pragma region SCAN DEVICES FUNCTIONS

DeviceWatcher deviceWatcher{ nullptr };
mutex deviceWatcherLock;
DeviceWatcher::Added_revoker deviceWatcherAddedRevoker;
DeviceWatcher::Updated_revoker deviceWatcherUpdatedRevoker;
DeviceWatcher::Removed_revoker deviceWatcherRemovedRevoker;
DeviceWatcher::EnumerationCompleted_revoker deviceWatcherCompletedRevoker;

struct TestBLE {
    static void ScanDevices();
    static void StopDeviceScan();
};

//This function would be called when a new BLE device is detected
void DeviceWatcher_Added(DeviceWatcher sender, DeviceInformation deviceInfo) {
    BLEDeviceData deviceData;
    deviceData.id = wstring(deviceInfo.Id().c_str());
    deviceData.name = wstring(deviceInfo.Name().c_str());
    if (deviceInfo.Properties().HasKey(L"System.Devices.Aep.Bluetooth.Le.IsConnectable")) {
        deviceData.isConnectable = unbox_value<bool>(deviceInfo.Properties().Lookup(L"System.Devices.Aep.Bluetooth.Le.IsConnectable"));
    }
    deviceList.push_back(deviceData);
}

//This function would be called when an existing BLE device is updated
void DeviceWatcher_Updated(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
    wstring deviceData = wstring(deviceInfoUpdate.Id().c_str());
    for (int i = 0; i < deviceList.size(); i++) {
        if (deviceList[i].id == deviceData) {
            if (deviceInfoUpdate.Properties().HasKey(L"System.Devices.Aep.Bluetooth.Le.IsConnectable")) {
                deviceList[i].isConnectable = unbox_value<bool>(deviceInfoUpdate.Properties().Lookup(L"System.Devices.Aep.Bluetooth.Le.IsConnectable"));
            }
            break;
        }
    }
}

void DeviceWatcher_Removed(DeviceWatcher sender, DeviceInformationUpdate deviceInfoUpdate) {
    
}

void DeviceWatcher_EnumerationCompleted(DeviceWatcher sender, IInspectable const&) {
    TestBLE::StopDeviceScan();
    TestBLE::ScanDevices();
}

//Call this function to scan async all BLE devices
void TestBLE::ScanDevices() {
    try {
        lock_guard lock(deviceWatcherLock);
        IVector<hstring> requestedProperties = single_threaded_vector<hstring>({ L"System.Devices.Aep.DeviceAddress", L"System.Devices.Aep.IsConnected", L"System.Devices.Aep.Bluetooth.Le.IsConnectable" });
        hstring aqsFilter = L"(System.Devices.Aep.ProtocolId:=\"{bb7bb05e-5972-42b5-94fc-76eaa7084d49}\")"; // list Bluetooth LE devices
        deviceWatcher = DeviceInformation::CreateWatcher(aqsFilter, requestedProperties, DeviceInformationKind::AssociationEndpoint);
        deviceWatcherAddedRevoker = deviceWatcher.Added(auto_revoke, &DeviceWatcher_Added);
        deviceWatcherUpdatedRevoker = deviceWatcher.Updated(auto_revoke, &DeviceWatcher_Updated);
        deviceWatcherRemovedRevoker = deviceWatcher.Removed(auto_revoke, &DeviceWatcher_Removed);
        deviceWatcherCompletedRevoker = deviceWatcher.EnumerationCompleted(auto_revoke, &DeviceWatcher_EnumerationCompleted);
        deviceWatcher.Start();
    }
    catch (exception e) {
        LOG_ERROR(e.what())
    }
}

void TestBLE::StopDeviceScan() {
    scoped_lock lock(deviceListLock, deviceWatcherLock);
    if (deviceWatcher != nullptr) {
        deviceWatcherAddedRevoker.revoke();
        deviceWatcherUpdatedRevoker.revoke();
        deviceWatcherRemovedRevoker.revoke();
        deviceWatcherCompletedRevoker.revoke();
        deviceWatcher.Stop();
        deviceWatcher = nullptr;
    }
    deviceListSignal.notify_one();
}

#pragma endregion

#pragma region SUBSCRIBE/READ FUNCTIONS

//On this function you can read all data from the specified characteristic
void Characteristic_ValueChanged(GattCharacteristic const& characteristic, GattValueChangedEventArgs args)
{
    LOG_ERROR("Read data from device: " << to_string(characteristic.Service().Device().DeviceId()) << ", data size: " << args.CharacteristicValue().Length())
}

//Function used to subscribe async to the specific device
fire_and_forget SubscribeCharacteristicAsync(wstring deviceId, wstring serviceId, wstring characteristicId, bool* result) {
    try {
        auto characteristic = co_await getCharacteristic(&deviceId[0], &serviceId[0], &characteristicId[0]);
        if (characteristic != nullptr) {
            auto status = co_await characteristic.WriteClientCharacteristicConfigurationDescriptorAsync(GattClientCharacteristicConfigurationDescriptorValue::Notify);
            if (status != GattCommunicationStatus::Success) {
                LOG_ERROR("Error subscribing to characteristic. Status: " << (int)status)
            }
            else {
                for (int i = 0; i < deviceList.size(); i++) {
                    if (deviceList[i].id == deviceId) {
                        deviceList[i].subscription = new Subscription();
                        deviceList[i].subscription->revoker = characteristic.ValueChanged(auto_revoke, &Characteristic_ValueChanged);
                        break;
                    }
                }
                if (result != 0)
                    *result = true;
            }
        }
    }
    catch (hresult_error& ex)
    {
        LOG_ERROR("SubscribeCharacteristicAsync error: " << to_string(ex.message().c_str()))
        for (int i = 0; i < deviceList.size(); i++) {
            if (deviceList[i].id == deviceId && deviceList[i].subscription) {
                delete deviceList[i].subscription;
                deviceList[i].subscription = NULL;
                break;
            }
        }
    }
    subscribeSignal.notify_one();
}

//Call this function to subscribe to the specific device so you can read data from it
bool SubscribeCharacteristic(wstring deviceId, wstring serviceId, wstring characteristicId) {
    unique_lock<mutex> lock(subscribeLock);
    bool result = false;
    SubscribeCharacteristicAsync(deviceId, serviceId, characteristicId, &result);
    subscribeSignal.wait(lock);
    return result;
}

#pragma endregion

#pragma region WRITE FUNCTIONS

//Function used to send data async to the specific device
fire_and_forget SendDataAsync(wchar_t* deviceId, wchar_t* serviceId, wchar_t* characteristicId, uint8_t * data, uint16_t size, bool* result) {
    try {
        auto characteristic = co_await getCharacteristic(deviceId, serviceId, characteristicId);
        if (characteristic != nullptr) {
            DataWriter writer;
            writer.WriteBytes(array_view<uint8_t const>(data, data + size));
            IBuffer buffer = writer.DetachBuffer();
            auto status = co_await characteristic.WriteValueAsync(buffer, GattWriteOption::WriteWithoutResponse);
            if (status != GattCommunicationStatus::Success) {
                LOG_ERROR("Error writing value to characteristic. Status: " << (int)status)
            }
            else if (result != 0) {
                LOG_ERROR("Data written succesfully")
                *result = true;
            }
        }
    }
    catch (hresult_error& ex)
    {
        LOG_ERROR("SendDataAsync error: " << to_string(ex.message().c_str()))
        for (int i = 0; i < deviceList.size(); i++) {
            if (deviceList[i].id == deviceId && deviceList[i].subscription) {
                delete deviceList[i].subscription;
                deviceList[i].subscription = NULL;
                break;
            }
        }
    }
    signalWrite.notify_one();
}

//Call this function to write data on the device
bool SendData(wchar_t* deviceId, wchar_t* serviceId, wchar_t* characteristicId, uint8_t * data, uint16_t size) {
    bool result = false;
    unique_lock<mutex> lock(_mutexWrite);
    // copy data to stack so that caller can free its memory in non-blocking mode
    SendDataAsync(deviceId, serviceId, characteristicId, data, size, &result);

    signalWrite.wait(lock);

    return result;
}

#pragma endregion
Run Code Online (Sandbox Code Playgroud)

最后复制这个main函数(可以复制到同一个文件的末尾):

int main() {
    //The mac of the device that will be tested
    wstring deviceMac = L"00:11:22:33:44:55";
    //These are the serviceUUID, readCharacteristicUUID and writeCharacteristicUUID as I said previously
    wstring serviceUUID = L"{47918888-5555-2222-1111-000000000000}";
    wstring readUUID = L"{31a28888-5555-2222-1111-00000000cede}";
    wstring writeUUID = L"{f55a8888-5555-222-1111-00000000957a}";

    //I think it is the mac of the BLE USB Dongle because it is in all device id when they are enumerated
    wstring otherMac = L"24:4b:fe:3a:1a:ba";
    //The device Id that we are looking for
    wstring deviceId = L"BluetoothLE#BluetoothLE" + otherMac;
    deviceId += L"-";
    deviceId += deviceMac;

    //To start scanning just call this function
    TestBLE::ScanDevices();

    //Data to be written all the time
    const uint16_t dataSize = 3;
    uint8_t data [dataSize]= { 0x0, 0xff, 0xff };

    //Wait time in miliseconds between each write
    chrono::milliseconds waitTime = 100ms;

    //It will be executed always
    while (true) {
        //Then every device and their info updated would be in this vector
        for (int i = 0; i < deviceList.size(); i++) {
            //If the device is connectable we will try to connect if we aren't subscribed yet or send information
            if (deviceList[i].isConnectable) {
                //We can do here the following code to know the structure of the device id (if otherMac variable is the BLE USB dongle mac or not)
                //cout << to_string(deviceList[i].id) << endl;
                if (!deviceList[i].subscription && deviceList[i].id == deviceId) {
                    SubscribeCharacteristic(deviceList[i].id, serviceUUID, readUUID);
                }
                else if (deviceList[i].subscription) {
                    SendData(&deviceId[0], &serviceUUID[0], &writeUUID[0], data, dataSize);
                }
            }
        }
        this_thread::sleep_for(waitTime);
    }
}
Run Code Online (Sandbox Code Playgroud)

您将需要一个具有包含读取和写入特性的服务的 BLE 设备,在deviceMacserviceUUIDreadUUIDwriteUUID变量中设置相应的值,您还可以修改将要写入数据的字节和dataSize,以及waitTime 中写入之间的时间。该otherMac变量应该是BLE USB加密狗设备的MAC地址,但我建议你通过得到从设备的ID检查DEVICELIST里面的for循环。

当您偶尔运行此代码时,您将收到错误“获取服务失败。状态:”结果为 1(无法访问)或 3(访问被拒绝),在其余情况下,它将正确读取设备数据,并且一段时间后,它会给出错误“SendDataAsync 错误:对象已被处理”,然后它将继续给出“SubscribeCharacteristicAsync 错误:对象已被处理”,因此在某些时候它将停止能够读取设备的数据。可能是什么原因?

编辑 1: 这很奇怪,因为使用此代码,数据从未正确写入(未显示“数据写入成功”消息)但在我完成的代码中,我一直能够写入数据,也许问题仍然是相同的,并且它与存储在所述特征“地图<wstring的,DeviceCacheEntry>缓存”,因为也许它被存储为一个拷贝,并试图在某个时候被访问它时,设置由Windows(因为它是的一个拷贝存储在缓存中的原始文件),并在名为“UPDATE 2 - SOME WEIRDNESS”的点中给出了这篇文章的答案中描述的错误