簡體   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