none
Do not receive events from sensor via ISensorEvents.

    Question

  • Hi,
    I am trying to learn using the Win7 Sensor API. I was able to write a working piece of code
    in C# fairly quickly, but when I tried to do the same using native C++, I don't seem to be receiving any events from the sensor. Below is my code:

    #include <iostream>
    #include <atlbase.h>
    #include <sensorsapi.h>
    #include <sensors.h>
    using namespace std;
    
    class SensorEvents : public ISensorEvents
    {
    public:
    	//ISensorEvents
    	HRESULT __stdcall OnEvent(ISensor *pSensor,REFGUID eventID,IPortableDeviceValues *pEventData)
    	{
    		return S_OK;
    	}
    	HRESULT __stdcall OnDataUpdated(ISensor *pSensor,ISensorDataReport *pNewData)
    	{
    		cout << "ondataupdated" << endl;	
    		return S_OK;
    	}
    	HRESULT __stdcall OnLeave(REFSENSOR_ID sensorID)
    	{
    		cout << "Onleave" << endl;
    		return S_OK;
    	}
    	HRESULT __stdcall OnStateChanged(ISensor *pSensor,SensorState state)
    	{
    		return S_OK;
    	}
    	// IUnknown
    	ULONG __stdcall AddRef()
    	{
    		return ++ref;
    	}
    	ULONG __stdcall Release()
    	{
    		return --ref;
    	}
    	HRESULT __stdcall QueryInterface(const IID& id,void**p)
    	{
    		if (id == IID_IUnknown || id == __uuidof(ISensorEvents))
    		{
    			*p = this;
    			AddRef();
    			return S_OK;
    		}
    		return E_NOINTERFACE;
    	}
    
    	SensorEvents() { ref = 0; }
    private:
    	unsigned long ref;
    };
    
    int main()
    {
    	CoInitializeEx( NULL, COINIT_APARTMENTTHREADED );
    
    	HRESULT hr = S_OK;
    
    	CComPtr<ISensorCollection> Sensors;
    	CComPtr<ISensor> LightSensor;
    	CComPtr<ISensorManager> m_ISensorManager;
    	SensorEvents EventClass;
    
    	hr = CoCreateInstance(CLSID_SensorManager, NULL, CLSCTX_INPROC_SERVER,IID_PPV_ARGS(&m_ISensorManager));
    
    	if(SUCCEEDED(hr))
    	{
    		cout << "Created SensorManager object." << endl;
    	}
    	else
    		cout << "Could not create SensorManager object." << endl;
    
    	hr = m_ISensorManager->GetSensorsByCategory(SENSOR_CATEGORY_LIGHT, &Sensors);
    	
    	if(SUCCEEDED(hr))
    		cout << "Created SensorCollection object." << endl;
    	else
    		cout << "Could not create SensorCollection object." << endl;
    
    	hr = m_ISensorManager->RequestPermissions(0, Sensors, TRUE);
    	if(SUCCEEDED(hr))
    		cout << "Successfully got permission to talk to the sensors." << endl;
    	else
    		cout << "Could not get permission to talk to the sensors." << endl;
    
    	hr = Sensors->GetAt(0,&LightSensor); 
    
    	if(SUCCEEDED(hr))
    		cout << "Got handle on the ambient light sensor." << endl;
    	else
    		cout << "Could not acquire a handle on the sensor." << endl;
    
    	hr = LightSensor->SetEventSink(&EventClass);
    	if(SUCCEEDED(hr))
    		cout << "Set Event Sink." << endl;
    	else
    		cout << "Could not set Event Sink." << endl;
    
    	GUID pguid[2];
    	pguid[1] = SENSOR_EVENT_DATA_UPDATED;
    	pguid[0] = SENSOR_EVENT_STATE_CHANGED;
    
    	hr = LightSensor->SetEventInterest(pguid, 2);
    
    	Sleep(10000);
    
    	return 0;
    }

    I hoped to see the "ondataupdated" message output in my console. I inserted a 10s sleep at the end so that I could see incoming events during that period.

    However, I find that the OnDataUpdated callback is made at most once/twice only in a few cases, and in all those cases, it seems to happen only while the Sensor object is being freed, just before the "main" function terminates.

    What am I doing wrong?

     

    Wednesday, June 16, 2010 7:51 PM

All replies

  • Did you test with a reliable sensor driver ?

     


    Michael
    Thursday, July 01, 2010 9:13 PM
  • What sensor are you using as your data source ?
    Friday, July 09, 2010 4:27 PM
    Moderator
  • I am here searching for an answer too. The same data source works well in an MFC application. It does not in a console application. Is the data is reported using the Windows message loop or something similar?
    Tuesday, July 15, 2014 1:54 AM
  • please try to  add the codes to you end of the  main loop like this:

    while(GetMessage(&msg,NULL,0,0)){

    DispatchMessage(&msg);

     }
    Wednesday, August 05, 2015 4:52 AM