You Are Here:

Community: Wiki

This page was last modified on 10 September 2009, at 05:34.

Monitoring battery status with CTelephony

From Forum Nokia Wiki

Reviewer Approved   

CBatteryCheck implementation illustrates how to check and monitor the battery status of S60 3rd Edition devices with CTelephony API.

The implementation is pretty simple, and when using this one only thing to do in calling class is to implement the callback interface and then to construct an instance of the CBatteryCheck.

When constructing CBatteryCheck, the initial battery level is checked with GetBatteryInfo()-function and after this function returns and calls back the RunL, all changes in battery status are monitored by calling NotifyChange for the battery status.

In the callback the aChangeStatus variable has the value of the current percentage of the battery power left, and the aStatus indicates the battery status with the help of enum from CTelephony:

enum TBatteryStatus
{
/**
The phone software can not determine the phone's current power status.
*/

EPowerStatusUnknown,
 
/**
The phone is powered by the battery.
*/

EPoweredByBattery,
 
/**
A battery is connected, but the phone is externally powered.
*/

EBatteryConnectedButExternallyPowered,
 
/**
No battery is connected.
*/

ENoBatteryConnected,
 
/**
Power fault.
*/

EPowerFault
};

Link against:

LIBRARY etel3rdparty.lib

BatteryLevel.cpp

#include "BatteryLevel.h"
 
CBatteryCheck* CBatteryCheck::NewLC(MBatteryObserver& aObserver)
{
CBatteryCheck* self = new (ELeave) CBatteryCheck(aObserver);
CleanupStack::PushL(self);
self->ConstructL();
return self;
}
 
CBatteryCheck* CBatteryCheck::NewL(MBatteryObserver& aObserver)
{
CBatteryCheck* self = CBatteryCheck::NewLC(aObserver);
CleanupStack::Pop(); // self;
return self;
}
 
void CBatteryCheck::ConstructL(void)
{
iTelephony = CTelephony::NewL();
GetBatteryInfo();
}
 
CBatteryCheck::~CBatteryCheck()
{
Cancel();
delete iTelephony;
}
 
CBatteryCheck::CBatteryCheck(MBatteryObserver& aObserver)
: CActive(EPriorityStandard),iObserver(aObserver),iBatteryInfoV1Pckg(iBatteryInfoV1)
{
CActiveScheduler::Add(this);
}
 
void CBatteryCheck::GetBatteryInfo()
{
if(iTelephony && !IsActive())
{
iGettingBattery = ETrue;
iTelephony->GetBatteryInfo(iStatus, iBatteryInfoV1Pckg);
SetActive();
}
}
 
void CBatteryCheck::RunL()
{
iObserver.BatteryLevelL(iBatteryInfoV1.iChargeLevel,iBatteryInfoV1.iStatus);
 
if(iStatus != KErrCancel)
{
iTelephony->NotifyChange(iStatus,CTelephony::EBatteryInfoChange,
iBatteryInfoV1Pckg);
SetActive();
}
 
iGettingBattery = EFalse;
}
 
void CBatteryCheck::DoCancel()
{
if(iGettingBattery)
iTelephony->CancelAsync(CTelephony::EGetBatteryInfoCancel);
else
iTelephony->CancelAsync(CTelephony::EBatteryInfoChangeCancel);
}

BatteryLevel.h

#include <Etel3rdParty.h>
 
class MBatteryObserver
{
public:
virtual void BatteryLevelL(TUint aChargeStatus, CTelephony::TBatteryStatus aStatus) = 0;
};
 
class CBatteryCheck : public CActive
{
public:
~CBatteryCheck();
static CBatteryCheck* NewL(MBatteryObserver& aObserver);
static CBatteryCheck* NewLC(MBatteryObserver& aObserver);
 
private:
CBatteryCheck(MBatteryObserver& aObserver);
void ConstructL(void);
 
private:
void GetBatteryInfo();
void RunL();
void DoCancel();
 
private:
MBatteryObserver& iObserver;
CTelephony* iTelephony;
CTelephony::TBatteryInfoV1 iBatteryInfoV1;
CTelephony::TBatteryInfoV1Pckg iBatteryInfoV1Pckg;
TBool iGettingBattery;
};

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fTalkE3aE4cargeE5fscreenE5fsaverX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ
User Rating: qfnZuserE5FratingQNx5E2E0000X