在iOS程序中以编程方式获取电池温度

3

我该如何在IOS程序中编程地获取电池温度?此外,在IOS中是否有任何API可以提供关于电池制造商的信息等。目前,我能够获取电池电量和充电状态。想要检查是否还有其他与电池相关的信息可以在IOS中获取。


为什么这是一个封闭的问题?对我来说它似乎完全有效。答案也似乎完全有效。 - Chris Williams
4个回答

4
正如@Fogh所指出的那样,使用公共API是不可能的。数据在IOKit中可用,但IOKit在iOS上被视为私有框架。如果您正在开发面向企业内部分发的应用程序,请使用IOKit(这是一个演示如何获取数据的示例程序:https://github.com/eldoogy/EEIOKitListener)。
如果您更加冒险,并想以某种方式在App Store应用程序中获取此数据,则可以使用我编写的一个名为UIDeviceListener的类,它基本上从系统中窃取数据,而不依赖于任何私有API,但我不能保证苹果会批准它... https://github.com/eldoogy/UIDeviceListener 无论您使用哪种方法,最终都将得到一个包含名为Temperature的键的字典,该键是表示摄氏度电池温度的整数(将其除以100转换为float以获得准确值)。

2019年..。不过很遗憾,它已经被从iOKit中移除了 :/ - Fattie

2
在公共API中不可能实现这一点。

我不明白为什么这个应用可以上传到AppStore https://appsto.re/hk/Wc4G-.i - cxphong

0

制造商信息可以托管在您的服务器上,您只需检测运行您的应用程序的设备,获取电池统计信息,解析并显示。

对于电池温度,如果没有相应的API,我猜它是使用当前CPU使用率和GPU使用率之类的近似值?


0
您可以使用IOKit中的以下私有API:
typedef struct __IOHIDEventSystemClient* IOHIDEventSystemClientRef;
typedef struct __IOHIDServiceClient *IOHIDServiceClientRef;
typedef struct __IOHIDEvent *IOHIDEventRef;
typedef double IOHIDFloat;

@interface HIDServiceClient : NSObject
{
    struct {
        struct __IOHIDEventSystemClient *system;
        void *serviceID;
        struct __CFDictionary *cachedProperties;
        struct IOHIDServiceFastPathInterface **fastPathInterface;
        struct IOCFPlugInInterfaceStruct **plugInInterface;
        void *removalHandler;
        unsigned int primaryUsagePage;
        unsigned int primaryUsage;
        struct _IOHIDServiceClientUsagePair *usagePairs;
        unsigned int usagePairsCount;
    } _client;
}

- (id)description;
- (void)dealloc;
- (unsigned long long)_cfTypeID;

@end

IOHIDEventSystemClientRef IOHIDEventSystemClientCreate(CFAllocatorRef allocator);
int IOHIDEventSystemClientSetMatching(IOHIDEventSystemClientRef client, CFDictionaryRef match);
CFArrayRef IOHIDEventSystemClientCopyServices(IOHIDEventSystemClientRef x);
CFStringRef IOHIDServiceClientCopyProperty(HIDServiceClient *service, CFStringRef property);
IOHIDEventRef IOHIDServiceClientCopyEvent(HIDServiceClient *event, int64_t , int32_t, int64_t);
IOHIDFloat IOHIDEventGetFloatValue(IOHIDEventRef event, int32_t field);

它在内部测试中有效,但如果您想上传到商店,可能会被拒绝。

关于如何使用这个的示例,您可以查看这个示例应用程序https://github.com/Dev1an/ThermalSensorMonitor

我在iPhone 7上的iOS 12和iPhone XS上的iOS 16上测试了IOKit代码,两者都运行良好。显然,只有带有Swift Charts的代码在iOS 16上才能工作。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接