Symbian3/SDK/Source/GUID-BA0D9DA9-3B54-4964-A4D6-9ADA4DEB7231.dita
author Dominic Pinkman <dominic.pinkman@nokia.com>
Fri, 11 Jun 2010 12:39:03 +0100
changeset 8 ae94777fff8f
parent 7 51a74ef9ed63
child 13 48780e181b38
permissions -rw-r--r--
Week 23 contribution of SDK documentation content. See release notes for details. Fixes bugs Bug 2714, Bug 462.

<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies) All rights reserved. -->
<!-- This component and the accompanying materials are made available under the terms of the License 
"Eclipse Public License v1.0" which accompanies this distribution, 
and is available at the URL "http://www.eclipse.org/legal/epl-v10.html". -->
<!-- Initial Contributors:
    Nokia Corporation - initial contribution.
Contributors: 
-->
<!DOCTYPE task
  PUBLIC "-//OASIS//DTD DITA Task//EN" "task.dtd">
<task id="GUID-BA0D9DA9-3B54-4964-A4D6-9ADA4DEB7231" xml:lang="en"><title>Receiving
Data from Sensors</title><shortdesc>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.</shortdesc><prolog><metadata><keywords/></metadata></prolog><taskbody>
<prereq id="GUID-E09C6ABC-F717-4791-A2EE-491421D2BD30">       <p>Before listening
for sensor channel data, you must <xref href="GUID-4B709839-8EEF-4AD2-9868-9AF0176B1E5B.dita#GUID-4B709839-8EEF-4AD2-9868-9AF0176B1E5B/GUID-FBBD3A8B-F240-4DC0-B516-3A51FDD86A88">open
the sensor channel</xref>.</p>     </prereq>
<steps id="GUID-4DD07DEC-6017-4237-BE46-1D69E5FBD744-GENID-1-10-1-9-1-1-6-1-4-1-4-1-5-1-3-2">
<step id="GUID-CE0EE6EE-84A7-46F6-8590-C75FAA381843"><cmd>Implement the data
listener.</cmd>
<info><p>Inherit from the <parmname>MSensrvDataListener</parmname> interface
and implement the following pure virtual functions: <xref href="GUID-519E744E-246C-3F4A-A869-F25B03F1126F.dita#GUID-519E744E-246C-3F4A-A869-F25B03F1126F/GUID-92EDE76E-F5D8-3D45-939B-FD5C03347AE8"><apiname>MSensrvDataListener::DataReceived</apiname></xref>, <xref href="GUID-519E744E-246C-3F4A-A869-F25B03F1126F.dita#GUID-519E744E-246C-3F4A-A869-F25B03F1126F/GUID-F6C22940-A276-38C2-890F-CA45B75D613F"><apiname>MSensrvDataListener::DataError</apiname></xref> and <xref href="GUID-519E744E-246C-3F4A-A869-F25B03F1126F.dita#GUID-519E744E-246C-3F4A-A869-F25B03F1126F/GUID-4062844D-2F25-38FC-B91A-DD5B41E6FF3F"><apiname>MSensrvDataListener::GetDataListenerInterfaceL</apiname></xref></p><codeblock xml:space="preserve">class DataListener:public MSensrvDataListener
    {
    public:
        void DataReceived(CSensrvChannel&amp; aChannel, TInt aCount, TInt aDataLost )
        {
        ...
         //Implementation
        }
    void DataError( CSensrvChannel&amp;, TSensrvErrorSeverity)
        {
        ...
         //Implementation
        }
    void GetDataListenerInterfaceL( TUid, TAny*&amp;)
        {
        ...
         //Implementation
        }
    };</codeblock></info>
</step>
<step id="GUID-107B26D8-CD5A-4571-8441-F1A5A2C0EB2B"><cmd>Start data listening.</cmd>
<info><p>Pass an instance of data listener implementation using the <xref href="GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844.dita#GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844/GUID-F851A108-C72F-3132-BE15-DC41CACB64F9"><apiname>CSensrvChannel::StartDataListeningL()</apiname></xref> function.</p><codeblock xml:space="preserve">//Instance of the data listener implementation
DataListener dataListener; 
...
CSensrvChannel* channel;
...
channel-&gt;StartDataListeningL(&amp;dataListener, 1, 1, 0);
...</codeblock><p> When new data is available to be read in the sensor channel,
the client is notified by the <xref href="GUID-519E744E-246C-3F4A-A869-F25B03F1126F.dita#GUID-519E744E-246C-3F4A-A869-F25B03F1126F/GUID-7EE31046-31A6-356E-AFEF-2E8C003A7F15"><apiname>MSensrvDataListener::DataReceived(CSensrvChannel
&amp;,TInt,TInt)</apiname></xref> 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. </p></info>
</step>
<step id="GUID-40F9F566-475E-4ECD-B420-70EB1B77DEAA"><cmd>Read the channel
data from the receiving buffer using the <xref href="GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844.dita#GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844/GUID-EC1AF564-F05D-3729-B0C9-7573A1F79B86"><apiname>CSensrvChannel::GetData()</apiname></xref> function.
For more information about handling data notifications, see the following <xref href="GUID-BA0D9DA9-3B54-4964-A4D6-9ADA4DEB7231.dita#GUID-BA0D9DA9-3B54-4964-A4D6-9ADA4DEB7231/5876a529-0484-497d-8cf8-9ceb9c725388">Example</xref>.</cmd>
</step>
<step id="GUID-D3928C98-3D10-40BE-A63E-584969AA0416"><cmd>After you get the
required sensor data feeds from the sensor channel, you can stop data listening
using the <xref href="GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844.dita#GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844/GUID-0129F5FA-BD26-37F0-8FE4-19F393AA817C"><apiname>CSensrvChannel::StopDataListening()</apiname></xref> function.</cmd>
<stepxmp><codeblock xml:space="preserve">channel-&gt;StopDataListening();</codeblock></stepxmp>
</step>
</steps>
<example id="5876A529-0484-497D-8CF8-9CEB9C725388"><p>The following example
shows how to handle the double tapping data notification that is received. </p><p>Check
the channel type of the received data and then receive the data object using
the <xref href="GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844.dita#GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844/GUID-EC1AF564-F05D-3729-B0C9-7573A1F79B86"><apiname>CSensrvChannel::GetData()</apiname></xref> function. The <parmname>aCount</parmname> 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 <parmname>aDataLost</parmname> parameter provides
the details about the number of the lost data objects. For example, in heavy
load situations. </p><codeblock xml:space="preserve">void CTestClass::DataReceived( CSensrvChannel&amp; aChannel, 
                               TInt aCount, 
                               TInt aDataLost )
    {
    if ( aChannel.GetChannelInfo().iChannelType == KSensrvChannelTypeIdAccelerometerDoubleTappingData )
        {
        TSensrvTappingData tappingData;
        TPckg &lt;TSensrvTappingData&gt; tappingPackage( tappingData );
        aChannel.GetData( tappingPackage );
        }
    }</codeblock></example>
<postreq id="GUID-69071DC2-EB41-43BA-8DFC-CE2D4423AF8A"><p>End the session
with the sensor channel using the <xref href="GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844.dita#GUID-22DC917F-D833-3531-AB2D-A6E2D52EF844/GUID-0B8F50A6-2CA1-316A-82AB-AA026005E810"><apiname>CSensrvChannel::CloseChannel()</apiname></xref> function.</p></postreq>
</taskbody></task>