Monitoring battery status with CTelephony

Monitoring battery status with CTelephony


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;
};

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.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值