×
Namespaces

Variants
Actions

How to detect when a charger is connected

From Nokia Developer Wiki
Jump to: navigation, search

This article shows how to check whether a charger is connected using CTelephony::GetIndicator(). Note that this function is asynchronous, and can be cancelled using CTelephony::EGetIndicatorCancel.

Article Metadata
Article
Keywords: CTelephony::GetIndicator()
Created: polaka_sr (20 Dec 2007)
Updated: ircama (01 May 2012)
Last edited: hamishwillee (06 May 2013)

Source code

Libraries needed:

LIBRARY Etel3rdParty.lib and euser.lib


class CClientApp : public CActive
{
 
private:
CTelephony* iTelephony;
CTelephony::TIndicatorV1 iIndicatorV1;
CTelephony::TIndicatorV1Pckg iIndicatorV1Pckg;
public:
CClientApp(CTelephony* aTelephony);
void DetectCharger();
 
private:
void RunL();
void DoCancel();
};
 
CClientApp::CClientApp(CTelephony* aTelephony)
: CActive(EPriorityStandard),
iTelephony(aTelephony),
iIndicatorV1Pckg(iIndicatorV1)
{
//Constructor
CActiveScheduler::Add(this);
}
 
void CClientApp::DetectCharger()
{
iTelephony->GetIndicator(iStatus, iIndicatorV1Pckg);
SetActive();
}
 
void CClientApp::RunL()
{
if(iStatus==KErrNone)
{
if(iIndicatorV1.iCapabilities & CTelephony::KIndChargerConnected)
{
//We can detect when a charger is connected
if(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected)
{
//Now, charger is connected
}
else
{
//Here, charger is not connected
}
}
else
{
//Here, we do not know whether or not a charger is connected
}
}
}
 
//To cancel this asynchronous call
void CClientApp::DoCancel()
{
iTelephony->CancelAsync(CTelephony::EGetIndicatorCancel);
}



This is another example to show how to get status changes:



CGetIndicator.h

#ifndef __CGETINDICATOR_H__
#define __CGETINDICATOR_H__
 
#include <e32base.h>
#include <e32cons.h>
#include <e32def.h>
#include <etel3rdparty.h>
 
/**
Retrieves a bitmask pertaining to data on the current state of the network
connection, whether the battery charger is connected and whether there is an
active call or not. Can be notified on change.
*/

class CGetIndicator : public CActive
{
 
// Methods
public:
static CGetIndicator* NewL(CTelephony* aTelephony);
~CGetIndicator();
 
void DoStartRequestL();
void DoRequestNotificationL();
 
private:
CGetIndicator(CTelephony* aTelephony);
void ConstructL();
 
void RunL();
void DoCancel();
 
// Data
public:
/**
Stores retrieved indicator information.
*/

CTelephony::TIndicatorV1 iIndicatorV1;
 
private:
struct
{
TUint32 iIndicator;
TBool IsInitialized;
} iIndicatorV1Old;
/**
Package descriptor for iIndicatorV1.
*/

CTelephony::TIndicatorV1Pckg iIndicatorV1Pckg;
TBool iRequestNotify;
CTelephony* iTelephony;
};
 
#endif // __CGETINDICATOR_H__



CGetIndicator.cpp

#include <e32base.h>
#include <e32debug.h>
#include <e32std.h>
#include <Etel3rdParty.h>
 
CGetIndicator* CGetIndicator::NewL(CTelephony* aTelephony)
{
CGetIndicator* self = new(ELeave) CGetIndicator(aTelephony);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
 
/**
Destructor.
Cancels outstanding requests.
*/

CGetIndicator::~CGetIndicator()
{
Cancel();
}
 
void CGetIndicator::ConstructL()
{
iIndicatorV1Old.IsInitialized=EFalse;
}
 
CGetIndicator::CGetIndicator(CTelephony* aTelephony): CActive(EPriorityStandard), iTelephony(aTelephony), iIndicatorV1Pckg(iIndicatorV1)
{
CActiveScheduler::Add(this); // Add to scheduler
}
void CGetIndicator::DoStartRequestL()
{
_LIT(KDummyAnswerPanic, "CGetIndicator Get Method");
__ASSERT_ALWAYS(!IsActive(), User::Panic(KDummyAnswerPanic, 1));
iRequestNotify = ETrue;
 
// Retrieves the initial values for battery charging indicator, network availability indicator and call-in-progress indicator.
iTelephony->GetIndicator(iStatus, iIndicatorV1Pckg);
SetActive();
}
void CGetIndicator::RunL()
{
// Print Indicator Info
if(iStatus != KErrNone)
{
// Print the error status code
}
else
{
if (iIndicatorV1Old.IsInitialized)
{
if ( ( !(iIndicatorV1Old.iIndicator & CTelephony::KIndCallInProgress) ) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndCallInProgress) &&
(iIndicatorV1.iIndicator & CTelephony::KIndCallInProgress) )
{
_LIT(KCallConnected, "Call connected");
CEikonEnv::Static()->InfoMsg(KCallConnected);
}
if ( ( (iIndicatorV1Old.iIndicator & CTelephony::KIndCallInProgress)) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndCallInProgress) &&
!(iIndicatorV1.iIndicator & CTelephony::KIndCallInProgress) )
{
_LIT(KCallDisconnected, "Call terminated");
CEikonEnv::Static()->InfoMsg(KCallDisconnected);
}
if ( ( !(iIndicatorV1Old.iIndicator & CTelephony::KIndNetworkAvailable)) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndNetworkAvailable) &&
(iIndicatorV1.iIndicator & CTelephony::KIndNetworkAvailable) )
{
_LIT(KNetworkReady, "Network ready");
CEikonEnv::Static()->InfoMsg(KNetworkReady);
}
if ( ( (iIndicatorV1Old.iIndicator & CTelephony::KIndNetworkAvailable)) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndNetworkAvailable) &&
!(iIndicatorV1.iIndicator & CTelephony::KIndNetworkAvailable) )
{
_LIT(KNetworkLost, "Network lost");
CEikonEnv::Static()->InfoMsg(KNetworkLost);
}
if ( ( !(iIndicatorV1Old.iIndicator & CTelephony::KIndChargerConnected)) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndChargerConnected) &&
(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected) )
{ // We can detect when a charger is connected
// Charger is connected
_LIT(KChargerConnected, "Charger connected");
CEikonEnv::Static()->InfoMsg(KChargerConnected);
}
if ( ( (iIndicatorV1Old.iIndicator & CTelephony::KIndChargerConnected)) &&
(iIndicatorV1.iCapabilities & CTelephony::KIndChargerConnected) &&
!(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected) )
{
// Charger is not connected
_LIT(KChargerDisconnected, "Charger disconnected");
CEikonEnv::Static()->InfoMsg(KChargerDisconnected);
}
}
iIndicatorV1Old.iIndicator=iIndicatorV1.iIndicator;
iIndicatorV1Old.IsInitialized=ETrue;
if (iRequestNotify)
{
DoRequestNotificationL();
}
else
{
//Uncomment if there is a need to make DoStartRequestL iterative
//DoStartRequestL();
//_LIT(KElse, "Something else");
}
}
}
 
/**
Requests to receive notifications of change in the indicator information.
*/

void CGetIndicator::DoRequestNotificationL()
{
// Panic if this object is already performing an asynchronous operation.
// Application will panic if you call SetActive() on an already active object.
_LIT( KNotifyPanic, "CGetIndicator Notify Method" );
__ASSERT_ALWAYS( !IsActive(), User::Panic( KNotifyPanic, 1 ));
iRequestNotify = ETrue;
 
// Registers interest in receiving change notifications for events.
iTelephony->NotifyChange(iStatus,
CTelephony::EIndicatorChange,
iIndicatorV1Pckg );
SetActive();
}
 
/**
Cancels asynchronous request to CTelephony::GetIndicator().
*/

void CGetIndicator::DoCancel()
{
// Cancels an outstanding asynchronous request.
iTelephony->CancelAsync(CTelephony::EIndicatorChangeCancel);
}




Calling program

...
// within the include:
CTelephony* iTelephonyApi;
CGetIndicator *iGetIndicator;
...
// inside the program, to activate the notification:
iTelephonyApi = CTelephony::NewL(); // Create an instance of CTelephony
iGetIndicator = CGetIndicator::NewL(iTelephonyApi); // Set initial values and start event notification
iGetIndicator->DoStartRequestL();
...
// inside the program, within the destructor:
delete iGetIndicator;
delete iTelephonyApi; // this must follow iGetIndicator
...


Wiki Internal Links

This page was last modified on 6 May 2013, at 10:32.
52 page views in the last 30 days.

Was this page helpful?

Your feedback about this content is important. Let us know what you think.

 

Thank you!

We appreciate your feedback.

×