Receiving Data from Sensors

You can implement a data listener to receive data from the sensors. Based on the data received, you can perform specific actions on the Symbian device. For example, if you receive information from the sensor about the distance between the device and the user then based on this information, you can disable or enable the loudspeaker mode on the Symbian device.

Before listening for sensor channel data, you must open the sensor channel .

  1. Implement the data listener.

    Inherit from the MSensrvDataListener interface and implement the following pure virtual functions: MSensrvDataListener::DataReceived , MSensrvDataListener::DataError and MSensrvDataListener::GetDataListenerInterfaceL

            class DataListener:public MSensrvDataListener
        {
        public:
            void DataReceived(CSensrvChannel& aChannel, TInt aCount, TInt aDataLost )
            {
            ...
             //Implementation
            }
        void DataError( CSensrvChannel&, TSensrvErrorSeverity)
            {
            ...
             //Implementation
            }
        void GetDataListenerInterfaceL( TUid, TAny*&)
            {
            ...
             //Implementation
            }
        };
           
  2. Start data listening.

    Pass an instance of data listener implementation using the CSensrvChannel::StartDataListeningL() function.

            //Instance of the data listener implementation
    DataListener dataListener; 
    ...
    CSensrvChannel* channel;
    ...
    channel->StartDataListeningL(&dataListener, 1, 1, 0);
    ...
           

    When new data is available to be read in the sensor channel, the client is notified by the MSensrvDataListener::DataReceived(CSensrvChannel &,TInt,TInt) callback function. The channel data is received into the receiving buffer. The receiving buffer is allocated from the heap in the client's thread and its size is the channel data item size multiplied by the maximum number of data items.

  3. Read the channel data from the receiving buffer using the CSensrvChannel::GetData() function. For more information about handling data notifications, see the following Example .
  4. After you get the required sensor data feeds from the sensor channel, you can stop data listening using the CSensrvChannel::StopDataListening() function.
            channel->StopDataListening();
           

The following example shows how to handle the double tapping data notification that is received.

Check the channel type of the received data and then receive the data object using the CSensrvChannel::GetData() function. The aCount parameter provides the details about the number of data objects in the channels receiving buffer. This number can be zero if the buffering period is used when data listening is started. The aDataLost parameter provides the details about the number of the lost data objects. For example, in heavy load situations.

       void CTestClass::DataReceived( CSensrvChannel& aChannel, 
                               TInt aCount, 
                               TInt aDataLost )
    {
    if ( aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdAccelerometerDoubleTappingData )
        {
        TSensrvTappingData tappingData;
        TPckg <TSensrvTappingData> tappingPackage( tappingData );
        aChannel.GetData( tappingPackage );
        }
    }
      

End the session with the sensor channel using the CSensrvChannel::CloseChannel() function.