2017-02-13 52 views
1
#include "stdafx.h" 

#include <iostream> 
#include <Windows.Foundation.h> 
#include <wrl\wrappers\corewrappers.h> 
#include <wrl\client.h> 
#include <wrl\event.h> 
#include <stdio.h> 
#include <../winrt/windows.devices.bluetooth.h> 
#include <../winrt/windows.devices.bluetooth.advertisement.h> 
#include <memory> 
#include <functional> 

using namespace std; 
using namespace Microsoft::WRL; 
using namespace Microsoft::WRL::Wrappers; 
using namespace ABI::Windows::Foundation; 
using namespace ABI::Windows::Devices::Bluetooth::Advertisement; 
using namespace ABI::Windows::UI::Input; 


// Prints an error string for the provided source code line and HRESULT 
// value and returns the HRESULT value as an int. 
int PrintError(unsigned int line, HRESULT hr) 
{ 
    wprintf_s(L"ERROR: Line:%d HRESULT: 0x%X\n", line, hr); 
    return hr; 
} 
struct Test { 
    Test() {} 
    Test(int i) {} 

    HRESULT OnConnectionReceived(BluetoothLEAdvertisementWatcher* watcher, BluetoothLEAdvertisementReceivedEventArgs* args) { 
     MessageBox(0, L"connected", L"MessageBox caption", MB_OK); 
     return S_OK; 
    } 
}; 

EventRegistrationToken *watcherToken; 


int main() 
{ 
    watcherToken = new EventRegistrationToken(); 

    // Initialize the Windows Runtime. 
    RoInitializeWrapper initialize(RO_INIT_MULTITHREADED); 
    if (FAILED(initialize)) 
    { 
     return PrintError(__LINE__, initialize); 
    } 

    // Get the activation factory for the IBluetoothLEAdvertisementWatcherFactory interface. 
    ComPtr<IBluetoothLEAdvertisementWatcherFactory> bleAdvWatcherFactory; 
    HRESULT hr = GetActivationFactory(HStringReference(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementWatcher).Get(), &bleAdvWatcherFactory); 
    if (FAILED(hr)) 
    { 
     return PrintError(__LINE__, hr); 
    } 


    ComPtr<IBluetoothLEAdvertisementWatcher> bleWatcher; 
    ComPtr<IBluetoothLEAdvertisementFilter> bleFilter; 


    Wrappers::HStringReference class_id_filter2(RuntimeClass_Windows_Devices_Bluetooth_Advertisement_BluetoothLEAdvertisementFilter); 
    hr = RoActivateInstance(class_id_filter2.Get(), reinterpret_cast<IInspectable**>(bleFilter.GetAddressOf())); 
    hr = bleAdvWatcherFactory->Create(bleFilter.Get(), &bleWatcher); 



    if (bleWatcher == NULL) 
    { 
     cout << "bleWatcher is NULL, err is " << hex << hr; 
    } 
    else 
    { 
     bleWatcher->Start(); 
     Test test; 
     //Problem is here 
     ComPtr<ITypedEventHandler<BluetoothLEAdvertisementWatcher*, BluetoothLEAdvertisementReceivedEventArgs*>> handler; 
     handler = Callback<ITypedEventHandler<BluetoothLEAdvertisementWatcher*, BluetoothLEAdvertisementReceivedEventArgs*> > 
      (std::bind(
       &Test::OnConnectionReceived, 
       &test, 
       placeholders::_1, 
       placeholders::_2 
      )); 

    hr = bleWatcher->add_Received(handler.Get(), watcherToken); 
     while (1) { 

      Sleep(1000); 
     } 
    } 


    return 0; 
} 

我試圖處理由bleWatcher生成的事件接收到連接時,當我嘗試創建我的回調我收到錯誤, 錯誤C2664「HRESULT微軟:: WRL :: DelegateTraits :: CheckReturn(HRESULT):性病:: _非受迫性「不能轉換參數1‘’到‘HRESULT’錯誤試圖創建回調

https://social.msdn.microsoft.com/Forums/Lync/en-US/e321cb3c-462a-4b16-b7e4-febdb3d0c7d6/windows-10-pairing-a-ble-device-from-code?forum=wdk&prof=required

用戶steno916似乎已經找到了如何處理這個問題,但我不明白是什麼他從他提供的代碼中完成了。

+2

你只需要定義OnConnectionReceived的說法是指向接口(IBluetoothLEAdvertisementWatcher *和* IBluetoothLEAdvertisementReceivedEventArgs),而不是類。 –

+0

你是我的英雄先生,我不知道我怎麼弄不清楚這一點,但我非常感謝你從評論到回答,並且獎勵你的獎勵。 –

+0

呃,太晚了!漢斯用他在社區維基下製作的答案摧毀了這個賞金...... –

回答

2

注:在編譯類模板成員函數「HRESULT微軟:: WRL ::詳細:: InvokeHelper ::調用(ABI:視窗:設備::藍牙::廣告:: IBluetoothLEAdvertisementWatcher *,ABI: :Windows ::設備::藍牙::廣告:: IBluetoothLEAdvertisementReceivedEventArgs *)'

模板編譯錯誤信息可以,呃,痛苦。這個肯定是一個母親,這個問題與HRESULT無關。在「輸出」窗口中查看完整的錯誤消息非常重要。這是該消息的一部分,它提供了重要提示,可幫助您診斷出現問題的地方。

儘管如此,你可以看看它一個小時或一天,仍然沒有看到它。有時編輯它很有用,用很短的名稱替換很長的名字以減少噪音水平。可能會揭示基本的細節,注意它如何將接口指針傳遞給Invoke()。所以回調也必須在其簽名中使用接口指針。添加兩個我的:

HRESULT OnConnectionReceived(IBluetoothLEAdvertisementWatcher* watcher, 
          IBluetoothLEAdvertisementReceivedEventArgs* args) { 
    // etc... 
}