3 Replies Latest reply on Sep 30, 2013 7:57 AM by Afshin Ghajar

    Android issue with Battery Module API

      Hi,

       

      I used Battery Module API (Rhoelements version 2) to record battery level of the device periodically in 1 minute interval.

      On Windows Mobile devices it's working correctly and I have battery level recorded consistently. Ranging from 25% up to 100%.

      But, the same code on Android does not work as it is expected. Always battery level is 60%. Even though the OS is showing different values. I tried it with charging and without charging, on my device and even customer device, all showing just one constant value 60%.

       

      With help of logs understood there is an exception when the very first Battery method is called.

       

      Battery.batteryEvent = "/app/Resource/record_battery_level"

      ...

       

      Although Battery Module is supported in both Windows Mobile and Android (based on Rho mobile API document) but, the same code throws exception on Android not on windows Mobile.

       

      Any suggestion is appreciated,

       

      Thanks,

       

      Afshin

        • Re: Android issue with Battery Module API
          Kutir Mobility

          Could you please share the relevant error log?

          What version of RhoMobile(Rhodes, RhoElements) do you use ?

           

          Visnupriya R

          Kutir Mobility

          • Re: Android issue with Battery Module API
            Robert Galvin

            What is the device and OS version being used on Android? Can you share your code snip as well?

              • Re: Android issue with Battery Module API

                Robert and Visnupriya,

                I believe device OS device is 4.x (I'm afraid I don't have device now since I am out of office for a couple of days)

                Rhodes: 2.2.1.13

                There's no error log from Rho framework, I added my own log to step into the issue.

                 

                Here is code:

                When application initializes (application.rb), calls method initialize_battery_checking below:


                application.rb:

                ...

                  def initialize_battery_checking

                    if !System.get_property('is_emulator')

                      Rho::Timer.start(BATTERY_CHECKING_INTERVAL_IN_SECONDS * 1000, "/app/Resource/start_battery_recorder", "")

                    end

                  end

                ...

                 

                and in Resource controller start_battery_recorder method  called after 1 sec :


                resource_controller.rb

                ...

                # event for get and set battery level

                  def record_battery_level

                    begin

                      #validate user is inactive

                      validate_idleness

                      if @params['batteryLifeKnown'].to_s == "true"

                        bat_level = @params['batteryLifePercent'].to_i

                        Rho.get_app.set_battery_level(bat_level)    

                      else

                        Rho.get_app.set_battery_level(0)

                        RhoLog.error("Battery" , "Unkown battery life: " + @params['batteryLifePercent'] + "% , AC=" + @params['acLineStatus'])

                      end     

                      ac_line_connected = @params['acLineStatus'].to_i

                      Rho.get_app.set_ac_line_connected(ac_line_connected)

                    rescue Exception => e

                      RhoLog.error("Battery", "error getting battery status " + e.message)

                    ensure

                      Rho::Timer.start(AppApplication::BATTERY_CHECKING_INTERVAL_IN_SECONDS.to_i * 1000, "/app/Resource/start_battery_recoder", "")

                    end

                  end

                 

                  # wiring battery event

                  def start_battery_recorder  

                     Battery.batteryEvent = "/app/Resource/record_battery_level"

                     Battery.sampleInterval = 0 # 1min

                     Battery.getBatteryStatus  

                  end

                ...

                 

                When I added my own log, I noticed first line above (in start_battery_recorder) was not executed at all ( JUST on android). To me it looks like Battery.batteryEvent   is unknown in Android because the flow did not go to the the second line (Battery.sampleInterval = 0).

                 

                Thanks for your help,

                Afshin