×
Namespaces

Variants
Actions

Archived:Observing battery state with properties using Symbian C++

From Nokia Developer Wiki
Jump to: navigation, search

Archived.pngArchived: This article is archived because it is not considered relevant for third-party developers creating commercial solutions today. If you think this article is still relevant, let us know by adding the template {{ReviewForRemovalFromArchive|user=~~~~|write your reason here}}.

Article Metadata
Tested with
Devices(s): Nokia N93
Compatibility
Platform(s): S60 3rd Edition, FP1
S60 3rd Edition (initial release)
Article
Keywords: KPSUidHWRMPowerState, KHWRMBatteryLevel, KHWRMBatteryStatus, KHWRMChargingStatus, RProperty, RProperty::Subscribe(), RProperty::Get()
Created: aknyman (10 Apr 2008)
Last edited: hamishwillee (06 May 2013)

Contents

Overview

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.

MMP file

The following libraries are required:

LIBRARY euser.lib

Header file

 //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

Source file

#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.
*/

 
}
}


Using CBatteryStateObserver class

  • In the header file:
CBatteryStateObserver* iBatteryStatusObserver;
CBatteryStateObserver* iBatteryLevelObserver;
CBatteryStateObserver* iChargingStatusObserver;
  • In the source file:
#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;

Postconditions

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.


This page was last modified on 6 May 2013, at 14:16.
82 page views in the last 30 days.