0

I am trying to analyze the data contained in a bluetooth low energy advertisement, this is my code so far.

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;
}

I cannot find how to extract the data from the IBuffer, preferably as byte array. If there is a better way to extract the data, or anyone knows how to convert the IBuffer to byte array your help would be greatly appreciated.

Also, I have no way of checking that my code thus far is correct, so if any mistakes were made please feel free to let me know.

Alden Be
  • 508
  • 3
  • 15
  • This helps: https://stackoverflow.com/questions/11853838/getting-an-array-of-bytes-out-of-windowsstoragestreamsibuffer – Samer Tufail Jun 09 '17 at 00:51

1 Answers1

1

I have found the answer. Mostly it's just a mess of comptrs the extra casting used by others to convert the buffer is unnecessary here because the buffer is already a comptr object.

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;
}
Alden Be
  • 508
  • 3
  • 15