Join Now
Quality Rating:
  • Currently 3.0 / 5
(3.0 / 5 - 1 vote cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 20:16, 15 December 2007.

Monitoring battery status with CTelephony

From Forum Nokia Wiki

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:

- CTelephony::EPoweredByBattery: Normal battery operation

- CTelephony::EBatteryConnectedButExternallyPowered: Battery charger connected

- CTelephony::ENoBatteryConnected: No battery (powered by charger)

- CTelephony::EPowerStatusUnknown: Unknown battery status.

BatteryLevel.cpp

CBatteryCheck::~CBatteryCheck()
{
	Cancel();
	delete iTelephony;
}
 
void CBatteryCheck::ConstructL(void)
{
    iTelephony = CTelephony::NewL();
    GetBatteryInfo();
}
 
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(MBatteryObserver& aObserver);
    void ConstructL(void);
    ~CBatteryCheck();
private:	
    void GetBatteryInfo();
    void RunL();
    void DoCancel();
private:
    MBatteryObserver& 			iObserver;
    CTelephony* 			iTelephony;
    CTelephony::TBatteryInfoV1 		iBatteryInfoV1;
    CTelephony::TBatteryInfoV1Pckg 	iBatteryInfoV1Pckg;
    TBool				iGettingBattery;
   };
Related Discussions
Thread Thread Starter Forum Replies Last Post
Stronger Battery on 6230 lamberet General Discussion 1 2004-12-09 23:47
CBatteryCheck Naco General Symbian C++ 8 2008-03-10 10:38
Transparent Thin Status Pane? Thorsdad Themes/Carbide.ui 5 2008-05-10 18:23
Is there a way to show battery level in a scale from 0 to 99% instead 7preset values? alb3530 Python 4 2006-12-27 16:09
Detailed battery charge information Fredrik Eldh General Symbian C++ 1 2008-05-16 12:44
 
Powered by MediaWiki
     
     RDF Facets:
     
     
     qfnZtypeQUqfnTypeZCommunityContentQ
     qfnZtypeQUqfnTypeZWebpageQ
     qfnZtypeQUqfnTypeZWikiContentQ
     qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX