2017-02-13 1 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; 
} 

Je suis en train de gérer l'événement généré par bleWatcher lorsqu'une connexion est reçue, lorsque je tente de créer mon rappel, je reçois l'erreur, Erreur C2664 « HRESULT Microsoft :: WRL :: DelegateTraits :: CheckReturn (HRESULT) ': ne peut pas convertir l' argument 1 de 'std :: _ non forcé' à 'HRESULT'erreur en essayant de créer le rappel

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

utilisateur steno916 semble avoir compris comment gérer cela, mais je ne peux pas comprendre ce que il a fait de son code fourni.

+2

Vous avez juste besoin de définir l'argument de OnConnectionReceived être des pointeurs vers des interfaces (IBluetoothLEAdvertisementWatcher * et * IBluetoothLEAdvertisementReceivedEventArgs), et non des classes. –

+0

Vous êtes mon héros monsieur, je n'ai aucune idée de comment je ne pouvais pas comprendre cela, mais je vous remercie beaucoup de changer cela de commentaire à répondre et votre prime sera accordée. –

+0

Eh bien, trop tard! Hans a en quelque sorte détruit la générosité avec la réponse qu'il a faite sous wiki communautaire ... –

Répondre

2

Note: lors de la compilation fonction membre modèle de classe « HRESULT Microsoft :: WRL :: Détails :: InvokeHelper :: Invoke (ABI :: :: Devices de Windows :: Bluetooth :: Publicité :: IBluetoothLEAdvertisementWatcher *, ABI: : Windows :: :: Bluetooth Devices :: Publicité :: IBluetoothLEAdvertisementReceivedEventArgs *) »

modèle compiler les messages d'erreur peuvent être, erm, douloureux. Celui-ci est certainement une mère, le problème n'a rien à voir avec HRESULT. Très important de regarder dans la fenêtre de sortie pour lire le message d'erreur complet. C'est la partie du message qui fournit l'indice essentiel pour vous aider à diagnostiquer ce qui s'est mal passé. Pourtant, même alors, vous pouvez le regarder pendant une heure ou un jour et ne pas encore le voir. Parfois, il est utile de l'éditer, en remplaçant les noms très longs par des noms courts pour réduire le niveau de bruit. Pourrait révéler les détails essentiels, notez comment il passe pointeurs d'interface à Invoke(). Le callback doit donc également utiliser des pointeurs d'interface dans sa signature. Ajouter deux Moi:

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