Monitoring battery status with CTelephony

来源:互联网 发布:软件测试单元测试 编辑:程序博客网 时间:2024/06/07 06:22

Monitoring battery status with CTelephony


CBatteryCheck implementation illustrates how to check and monitor the battery status ofS60 3rd Edition devices withCTelephony 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 theCBatteryCheck.

When constructing CBatteryCheck, the initial battery level is checked withGetBatteryInfo()-function and after this function returns and calls back theRunL, all changes in battery status are monitored by callingNotifyChange for the battery status.

In the callback the aChangeStatus variable has the value of the current percentage of the battery power left, and theaStatus 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;};

Comments

Article Review by skumar_rao (20090910)

This article contains the useful example, especially for server applications. With the help of the class CBatteryCheck you could monitor state of the battery - it is important information in different cases. Also this class allows to detect the connect of the charger - as a reaction to this fact, you could for example start or stop your own application.
Article Review by User:FireSwarog (20090909)

I have tested this class on my Nokia 5800. It works fine. Anyway, it is necessary clearly understand how to work with active objects and how to use observers.

This article have a nice code snippet that can directly be used in any GUI / Non-UI applications by just copying to .h and .cpp files and adding to project. This article need to be re-formatted with headers. But that does not reduce the importance or usability of this article.


Article Review by savaj (20090928)

Knowing battery status is important for some application, for example background running exes. Article shows how to get battery status and charge level using CTelephony::GetBatteryInfo() API. The class CBatteryCheck implementation in this article is useful for beginners who feel hard to use active objects. This code example is useful to get battery's status and charge level in 3rd edition or 5th edition devices, as 2nd edition (except FP3)does not support CTelephony API.
原创粉丝点击