我正在尝试分析蓝牙低能耗广告中包含的数据,这是我目前的代码。
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转换为字节数组,那么我们将非常感谢您的帮助。
另外,我无法检查到目前为止我的代码是否正确,所以如果有任何错误,请随时告诉我。
答案 0 :(得分:0)
我找到了答案。大多数情况下,这只是一堆混乱,因为缓冲区已经是一个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;
}