Home Using the proximity sensor as a button
Post
Cancel

Using the proximity sensor as a button

The Proximity sensor is usually used to detect when the phone is near of the user’s ear, so the display can be turned off when speaking into the phone. The Symbian OS has an API for the proximity sensor, so it can also be used as a button.
My experience from the proximity sensor suggest that you shouldn’t use it when you are needing a high resolution refresh rates for the button( action games), since the API gives updates only few times in a second.

The Proximity sensor is located near the ear piece on the phone

Using the API

The Proximity sensor can be used via sensor framework. See this forum.nokia example http://wiki.forum.nokia.com/index.php/S60_Sensor_Framework )

Initializing the proximity sensor can be done with following snippet

1
2
3
4
5
6
7
8
9
10
11
12
    CSensrvChannelFinder* SensrvChannelFinder = CSensrvChannelFinder::NewLC();
    RSensrvChannelInfoList ChannelInfoList;
    CleanupClosePushL( ChannelInfoList );
    TSensrvChannelInfo mySearchConditions; // none, so matches all.
    mySearchConditions.iChannelDataTypeId = KSensrvChannelTypeIdProximityMonitor;
    SensrvChannelFinder->FindChannelsL(ChannelInfoList,mySearchConditions);
     // do something with the ChannelInfoList
    iSensrvChannel = CSensrvChannel::NewL( ChannelInfoList[0] );
    iSensrvChannel->OpenChannelL();
    ChannelInfoList.Close();
    CleanupStack::Pop( &ChannelInfoList );
    CleanupStack::PopAndDestroy( SensrvChannelFinder );

After the proximity sensor channel has been opened into iSensrvChannel it can be read with timer and calling

1
iSensrvChannel->StartDataListeningL( this, 1,1,0);

After this the data is being received trough MSensrvDataListener interface class via DataReceived function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void ProximityButton::DataReceived( CSensrvChannel& aChannel, TInt aCount, TInt aDataLost )
	{
    if ( aChannel.GetChannelInfo().iChannelType ==  KSensrvChannelTypeIdProximityMonitor )
    	{
    	TSensrvProximityData data;
    	for( TInt i = 0; i < aCount; i++ )
    		{
    		TPckgBuf<TSensrvProximityData> dataBuf;
    		aChannel.GetData( dataBuf );
    		data = dataBuf();
    		TSensrvProximityData::TProximityState state = data.iProximityState;
    		if ( iLastState != state )
    			{
    			if( state == TSensrvProximityData::EProximityIndiscernible )
    				{
    				//button up
    				PostKeyEvent( EFalse );
				}
    			if( state == TSensrvProximityData::EProximityDiscernible )
    				{
    				//button down
    				PostKeyEvent( ETrue );
    				}
    			}
    		iLastState = state;
    		}
    	}
    dataRequested = EFalse;
	}

After this you just have to implement the PostKeyEvent function where you can emit a key event.
The proximity sensor can also be used through Qt Mobility API’s sensor API but this time I wasn’t using it.

This post is licensed under CC BY 4.0 by the author.

EQ is making progress with PSX4ALL

Numpty Physics for Symbian

Comments powered by Disqus.