Thursday, March 6, 2008

Charger connected state

How to find whether charger connected or not?

Here is the sample code:

//System include
#include < etelthirdparty.h >

//*** CBatteryInfo *************/

class CBatteryInfo : public CActive
{
private:
CTelephony* iTelephony;
CTelephony::TIndicatorV1 iIndicatorV1;
CTelephony::TIndicatorV1Pckg iIndicatorV1Pckg;
public:
CBatteryInfo();
void GetIndicator();

private:
/*
These are the pure virtual methods from CActive that
MUST be implemented by all active objects
*/
void RunL();
void DoCancel();
};

//************************************************
CBatteryInfo* CBatteryInfo::NewL()
{
CBatteryInfo* self = new (ELeave) CBatteryInfo;
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop();
return self;
}

void CBatteryInfo::ConstructL()
{
iTelephony = CTelephony::NewL();
}

CBatteryInfo::CBatteryInfo()
: CActive(EPriorityStandard),
iIndicatorV1Pckg(iIndicatorV1)
{
//default constructor
CActiveScheduler::Add(this);
}

void CBatteryInfo::GetIndicator()
{
iTelephony->GetIndicator(iStatus, iIndicatorV1Pckg);
SetActive();
}

void CBatteryInfo::RunL()
{
if(iStatus==KErrNone)
{
if(iIndicatorV1.iCapabilities & CTelephony::KIndChargerConnected)
{
// We can detect when a charger is connected
if(iIndicatorV1.iIndicator & CTelephony::KIndChargerConnected)
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
// Show the information Note
note->ExecuteLD(_L("Charger Connected...!"));
} // Charger is connected
else
{
CAknInformationNote* note = new ( ELeave ) CAknInformationNote;
// Show the information Note
note->ExecuteLD(_L("XXX: Charger Not Connected...!"));
} // Charger is not connected
}
else
{} // We do not know whether a charger is connected
}
TRequestStatus* status = &iStatus;
User::RequestComplete(status, KErrNone);
}

void CBatteryInfo::DoCancel()
{
iTelephony->CancelAsync(CTelephony::EGetIndicatorCancel);
}

//*************caller code

iBatteryInfo = CBatteryInfo::NewL();
iBatteryInfo->GetIndicator();
CActiveScheduler::Start();
delete iBatteryInfo;
iBatteryInfo = NULL;

No comments: