| ID | CS000902 | Creation date | April 17, 2008 |
| Platform | S60 3rd Edition, FP1 | Tested on devices | Nokia N93 |
| Category | Symbian C++ | Subcategory | Files/Data, Hardware |
| Keywords (APIs, classes, methods, functions): KPSUidHWRMPowerState, KHWRMBatteryLevel, KHWRMBatteryStatus, KHWRMChargingStatus, RProperty, RProperty::Subscribe(), RProperty::Get() |
This snippet implements a generic class that can be used to receive notifications on battery state changes. The class instance can be registered to listen to the category KPSUIDHWRMPowerState which contains Publish&Subscribe keys for battery status changes, battery level changes, and charging status changes.
This snippet can be self-signed.
The following libraries are required:
LIBRARY euser.lib
//batteryobserver.h
#ifndef BATTERYSTATEOBSERVER_H
#define BATTERYSTATEOBSERVER_H
// INCLUDES
#include <e32base.h>
#include <e32property.h>
class CBatteryStateObserver : public CActive
{
enum {EPriority=0};
public:
static CBatteryStateObserver* NewL( const TUid aUid, const TUint32 aKey);
virtual ~CBatteryStateObserver();
private:
CBatteryStateObserver( const TUid aUid, const TUint32 aKey);
void ConstructL();
void RunL();
void DoCancel();
private:
RProperty iProperty;
const TUid iUid;
const TUint32 iKey;
};
#endif // BATTERYSTATEOBSERVER_H
#include <HWRMPowerStateSDKPSKeys.h>
#include "batteryobserver.h"
CBatteryStateObserver::CBatteryStateObserver(
const TUid aUid, const TUint32 aKey)
: CActive(EPriority), iUid( aUid ),
iKey( aKey )
{
}
CBatteryStateObserver* CBatteryStateObserver::NewL(const TUid aUid, const TUint32 aKey)
{
CBatteryStateObserver* self=
new(ELeave) CBatteryStateObserver(aUid, aKey);
CleanupStack::PushL(self);
self->ConstructL();
CleanupStack::Pop(self);
return self;
}
void CBatteryStateObserver::ConstructL()
{
User::LeaveIfError(iProperty.Attach(iUid, iKey));
CActiveScheduler::Add(this);
// initial subscription and process current property value
RunL();
}
CBatteryStateObserver::~CBatteryStateObserver()
{
Cancel();
iProperty.Close();
}
void CBatteryStateObserver::DoCancel()
{
iProperty.Cancel();
}
void CBatteryStateObserver::RunL()
{
//resubscribe before processing new value to prevent missing updates
iProperty.Subscribe( iStatus );
SetActive();
switch(iKey)
{
case KHWRMBatteryLevel:
{
//KHWRMBatteryLevel updated, do something...
break;
}
case KHWRMBatteryStatus:
{
//KHWRMBatteryStatus updated, do something...
break;
}
case KHWRMChargingStatus:
{
//KHWRMChargingStatus updated, do something...
break;
}
default:
break;
}
// property updated, get new value
TInt keyValue;
if( iProperty.Get( keyValue ) == KErrNotFound )
{
// property deleted, do necessary actions here...
}
else
{
// use new value...
/*
- Enumerations for EPSHWRMBatteryLevel(Battery level of device)
EBatteryLevelUnknown Uninitialized or some other error.
EBatteryLevelLevel0 Lowest battery level.
EBatteryLevelLevel1
EBatteryLevelLevel2
EBatteryLevelLevel3
EBatteryLevelLevel4
EBatteryLevelLevel5
EBatteryLevelLevel6
EBatteryLevelLevel7 Highest battery level.
- Enumerations for EPSHWRMBatteryStatus(Battery status of device)
EBatteryStatusUnknown Uninitialized or some other error.
EBatteryStatusOk This can also be used during charging.
EBatteryStatusLow Show note to user "Battery low".
EBatteryStatusEmpty Show "recharge battery" note to user.
- Enumerations for EPSHWRMChargingStatus(Charging status of device)
EChargingStatusError Some error has occurred when charger is
connected or charging.
EChargingStatusNotConnected Charger not connected/uninitialized.
EChargingStatusCharging Device is charging.
EChargingStatusNotCharging Charger is connected, device not charging.
EChargingStatusAlmostComplete Charging almost completed.
EChargingStatusChargingComplete Charging completed.
EChargingStatusChargingContinued Charging continued after brief interruption.
*/
}
}
CBatteryStateObserver* iBatteryStatusObserver;
CBatteryStateObserver* iBatteryLevelObserver;
CBatteryStateObserver* iChargingStatusObserver;
#include <HWRMPowerStateSDKPSKeys.h>
//create the observers
iBatteryStatusObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMBatteryStatus);
iBatteryLevelObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMBatteryLevel);
iChargingStatusObserver =
CBatteryStateObserver::NewL(KPSUidHWRMPowerState,KHWRMChargingStatus);
//delete the observers
delete iBatteryStatusObserver;
delete iBatteryLevelObserver;
delete iChargingStatusObserver;
The up-to-date information on the charging status, battery level, and battery state of the device is received and the user can write his or her own implementation in the CBatteryStateObserver::RunL() method according to published values.
No related wiki articles found