You Are Here:

Community: Wiki

This page was last modified on 9 October 2008, at 17:18.

CS000902 - Observing battery state with properties

From Forum Nokia Wiki



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()

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.

Related Wiki Articles

No related wiki articles found

Rate This

 
Bookmark this page: DeliciousDiggFacebookGoogleYahooStumbleUponRedditDiigoTechnocratiTwitter  Share this page Share this page Print this Page Print this page Invite a friend Invite a friend
京ICP备05048969号    Email Newsletters Press Terms & Conditions Privacy Policy Sitemap Contact Us © 2009 Nokia 
RDF Facets: qdcZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qdcZidentifierQSxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2findeE78E2ephpE2fE4cockingE5fphoneE5fkeypadX qdcZpublisherQUxhttpE3aE2fE2fswE2enokiaE2ecomE2fidE2fc764fd1cE2d8b06E2d499aE2d9a6aE2d17c3903d5a65E2fforumE5fnokiaE5fcrawlerE5fagentX qdcZtitleQSxE4cockingE20phoneE20keypadE20E2dE20ForumE20NokiaE20WikiX qdcZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qdcZtypeQUqfntypeZCommunityContentQ qdcZtypeQUqfntypeZE52esourceQ qdcZtypeQUqfntypeZWebpageQ qdcZtypeQUqfntypeZWikiContentQ qdcZtypeQUqmarsZManagedE52esourceQ qdcZtypeQUqwebZInformationE52esourceQ qdcZtypeQUqwebZPageQ qdcZtypeQUqwebZE52esourceQ qdcZtypeQUqrdfsZE52esourceQ qrssZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qfnZdistributionQUxhttpE3aE2fE2fwikiE2eforumE2enokiaE2ecomE2fX qfnZtopicQUqfnTopicZhardwareQRqdcZtypeQUqrdfsZE52esourceQRqmarsZrelevanceQNx100X qfnZtypeQUqfntypeZCommunityContentQ qfnZtypeQUqfntypeZE52esourceQ qfnZtypeQUqfntypeZWebpageQ qfnZtypeQUqfntypeZWikiContentQ qfnZupdatedQDx2008E2d10E2d02X qfnZuserE5ftagQSxhardwareX qmarsZdescriptionQSxEa0E20WikiE20javaE20symbianE5fosE20s60E20maemoE20cE2bE2bE20WikiE20HomeE20WikiE20HelpE20OverviewE20GlossaryE20CreateE20PageE20ProposeE20anE20ArticleE20SpotlightE20TopicE20E2dE20WE52TE20WidgetsE20ProgrammingE20E4canguageE20E2dE20SymbianE20CE2bE2bE20E2dE20OpenE20CE2fCE2bE2bE20E2dE20JavaE20E2dE20FlashE20E4citeE20E2dE20PythonE20WebE20TechnologiesE20E2dE20WE52TE20WidgetsE20E2dE20WidSetsE20ToolsE20andE20SE44KE20CodeE20E45E78amplesE20KnowledgeE20BaseE20TechnologyE20AreasE20SoftwareE20PlatformsE20E44evelopmentE20ProcessE20E3fE3fWikiE20ChineseE20E3fE3fE3fWikiE20JapaneseE20PortugueseE2fBrazilianE20E52ussianE20WhatE20linksE20hereE20UploadE20fileE20SpecialE20pagesE20PrintableE20versionE44ownloadE20asE20PE44FE20GoE20ToE20E2eE2eE2eX qmarsZlanguageQUxhttpE3aE2fE2fswE2enokiaE2ecomE2flanguageE2d1E2fenX qrdfZtypeQUqfnZE45E78cludedFromGeneralE4cistingsQ qrdfZtypeQUqfntypeZCommunityContentQ qrdfZtypeQUqfntypeZE52esourceQ qrdfZtypeQUqfntypeZWebpageQ qrdfZtypeQUqfntypeZWikiContentQ qrdfZtypeQUqmarsZManagedE52esourceQ qrdfZtypeQUqwebZInformationE52esourceQ qrdfZtypeQUqwebZPageQ qrdfZtypeQUqwebZE52esourceQ qrdfZtypeQUqrdfsZE52esourceQ