3

I would like to know whether it is possible to measure how much energy is flowing in a mobile device (IOS or Android) when it is charging ? In watt per Hour or MilliAmperes per Hour for exemple.

Basically I would like to measure how much electricity I have taken from my charge. Is there a native or low level API for that ?

thanks for yout help

General Grievance
  • 4,555
  • 31
  • 31
  • 45
G.D.
  • 33
  • 4

2 Answers2

1

Android

Battery level checking Android

Battery level checking Android 1

See demo : Battery Demo Android

iOS

Yes in iOS Device when you charging device you can get Inforation about battery status. Notification of your batteryLevelChanged and batteryStateChanged

See demo : Batterry Demo iOS

Note : Run This demo in iOS Device. Not Simulator.

// Register for battery level and state change notifications.
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(batteryLevelChanged:)
                                                 name:UIDeviceBatteryLevelDidChangeNotification object:nil];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(batteryStateChanged:)
                                                 name:UIDeviceBatteryStateDidChangeNotification object:nil];

Code for updateBatteryLevel:

- (void)updateBatteryLevel
{
    float batteryLevel = [UIDevice currentDevice].batteryLevel;
    if (batteryLevel < 0.0) {
        // -1.0 means battery state is UIDeviceBatteryStateUnknown
        self.levelLabel.text = NSLocalizedString(@"Unknown", @"");
    }
    else {
        static NSNumberFormatter *numberFormatter = nil;
        if (numberFormatter == nil) {
            numberFormatter = [[NSNumberFormatter alloc] init];
            [numberFormatter setNumberStyle:NSNumberFormatterPercentStyle];
            [numberFormatter setMaximumFractionDigits:1];
        }

        NSNumber *levelObj = [NSNumber numberWithFloat:batteryLevel];
        self.levelLabel.text = [numberFormatter stringFromNumber:levelObj];
    }
}

- (void)updateBatteryState
{
    NSArray *batteryStateCells = @[self.unknownCell, self.unpluggedCell, self.chargingCell, self.fullCell];

    UIDeviceBatteryState currentState = [UIDevice currentDevice].batteryState;

    for (int i = 0; i < [batteryStateCells count]; i++) {
        UITableViewCell *cell = (UITableViewCell *) batteryStateCells[i];

        if (i + UIDeviceBatteryStateUnknown == currentState) {
            cell.accessoryType = UITableViewCellAccessoryCheckmark;
        }
        else {
            cell.accessoryType = UITableViewCellAccessoryNone;
        }
    }
}
Kirit Modi
  • 23,155
  • 15
  • 89
  • 112
  • Hi Kirit, thanks for your help ! Doing these codes, what kind of info do I get exactly ? % of battery level ? Miliamperes...? Sorry if my question seems a little bit naïve :) – G.D. Sep 15 '15 at 13:09
  • self.levelLabel.text you get already percentage value in ios, same as Android demo, Not take Milometer. – Kirit Modi Sep 15 '15 at 13:16
  • ok. Just to confirm : it is possible to get miliampers via Android, but not possible via IOS ? – G.D. Sep 15 '15 at 13:18
0

One of he simplest solutions (and most accurate) is to get a current meter that records the current through your wall plug. Any power measure API depends upon what instrumentation there is on the board. Sometimes it is a direct measure (accurate) and sometimes it is computed / inferred (maybe accurate, maybe very inaccurate).

These data recorders can range from industrial (more expensive, less work) to DIY (cheap, more work). Which you want depends upon how much time you have and how often you are going to use it.

Taylor Kidd
  • 1,463
  • 1
  • 9
  • 11
  • Hi Taylor, thanks for your answer ! So, to see If I get it correctly : it is not possible to get this only with the mobile phone or Tablette because it won't be accurate enough ? – G.D. Sep 15 '15 at 14:00
  • It all depends upon the hardware. I've seen both. The decision has to do with the engineering needs of the designers when they need to debug and spec the components. The from the wall will definitely be accurate because it is a direct measure. A software is likely accurate enough for most work. If you really have to know precisely, I'd go with the from the wall. If approximately is good enough, say within +/- 20%, then using the supplied APIs should be fine. – Taylor Kidd Sep 15 '15 at 17:50
  • Hi Taylor, thanks a lot for the details. I think I'll go for the wall then and probably test with my appli how accurate it is :) – G.D. Sep 15 '15 at 18:45