How to get battery consumption value for an iOS ap

2019-03-13 11:02发布

问题:

I'm currently trying to improve the battery usage of the app we're developing.

The only thing I found was percentage with a jailbreak solution (here),

I did not find absolute value like 3.4 mAh/min as example.

I also did not find iOS guidelines to assess the battery consumption.

Ideally, I would love to see is something like that :

  • Google Analytics SDK : 0.4 mAh/min
  • Bluetooth SDK : 1.0 mAh/min
  • API SDK : 2.0 mAh/min

Something as Battery Doctor is providing :

Is that even possible ?

Thank for your help !

回答1:

We have had mixed success using https://github.com/ethan605/ios-battery-stat, which relies on a private API.

So, you cannot use that if you deploy your app on the Apple Store, but it can be useful when deploying to TestFlight or similar beta-user app stores.

Sami



回答2:

After doing some tests, over a couple of days, I have a VERY strong suspicion that these numbers we see are NOT actual measurements on the device, they seem WAY too constant, and only some of the actually running apps are on the list...

I have a THEORY that they have some sort of limited database, and if any app on the phone is matching this list, it is included.

It could still be useful as a hint on what uses a lot, and what uses a little. But I do not think you should see it as 'real' numbers from your phone. Can anyone confirm or disprove this theory???

That also explains why you can't find a method to get this info

There are also TWO almost identical apps one with a orange and one with a green battery icon, And it seems to be the same apps that are included and excluded. that seems a bit fishy too... There is a third app "Battery Lite", that are similar but without mAh/min readings (strange unit btw)



回答3:

Try UIDeviceListener, it basically steals the entire battery data dictionary from UIDevice without using any private APIs. It exposes tons of information:

{
    AdapterDetails =     {
        Amperage = 1000;
        Description = "usb host";
        FamilyCode = "-536854528";
        PMUConfiguration = 1000;
        Watts = 5;
    };
    AdapterInfo = 16384;
    Amperage = 1000;
    AppleRawCurrentCapacity = 1279;
    AppleRawMaxCapacity = 1275;
    AtCriticalLevel = 0;
    AtWarnLevel = 0;
    BatteryData =     {
        BatterySerialNumber = REDACTED;
        ChemID = 355;
        CycleCount = 524;
        DesignCapacity = 1420;
        Flags = 640;
        FullAvailableCapacity = 1325;
        ManufactureDate = REDACTED;
        MaxCapacity = 1273;
        MfgData = REDACTED;
        QmaxCell0 = 1350;
        StateOfCharge = 100;
        Voltage = 4194;
    };
    BatteryInstalled = 1;
    BatteryKey = "0003-default";
    BootBBCapacity = 52;
    BootCapacityEstimate = 2;
    BootVoltage = 3518;
    CFBundleIdentifier = "com.apple.driver.AppleD1815PMU";
    ChargerConfiguration = 990;
    CurrentCapacity = 1275;
    CycleCount = 524;
    DesignCapacity = 1420;
    ExternalChargeCapable = 1;
    ExternalConnected = 1;
    FullyCharged = 1;
    IOClass = AppleD1815PMUPowerSource;
    IOFunctionParent64000000 = <>;
    IOGeneralInterest = "IOCommand is not serializable";
    IOInterruptControllers =     (
        IOInterruptController34000000,
        IOInterruptController34000000,
        IOInterruptController34000000,
        IOInterruptController34000000
    );
    IOInterruptSpecifiers =     (
        <03000000>,
        <26000000>,
        <04000000>,
        <24000000>
    );
    IOMatchCategory = AppleD1815PMUPowerSource;
    IOPowerManagement =     {
        CurrentPowerState = 2;
        DevicePowerState = 2;
        MaxPowerState = 2;
    };
    IOProbeScore = 0;
    IOProviderClass = AppleD1815PMU;
    InstantAmperage = 0;
    IsCharging = 0;
    Location = 0;
    Manufacturer = A;
    MaxCapacity = 1275;
    Model = "0003-A";
    Serial = REDACTED;
    Temperature = 2590;
    TimeRemaining = 0;
    UpdateTime = 1461830702;
    Voltage = 4182;
    "battery-data" =     {
        "0003-default" = <...>;
        "0004-default" = <...>;
        "0005-default" = <...};
    "built-in" = 1;
}

Of special interest is the InstantAmperage key, which (while the device is unplugged) shows actual device power consumption in mAh. While your app is running, that will show you exact power consumption for your app.

UPDATE: Based on the iOS 10 beta, it looks like Apple has completely removed this information from the dictionary (regardless of how it is loaded, even if we directly load it by calling IOKit). The dictionary still exists, but it now contains only basic keys such as ExternalConnected and CurrentCapacity.



回答4:

Instruments won't give you a pretty picture like this but it will show you CPU, GPU usage which should generally be an indicator of battery drain and is certainly an good way to look at your application's performance/hardware utilization.



回答5:

I'm also searching for the same, but haven't got anything yet but few initial workarounds. If you are still searching for solution, you can start with this.

You can get device's logs both crash logs and console logs, from console logs you may get the information about battery usage, it may not give you a current(mA) info, but to start with this will help you.

here is the link to get the devices logs.