Skip to content

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 example )

Initializing the proximity sensor can be done with following snippet

    CSensrvChannelFinder* SensrvChannelFinder = CSensrvChannelFinder::NewLC();
    RSensrvChannelInfoList ChannelInfoList;
    CleanupClosePushL( ChannelInfoList );
    TSensrvChannelInfo mySearchConditions; // none, so matches all.
    mySearchConditions.iChannelDataTypeId = KSensrvChannelTypeIdProximityMonitor;
     // do something with the ChannelInfoList
    iSensrvChannel = CSensrvChannel::NewL( ChannelInfoList[0] );
    CleanupStack::Pop( &ChannelInfoList );
    CleanupStack::PopAndDestroy( SensrvChannelFinder );

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

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

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

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.

One Comment

  1. […] This post was mentioned on Twitter by Lucian TomuČ›a, Summeli. Summeli said: Blog: Using the proximity sensor as a button […]

Leave a Reply