iOS基础篇

关于iOS电量检测和分析的调研

2019-06-20  本文已影响0人  child_cool

一、私有方法获取电量相关的信息

#include <dlfcn.h>
- (NSDictionary*)fetchBatteryInfo
{
    mach_port_t *kIOMasterPortDefault;
    
    kern_return_t (*ioRegistryEntryCreateCFProperties)(mach_port_t entry, CFMutableDictionaryRef *properties, CFAllocatorRef allocator, UInt32 options) = NULL;
    
    mach_port_t (*ioServiceGetMatchingService)(mach_port_t masterPort, CFDictionaryRef matching CF_RELEASES_ARGUMENT) = NULL;
    
    CFMutableDictionaryRef (*ioServiceMatching)(const char *name) = NULL;
    
    CFTypeRef (*ioPSCopyPowerSourcesInfo)(void) = NULL;
    
    CFArrayRef (*ioPSCopyPowerSourcesList)(CFTypeRef blob) = NULL;
    
    CFDictionaryRef (*ioPSGetPowerSourceDescription)(CFTypeRef blob, CFTypeRef ps) = NULL;
    
    CFMutableDictionaryRef powerSourceService = NULL;
    mach_port_t platformExpertDevice = 0;

    var handle = dlopen("/System/Library/Frameworks/IOKit.framework/Versions/A/IOKit", RTLD_LAZY);
    
    ioRegistryEntryCreateCFProperties = dlsym(handle, "IORegistryEntryCreateCFProperties");
    kIOMasterPortDefault = dlsym(handle, "kIOMasterPortDefault");
    ioServiceMatching = dlsym(handle, "IOServiceMatching");
    ioServiceGetMatchingService = dlsym(handle, "IOServiceGetMatchingService");
    
    ioPSCopyPowerSourcesInfo = dlsym(handle, "IOPSCopyPowerSourcesInfo");
    ioPSCopyPowerSourcesList = dlsym(handle, "IOPSCopyPowerSourcesList");
    ioPSGetPowerSourceDescription = dlsym(handle, "IOPSGetPowerSourceDescription");
    
    if (ioRegistryEntryCreateCFProperties != NULL &&
        ioServiceMatching != NULL &&
        ioServiceGetMatchingService != NULL)
    {
        powerSourceService = ioServiceMatching("IOPMPowerSource");
        platformExpertDevice = ioServiceGetMatchingService(*kIOMasterPortDefault, powerSourceService);
    }
    
    NSMutableDictionary *batteryInfo = nil;
    
    if(powerSourceService != NULL && platformExpertDevice != 0)
    {
        CFMutableDictionaryRef prop = NULL;
        ioRegistryEntryCreateCFProperties(platformExpertDevice, &prop, 0, 0);
        if(prop != NULL)
        {
            batteryInfo = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary*)prop];
            CFRelease(prop);
        }
    }
    
    var blob = ioPSCopyPowerSourcesInfo();
    var sources = ioPSCopyPowerSourcesList(blob);
    CFDictionaryRef pSource = NULL;

    var numOfSources = CFArrayGetCount(sources);
    for(CFIndex i = 0; i < numOfSources; I++)
    {
        pSource = ioPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
        if(pSource != NULL)
        {
            if(batteryInfo == nil)
                batteryInfo = [NSMutableDictionary dictionaryWithDictionary:(NSDictionary*)pSource];
            else
                [batteryInfo setValuesForKeysWithDictionary:(NSDictionary*)pSource];
            
            break;
        }
    }
    
    dlclose(handle);
    CFRelease(blob);
    CFRelease(sources);
    
    return batteryInfo;
}
{
    "Battery Provides Time Remaining" = 1;
    BatteryHealth = Poor;
    BatteryHealthCondition = "Check Battery";
    BatteryInstalled = 1;
    "Current Capacity" = 100;
    ExternalConnected = 1;
    "Is Charged" = 1;
    "Is Charging" = 0;
    "Is Present" = 1;
    "Max Capacity" = 100;
    Name = "InternalBattery-0";
    "Power Source ID" = 2424931;
    "Power Source State" = "AC Power";
    "Raw External Connected" = 1;
    "Show Charging UI" = 1;
    "Time to Empty" = 0;
    "Time to Full Charge" = 0;
    "Transport Type" = Internal;
    Type = InternalBattery;
}
{
    AdapterInfo = 16384;
    AtCriticalLevel = 0;
    "Battery Provides Time Remaining" = 1;
    BatteryHealth = Good;
    Current = 1000;
    "Current Capacity" = 87;
    CurrentCapacity = 1131;
    DesignCycleCount = 300;
    ExternalChargeCapable = 1;
    ExternalConnected = 1;
    FullyCharged = 0;
    IOClass = AppleARMPMUCharger;
    "Is Charging" = 1;
    "Is Finishing Charge" = 0;
    "Is Present" = 1;
    IsCharging = 1;
    "Max Capacity" = 100;
    MaxCapacity = 1300;
    Name = "InternalBattery-0";
    "Power Source ID" = 4063331;
    "Power Source State" = "AC Power";
    "Raw External Connected" = 1;
    "Show Charging UI" = 1;
    "Time to Empty" = 0;
    "Time to Full Charge" = 0;
    "Transport Type" = Internal;
    Type = InternalBattery;
    Voltage = 4178;
}

二、sysdiagnose

三、系统提供接口

 // #import <UIKit / UIDevice.h> 
   UIDevice * device = [UIDevice currentDevice ];
   device.batteryMonitoringEnabled = YES ;
   //。的UIDevice返回的batteryLevel的范围在0到1之间
   NSUInteger batteryLevel = device.batteryLevel * 100 ;
   //获取充电状态
   UIDeviceBatteryState state = device.batteryState;
   if(state == UIDeviceBatteryStateCharging || state == UIDeviceBatteryStateFull){
        //正在充电和电池已满 
   }

四、Ecergy Log

五、Energy Impact

上一篇 下一篇

猜你喜欢

热点阅读