繁体   English   中英

在C ++中提取蓝牙低能耗广告数据

[英]Extract bluetooth low energy advertisement data in c++

我正在尝试分析蓝牙低能耗广告中包含的数据,到目前为止,这是我的代码。

HRESULT BLEWatcher::OnAdvertisementReceived(IBluetoothLEAdvertisementWatcher* watcher, IBluetoothLEAdvertisementReceivedEventArgs* args)
{

    IBluetoothLEAdvertisement** advertisement;
    args->get_Advertisement(advertisement);

    __FIVector_1_Windows__CDevices__CBluetooth__CAdvertisement__CBluetoothLEAdvertisementDataSection** dataSections;
    (*advertisement)->get_DataSections(dataSections);
    IBluetoothLEAdvertisementDataSection** dataSection;
    (*dataSections)->GetAt(2, dataSection);
    ABI::Windows::Storage::Streams::IBuffer** buffer;
    (*dataSection)->get_Data(buffer);
    ComPtr<ABI::Windows::Storage::Streams::IDataReader> reader;
    UINT32 *length;
    (*buffer)->get_Length(length);

    return S_OK;
}

我找不到如何从IBuffer中提取数据的方法,最好是字节数组。 如果有更好的方法来提取数据,或者有人知道如何将IBuffer转换为字节数组,将非常感谢您的帮助。

此外,我无法检查到目前为止我的代码是否正确,因此,如果发生任何错误,请随时告诉我。

我找到了答案。 通常,这只是一团糟,其他人没有必要使用额外的转换来转换缓冲区,因为缓冲区已经是一个comptr对象。

HRESULT BLEWatcher::OnAdvertisementReceived(IBluetoothLEAdvertisementWatcher* watcher, IBluetoothLEAdvertisementReceivedEventArgs* args)
{


    ComPtr<IBluetoothLEAdvertisement> advertisement;
    args->get_Advertisement(&advertisement);
    ComPtr<__FIVector_1_Windows__CDevices__CBluetooth__CAdvertisement__CBluetoothLEAdvertisementDataSection> dataSections;
    advertisement->get_DataSections(&dataSections);
    ComPtr<IBluetoothLEAdvertisementDataSection> dataSection;
    //Get appropriate data section
    dataSections->GetAt(2, &dataSection);
    ComPtr<IBuffer> buffer;
    dataSection->get_Data(&buffer);
    ComPtr<IBufferByteAccess> bufferByteAccess;
    buffer.As(&bufferByteAccess);
    byte* bytes = nullptr;
    bufferByteAccess->Buffer(&bytes);

    //Handle bytes here

    return S_OK;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM