【问题标题】:energy flow in when charging a mobile or Tablet为手机或平板电脑充电时流入的能量
【发布时间】:2015-09-15 09:03:25
【问题描述】:

我想知道是否可以测量移动设备(IOS 或 Android)充电时的电量? 以瓦特/小时或毫安/小时为例。

基本上我想测量我从充电中消耗了多少电量。 是否有用于此的本机或低级 API?

感谢您的帮助

【问题讨论】:

    标签: android ios mobile flow energy


    【解决方案1】:

    安卓

    Battery level checking Android

    Battery level checking Android 1

    查看演示:Battery Demo Android

    iOS

    是的,在 iOS 设备中,当您为设备充电时,您可以获得有关电池状态的信息。通知您的batteryLevelChangedbatteryStateChanged

    查看演示:Batterry Demo iOS

    注意:在 iOS 设备上运行此演示。不是模拟器。

    // 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];
    

    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,感谢您的帮助!做这些代码,我能得到什么样的信息?电池电量百分比?毫安……?对不起,如果我的问题看起来有点幼稚:)
    • self.levelLabel.text 你已经在 ios 中获得了百分比值,与 Android 演示相同,不采用 Milometer。
    • 好的。只是为了确认:可以通过 Android 获得 miliampers,但不能通过 IOS 获得?
    【解决方案2】:

    最简单(也是最准确)的解决方案之一是使用电流表来记录通过墙上插头的电流。任何功率测量 API 都取决于板上的仪器。有时是直接测量(准确),有时是计算/推断(可能准确,也可能非常不准确)。

    这些数据记录器的范围从工业(更昂贵,工作量更少)到 DIY(便宜,工作量更大)。你想要什么取决于你有多少时间以及你打算使用它的频率。

    【讨论】:

    • 嗨,泰勒,感谢您的回答!所以,看看我是否正确:仅通过手机或平板电脑无法获得此信息,因为它不够准确?
    • 这一切都取决于硬件。两个我都见过。该决定与设计人员在需要调试和指定组件时的工程需求有关。从墙上看肯定是准确的,因为它是直接测量。对于大多数工作,软件可能足够准确。如果您真的必须精确地知道,我会选择从墙上。如果大约足够好,比如在 +/- 20% 以内,那么使用提供的 API 应该没问题。
    • 嗨,泰勒,非常感谢您提供的详细信息。我想我会去墙然后可能用我的应用程序测试它有多准确:)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-27
    • 2016-01-09
    • 1970-01-01
    • 2011-09-29
    • 1970-01-01
    • 2012-12-08
    相关资源
    最近更新 更多