- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
Reading Sensor Data Asynchronously and Handling Sensor Events in Visual Basic .NET
Reading Sensor Data Asynchronously and Handling Sensor Events Print Denso QR Bar Code In Visual Basic .NET Using Barcode creation for .NET Control to generate, create QR Code ISO/IEC18004 image in .NET framework applications. www.OnBarcode.comQR Code ISO/IEC18004 Scanner In Visual Basic .NET Using Barcode decoder for .NET framework Control to read, scan read, scan image in .NET applications. www.OnBarcode.comAs we mentioned in the previous section, you can also read sensor data asynchronously. To do so, you need to use the Sensor API events, which provide event notifications for sensor data updates and state changes. To receive sensor event notifications, you must implement a set of callback methods defined by the ISensorEvents COM Interface: OnEvent Provides a generic event notification that includes an ID indicating which event was received: data update, sensors leave, or sensor state change Barcode Printer In Visual Basic .NET Using Barcode generator for .NET framework Control to generate, create bar code image in Visual Studio .NET applications. www.OnBarcode.comDecoding Bar Code In VB.NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.com 9
Painting QR Code ISO/IEC18004 In C#.NET Using Barcode creation for .NET Control to generate, create QR Code image in Visual Studio .NET applications. www.OnBarcode.comQuick Response Code Drawer In .NET Framework Using Barcode generation for ASP.NET Control to generate, create QR Code 2d barcode image in ASP.NET applications. www.OnBarcode.comIntroduction to the Sensor and Location Platform
Encode Quick Response Code In Visual Studio .NET Using Barcode printer for Visual Studio .NET Control to generate, create QR Code 2d barcode image in .NET framework applications. www.OnBarcode.comEAN13 Drawer In Visual Basic .NET Using Barcode creator for .NET Control to generate, create EAN / UCC - 13 image in .NET applications. www.OnBarcode.comOnDataUpdated The event you want to use to receive sensor data updates and read new sensor data values OnLeave The event you ll need to handle when a sensor is no longer available OnStateChanged Provides a notification that a sensor state has changed Print Barcode In VB.NET Using Barcode drawer for Visual Studio .NET Control to generate, create bar code image in .NET applications. www.OnBarcode.comANSI/AIM Code 39 Creation In VB.NET Using Barcode drawer for .NET framework Control to generate, create ANSI/AIM Code 39 image in .NET framework applications. www.OnBarcode.comISensorEvents is a COM interface that inherits from IUnknown. If you want to read sensor data asynchronously, you need to implement the ISensorEvents.OnDataUpdate callback method. This method receives pointers to a ISensor object and to a ISensorDataReport as shown in the following code snippet: 2D Barcode Printer In VB.NET Using Barcode generation for .NET Control to generate, create Matrix Barcode image in .NET framework applications. www.OnBarcode.comANSI/AIM Code 93 Creator In Visual Basic .NET Using Barcode creator for .NET Control to generate, create USS-93 image in .NET applications. www.OnBarcode.comHRESULT CAmbientLightAwareSensorEvents::OnDataUpdated(ISensor *pSensor, ISensorDataReport *pNewData) { HRESULT hr = S_OK; if ((NULL != pSensor) && (NULL != pNewData)) { // Helper function, gets LUX data from a sensor and updates the application's UI hr = GetSensorData(pSensor, pNewData); } else { hr = E_UNEXPECTED; } return hr; } Painting Code 128 Code Set B In Visual Studio .NET Using Barcode creation for .NET framework Control to generate, create Code 128B image in .NET applications. www.OnBarcode.comData Matrix ECC200 Reader In C#.NET Using Barcode scanner for VS .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comHere you can see that after verifying that the sensor and its data report are valid (not null pointers), you call a helper function, GetSensorData, that reads the LUX value from the sensor report and updates the application s UI. This is the same helper function we described in the Reading Sensor Data Synchronously section. The OnStateChange event is also an important event to handle. This is mainly because when you ask a user to enable a sensor, if the user does enable the sensor, you can expect the sensor to generate a state change event. In this case, the state change event indicates that the sensor state has changed from SENSOR_STATE_ACCESS_DENIED to SENSOR_STATE_READY. When implementing the OnStateChanged event handler, you should handle all the possible states a sensor can have, which were described in the What Is a Sensor section earlier in the chapter. The following code snippet is the simple (and not complete) implementation of the OnStateChanged method in our Ambient Light application. 2D Barcode Maker In .NET Framework Using Barcode generator for .NET Control to generate, create Matrix 2D Barcode image in Visual Studio .NET applications. www.OnBarcode.comCreating Barcode In Java Using Barcode creation for Java Control to generate, create bar code image in Java applications. www.OnBarcode.comHRESULT CAmbientLightAwareSensorEvents::OnStateChanged(ISensor *pSensor, SensorState state) { HRESULT hr = S_OK; if (NULL != pSensor) { Code 3 Of 9 Printer In Java Using Barcode generator for BIRT reports Control to generate, create Code 3 of 9 image in BIRT reports applications. www.OnBarcode.comECC200 Creation In Visual C#.NET Using Barcode encoder for .NET Control to generate, create Data Matrix ECC200 image in Visual Studio .NET applications. www.OnBarcode.comIntroducing Windows 7 for Developers
Data Matrix 2d Barcode Reader In Visual Basic .NET Using Barcode scanner for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comPainting GTIN - 128 In Visual Studio .NET Using Barcode printer for Reporting Service Control to generate, create UCC.EAN - 128 image in Reporting Service applications. www.OnBarcode.comSENSOR_ID idSensor = GUID_NULL; hr = pSensor->GetID(&idSensor); if (SUCCEEDED(hr)) { if (SENSOR_STATE_READY == state) { hr = GetSensorData(pSensor); } else { // If the sensor is not ready, its lux value is ignored m_mapLux[idSensor] = -1.0; hr = UpdateLux(); } } } else { hr = E_POINTER; } return hr; } Here you can see that first you verify the sensor state, and if it s ready, you synchronously read its data using the GetSensorData method that we described in the Reading Sensor Data Synchronously section. Setting Sensor Event Sinking
There is just one more thing you need to do to receive sensor events. You need to register your ISensorEvents implementation to specify the interface that receives sensor event notifications. We touched upon this topic in the Discovering Sensors section earlier in the chapter. During the Ambient Light application initialization phase, for each sensor you discover, you call the AddSensor function. This is a helper function that sets up event sinking for a specific sensor, and its code looks like this: HRESULT CAmbientLightAwareSensorManagerEvents::AddSensor(ISensor *pSensor) { HRESULT hr = S_OK; if (NULL != pSensor) { hr = pSensor->SetEventSink(m_pSensorEvents); if (SUCCEEDED(hr)) { // Get the sensor's ID to be used as a key to store the sensor SENSOR_ID idSensor = GUID_NULL; hr = pSensor->GetID(&idSensor); if (SUCCEEDED(hr)) { // Enter the sensor into the map and take the ownership of its lifetime pSensor->AddRef(); // the sensor is released in the destructor m_Sensors[idSensor] = pSensor;
|
|