How to handle call events using CTelephony 3rd Edition
Article Metadata
This article intends to explain how to handle most of the events on a call using CTelephony in 3rd Edition
Events handled here are:
- Call Ringing
- Call Answered
- Call connecting
- Call connected
- Call disconnected
- Dial call
The implementation is so simple. The idea is simply active object listening to change in voice line
Contents |
Headers Required
#include <Etel3rdParty.h>Libraries Required
LIBRARY etel3rdparty.libCapability Required
Capability NetworkServices
Code
The following code assumes that you have a class for log text (CMyLog).
Define observer interface to be implemented by class you want it to be notified with events (if you like)
MPhoneReceiverObserver
#ifndef MPHONERECEIVEROBSERVER_H
#define MPHONERECEIVEROBSERVER_H
class MPhoneReceiverObserver
{
public:
virtual void CallRinging() {}
virtual void CallAnswered() {}
virtual void DialingCall() {}
virtual void CallConnecting() {}
virtual void CallConnected() {}
virtual void CallDisConnected() {}
};
#endif // MPHONERECEIVEROBSERVER_H
Define the active object that will listen to any change in voice line status
CPhoneReceiver Declaration
#include <e32base.h> // For CActive, link against: euser.lib
#include "Etel3rdParty.h" //For CTelephony, link against etel3rdparty.lib
#include "MPhoneReceiverObserver.h"
#include "MyLog.h"
class CPhoneReceiver : public CActive
{
public:
// C++ constructor
CPhoneReceiver(MPhoneReceiverObserver& aPhoneReceiverObserver);
// Second-phase constructor
void ConstructL();
// Cancel and destroy
~CPhoneReceiver();
public: // New functions
// Function for making the initial request
void StartL();
private: // From CActive
// Handle completion
void RunL();
// How to cancel me
void DoCancel();
private:
//Define object to CTelephone to manage calls
CTelephony* iTelephony;
CTelephony::TCallInfoV1 iCurrentCallInfo;
CTelephony::TCallInfoV1Pckg iCurrentStatusPckg;
CTelephony::TCallId iCallID;
MPhoneReceiverObserver& iPhoneReceiverObserver;
};
#endif
CPhoneReceiver Implementation
#include "PhoneReceiver.h"
CPhoneReceiver::CPhoneReceiver(MPhoneReceiverObserver& aPhoneReceiverObserver)
: CActive(EPriorityStandard),
iCurrentStatusPckg(iCurrentCallInfo),
iPhoneReceiverObserver(aPhoneReceiverObserver)
{
}
void CPhoneReceiver::ConstructL()
{
//Create new object to telephony
iTelephony = CTelephony::NewL();
CActiveScheduler::Add(this); // Add to scheduler
}
CPhoneReceiver::~CPhoneReceiver()
{
Cancel(); // Cancel any request, if outstanding
delete iTelephony;
}
void CPhoneReceiver::DoCancel()
{
iTelephony->CancelAsync( CTelephony::EVoiceLineStatusChangeCancel );
}
void CPhoneReceiver::StartL()
{
Cancel(); // Cancel any request, just to be sure
//Notify of change in telephone line
iTelephony->NotifyChange( iStatus, CTelephony::EVoiceLineStatusChange,
iCurrentStatusPckg );
SetActive(); // Tell scheduler a request is active
}
void CPhoneReceiver::RunL()
{
if(iStatus.Int() == KErrNone)
{
//Get call status
CTelephony::TCallStatus callStatus = iCurrentStatusPckg().iStatus;
switch(callStatus)
{
case CTelephony::EStatusRinging:
iPhoneReceiverObserver.CallRinging();
break;
case CTelephony::EStatusAnswering:
iPhoneReceiverObserver.CallAnswered();
break;
case CTelephony::EStatusDialling:
iPhoneReceiverObserver.DialingCall();
break;
case CTelephony::EStatusConnecting:
iPhoneReceiverObserver.CallConnecting();
break;
case CTelephony::EStatusConnected:
iPhoneReceiverObserver.CallConnected();
break;
case CTelephony::EStatusDisconnecting:
iPhoneReceiverObserver.CallDisConnected();
break;
default:
#ifdef __USE_PHONE_RECEIVER_LOG__
TBuf<50> num;
num.Copy(_L("Default: "));
num.AppendNum(callStatus);
CMyLog::OpenAndWriteToFile(num);
#endif
break;
}
}
else
{
#ifdef __USE_PHONE_RECEIVER_LOG__
TBuf<50> num;
num.AppendNum(iStatus.Int());
num.Append(_L(" -- "));
CTelephony::TCallStatus callStatus = iCurrentStatusPckg().iStatus;
num.AppendNum(callStatus);
CMyLog::OpenAndWriteToFile(num);
#endif
}
//Notify every time before activate object
iTelephony->NotifyChange( iStatus, CTelephony::EVoiceLineStatusChange,
iCurrentStatusPckg );
if(!IsActive())
SetActive();
}
Output
The following is a sample for the log file written above
Log:25-4-2009-15-18-53
////////////////////
Call Ringing
////////////////////
Call Answered
////////////////////
Call connected
////////////////////
Call disconnected
////////////////////
Default: 1 //Line Idle
////////////////////


(no comments yet)