×

Discussion Board

Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Angry How to get battery level using CHWRMPower

    hi all,

    I've been trying in vain to get notified with battery level when it changes using CHWRMPower.

    I searched Wiki, Google, Dibo for examples but nothing exist.

    I tried documentation, but it has no enough information, and so the header file of hwrmpower.h



    Could anyone has a working example, or at least answer these questions:

    1- to use CHWRMPower* iPower; Is it must to be implemented via Active Object, or I can use it in my AppUI object?

    2- The object who will be drived from MHWRMBatteryPowerObserver, Is it must to be an Active Object?


    void PowerMeasurement(TInt aErr, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement);

    The above virtual function, who will call it? And which function I should use so system can call it with battery level?


    In my code: Header
    Code:
    #include <HWRMPower.h>
    
    class CPowerAppUi : public CAknViewAppUi,  public MHWRMBatteryPowerObserver
    {
    public:
    void PowerMeasurement(TInt aErr, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement);
    
    
    private:
        CHWRMPower* iPower;
    
    };
    In my code: Source
    Code:
    void CPowerAppUi::ConstructL()
    {
    iPower = CHWRMPower::NewL();
    iPower->SetPowerReportObserver(this);
    }
    
    
    void CPowerAppUi::PowerMeasurement(TInt aErr, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)
        {
    		TInt x = aMeasurement.iAverageCurrent;
    		TBuf<20>buf; buf.Num(x);
    		CEikonEnv::QueryWinL(buf, _L(""));
    
        }

    I'm sure a lot need to know how to use this class on Symbian^3.


    I hope you have an answer to my questions. thanks all

  2. #2
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,169

    Re: How to get battery level using CHWRMPower

    have you considered on checking the tutorial from SDK docs: http://library.developer.nokia.com/i...22F4A2EB6.html

    Even though the tutorial uses WaitForRequest, I would strongly suggest making new class and deriving it from CActive instead.

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Yes I checked this link and as much it helped me, a much as it confused me.

    Anyway, I get the code to work, but I have a couple of questions:

    1- I'm using an AO and it has its CHWRMPower as a member variable. So, is there any need to implement MHWRMBatteryPowerObserver ? Knowing that I will get the current value in RunL() via TBatteryConsumptionData.iRemainingPercentageCapacity ?

    2- when I use TInt StartAveragePowerReporting(TRequestStatus& aStatus, const TUint& aInterval); , What is the proper value of aInterval ?

    3- I get the battery level via TBatteryConsumptionData.iRemainingPercentageCapacity , and it shows me 38% , while Nokia Battery Monitor shows me 53%. Knowing that I used 2 other published apps from Nokia Store and they has my result 38%



    I hope my answer is clear and easy to be answered, Many thanks.

  4. #4
    Nokia Developer Moderator
    Join Date
    Mar 2003
    Location
    Lempäälä/Finland
    Posts
    29,169

    Re: How to get battery level using CHWRMPower

    If the docs are not clear, then experiment and try different values until you are satisfied. I never used the API, so all I can say you are inside the docs already.

  5. #5
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    thanks, and I'll get back here with results.

    B.R

  6. #6
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Here is my code, using AO, it works only when using iPower->GetBatteryInfo(iStatus,iBatteryConsumptionData); but after that I doesn't notify me when battery level changes, what is wrong or missing:

    BatteryLevel.cpp
    Code:
    #include "BatteryLevel.h"
    
    CBatteryCheck* CBatteryCheck::NewLC()
    {
        CBatteryCheck* self = new (ELeave) CBatteryCheck();
        CleanupStack::PushL(self);
        self->ConstructL();
        return self;
    }
     
    CBatteryCheck* CBatteryCheck::NewL()
    {
        CBatteryCheck* self = CBatteryCheck::NewLC();
        CleanupStack::Pop(); // self;
        return self;
    }
     
    void CBatteryCheck::ConstructL()
    {
        iPower = CHWRMPower::NewL();
        
        GetBatteryInfo();
    }
     
    CBatteryCheck::~CBatteryCheck()
    {
        Cancel();
        
        if(iPower)
        	{
    			delete iPower;
    			iPower = NULL;
        	}
    }
     
    CBatteryCheck::CBatteryCheck()
    : CActive(EPriorityStandard)
    {
        CActiveScheduler::Add(this);
    }
     
    void CBatteryCheck::GetBatteryInfo()
    {
    	Cancel();
    	
    	if(iPower && !IsActive())
    	  {
    
    		  iPower->GetBatteryInfo(iStatus,iBatteryConsumptionData);
    	      SetActive();
    	  }
    }
    
    void CBatteryCheck::RunL()
    {
    	if(iStatus == KErrNone)
    		{
                           // Here I'm sending the battery level to the AO owner, temporally via a Global Variable, 
                          // and it works for the first time as a response to GetBatteryInfo() function.
    			g_Appui->BatteryLevelL(iBatteryConsumptionData.iRemainingPercentageCapacity);
                          
                           // This line doesn't work, and I don't receive battery level notifications
    			iPower->StartAveragePowerReporting(iStatus, 5 );
    			SetActive();
        	}
        
    
    }
    
    void CBatteryCheck::DoCancel()
    {
    	iPower->StopAveragePowerReportingL();
    }
    
    TInt CBatteryCheck::RunError(TInt aError)
    {
    	return aError;
    }
    BatteryLevel.h
    Code:
    #ifndef BATTERYLEVEL_H_
    #define BATTERYLEVEL_H_
    
    #include <HWRMPower.h>
    
    class CBatteryCheck : public CActive
    { 
        public:
            ~CBatteryCheck();
            static CBatteryCheck* NewL();
            static CBatteryCheck* NewLC();
            void GetBatteryInfo();
        private:
            CBatteryCheck();
            void ConstructL();
     
        private:
            
            void RunL();
            void DoCancel();
            TInt RunError(TInt aError);
            
        private:        
            CHWRMPower::TBatteryConsumptionData iBatteryConsumptionData;
            CHWRMPower* iPower;     
    };
    
    
    #endif /* BATTERYLEVEL_H_ */

    Anyone has a suggestion?

  7. #7
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: How to get battery level using CHWRMPower

    Got your mail. I do not know this API, but checking return values is generally a good idea in Symbian C++. StartAveragePowerReporting has a result code.

  8. #8
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Thank you wizard_hu_ for your quick response.

    I checked it and it returns -8 , what does this mean ?


    /**
    System wide error code -8 : an invalid handle has been passed.

    A system wide error code indicates an error in the environment, or in
    user input from which a program may recover.

    A function involving a resource owned by a server or the kernel has
    specified an invalid handle.
    */
    const TInt KErrBadHandle=(-8);
    Last edited by SymNewbi; 2012-06-14 at 10:57. Reason: Adding error description

  9. #9
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Hi again,

    Now as I modified my code by adding iPower->SetPowerReportObserver(g_Appui); which returns 0

    The return value of iPower->StartAveragePowerReporting(iStatus, iInterval ); also has become 0


    For now that is good, but the question here:

    What is the point of using AO if the CHWRMPower object sends its periodically results to its Observer [ SetPowerReportObserver (g_Appui) ] via Virtual PowerMeasurement?

    Second Q:

    As the observer receives TBatteryPowerMeasurementData via PowerMeasurement(TInt aErr, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement) implementation, and I want TBatteryConsumptionData.iRemainingPercentageCapacity

    How can I solve this, as I ended up with one time notification of battery level from iPower->GetBatteryInfo(iStatus,iBatteryConsumptionData);?

  10. #10
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: How to get battery level using CHWRMPower

    Well, it means that something is wrong.
    In fact I do not have any SDK here, just checked the source code and seen this BadHandle thing:
    Code:
    EXPORT_C TInt CHWRMPower::StartAveragePowerReporting(TRequestStatus& aStatus, const TUint& aInterval)
    {
        TInt err = iPowerImpl->DoStartAveragePowerReporting(aStatus, aInterval);
        return err;
    }
    and
    Code:
    TInt CHWRMPowerImpl::DoStartAveragePowerReporting(TRequestStatus& aStatus, const TUint& aInterval)
    {	
        if (!iBatteryPowerMeasurementsAO)
        {
            return KErrBadHandle;
    so I checked where it is set, and it is
    Code:
    void CHWRMPowerImpl::DoSetPowerReportObserverL(MHWRMBatteryPowerObserver* aPowerReportCallback)
    {
        if(aPowerReportCallback)
        {
            if(iBatteryPowerMeasurementsAO)
            {
                iBatteryPowerMeasurementsAO->SetCallBack(aPowerReportCallback);
            }
            else
            {
                iBatteryPowerMeasurementsAO = CHWRMBatteryPowerMeasurementsAO::NewL(aPowerReportCallback, iClient);
    So I assume you have to implement that MHWRMBatteryPowerObserver thing and invoke SetPowerReportObserver. Some parts of these things are appear in your previous posts, so you just probably have to combine everything.
    Answering some of your questions: MHWRMBatteryPowerObserver can be implemented in any of your classes, it does not have to be a separate thing, neither an AO. And yes, it is required since PowerMeasurement method will get the actual measurements, periodically (as I understand).

    EDIT: According to #9 (posted in the same minute as this one) you are progressing well.

    EDIT2: after checking TBatteryPowerMeasurementData, it is completely different from what you want. For getting the remaining charge, you probably have to use the other method, GetBatteryInfo, and invoke it periodically from your code. The only thing what this average thing can do for you is to predict the change.
    Last edited by wizard_hu_; 2012-06-14 at 11:43.

  11. #11
    Nokia Developer Champion
    Join Date
    Jan 2008
    Location
    Moscow, Russia
    Posts
    3,348

    Re: How to get battery level using CHWRMPower

    Got your email, ashraf. According the "Power API Tutorial" at SDK help: "You must have implemented and set the appropriate observers to receive notification."
    Check this example (SDK help) - "MHWRMBatteryPowerObserver Class Reference"

  12. #12
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Now:

    PowerMeasurement(TInt aErr, CHWRMPower::TBatteryPowerMeasurementData& aMeasurement)

    the aErr is ( -2 )

    /**
    System wide error code -2 : an error that has no specific categorisation.

    A system wide error code indicates an error in the environment, or in
    user input from which a program may recover.
    */


    So, what could be wrong ?

  13. #13
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Wizard_hu_

    Based on your post #10 , we are now on the same page and understand the ambiguity of the situation.

    So, do I use a timer that check battery level using GetBatteryInfo periodically ?


    I was thinking about this solution as a last resort, as it will consume more resources than when getting notified by system when battery level actually changes.

  14. #14
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,751

    Re: How to get battery level using CHWRMPower

    Yes, that is what I mean.
    Note that if you do not need this particular API and its data, just want to check battery stuff in general, you can just RProperty::Subscribe to KPSUidHWRMPowerState+one or more of its keys (KHWRMBatteryLevel/BatteryStatus/ChargingStatus). Then you will get notifications, however the resolution is 'bar', as the phone displays battery charge as 0-7 bars, you will get notifications in 14% steps. CTelephony also has a similar API with similar data.

    Hmm, this tab has been open for a long time in my browser, sorry.

  15. #15
    Registered User
    Join Date
    Feb 2012
    Posts
    41

    Re: How to get battery level using CHWRMPower

    Thank you wizard for your detailed answer, it really means a lot.

    Actually I do need to get the battery level scaled to 100 degrees, so RPorperty and CTelephony will not do this trick.

    Anyway, I tried to use CHWRMPower with a Timer, and it works; But it has a major flow that I could fix / understand:

    The maximum returned value for this API is 91%, even when battery is fully charged !!

    So, the question is: Do you have more suggestions or idea how to get this API working properly ?

Similar Threads

  1. How to get the battery level from 1% to 100%
    By fengwujiutian in forum Symbian
    Replies: 18
    Last Post: 2010-01-26, 21:19
  2. How to get Battery level using OMA-DM
    By aviag78 in forum OMA DM/DS/CP
    Replies: 2
    Last Post: 2008-04-29, 06:19
  3. battery level
    By Bhoopathi Kummara in forum Symbian
    Replies: 1
    Last Post: 2007-12-29, 14:51
  4. regarding chrge level of Battery
    By symashi in forum Symbian
    Replies: 2
    Last Post: 2005-06-17, 08:31
  5. Battery level
    By nanard in forum Symbian
    Replies: 5
    Last Post: 2003-12-20, 13:28

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×