Battery Level low resolution?

Discussion in 'iOS Programming' started by xArtx, Feb 18, 2013.

  1. xArtx macrumors 6502a

    Mar 30, 2012
    Hi Guys,
    I have just got battery monitoring working for the first time.
    Am I the only one who's seeing the level go down in increments of 5% ?
    I might think this is normal, and we just didn't get the same tool Apple uses,
    but also, when plugged into a power source, I don't see a negative number,
    just the battery level.

    So it's 100%, 95%, 80%, 75%, and so on....
    Thanks, Art.
  2. ArtOfWarfare macrumors G3


    Nov 26, 2007
    That sounds right. I think iOS takes that number and combines it with data about how long the screen has been on an how long it's been plugged in to display the 1% increment it shows the user. I know I've seen my iPhone shut off moments after it said it was at 5% more than once.
  3. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    That's ok, as long as I'm not seeing something different from everyone else.
    How do you detect it's plugged in? I've never seen a negative number in the
    battery level value.
  4. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    Have you looked into UIDeviceBatteryState yet?
  5. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    iOS devices use lithium ion batteries. You can't measure the remaining charge with lithium batteries like you can with other batteries. They hum along putting out full power, then suddenly stop without warning. Instead, lithium batteries have chips in them that track usage, battery age, etc, and use those factors to estimate remaining battery power. The estimate is just that, an estimate. As the battery ages, the estimate gets less accurate.

    It might be that Apple is using other factors to estimate remaining battery, but it might also be that they use a completely different private API. My money is on the private API.
  6. xArtx thread starter macrumors 6502a

    Mar 30, 2012
    It's all ok how they are doing it, but private API or not, isn't it odd that they
    are creating the potential for an App to say one thing, and the springboard
    to say another? Whatever they are doing, they could share that value with us.

    No, didn't get to reading into it.. but the tutorial did say you get a negative
    value if the device is plugged in. I still haven't seen that.
    Thanks though, I'll want to know the charging state.
  7. Duncan C macrumors 6502a

    Duncan C

    Jan 21, 2008
    Northern Virginia
    Don't trust third party tutorials without verifying what they say. A quick look at the docs for the UIDevice method batteryLevel reveals the following:

    Battery level ranges from 0.0 (fully discharged) to 1.0 (100% charged). Before accessing this property, ensure that battery monitoring is enabled.

    You don't get a negative number if the battery is charging - only if you have not enabled battery monitoring.

Share This Page