Join Now
Quality Rating:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)
Expertise Level:
  • Currently 0.0 / 5
(0.0 / 5 - 0 votes cast)

This page was last modified 07:14, 17 April 2008.

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 Discussions
Thread Thread Starter Forum Replies Last Post
Question about input method. cs_lcmaa Symbian User Interface 12 2006-03-23 05:46
Known Issues Nokia Ron Mobile Java General 49 2008-04-04 18:38
S60 Speed wernerkriel Series 40 & S60 Platform Feedback 11 2007-11-11 22:02
N-gage reset ivli General Symbian C++ 1 2004-09-16 21:23
sound could not be play again, help!! dragonxu Mobile Java Media (Graphics & Sounds) 8 2005-04-04 10:00
 
Powered by MediaWiki