Reputation: 249
How can I obtain the temperature of the Battery in IOS programatically. Also are there any API's in IOS that would give us any information on the Battery Manufacturer etc. Currently I am able to obtain the Battery level and charging status . Wanted to check if there is any other Battery related information that we can obtain in IOS
Upvotes: 3
Views: 4289
Reputation: 4785
You can use the following private API from IOKit:
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);
It works for internal testing but will probably be rejected if you want to upload it to the store.
For an example on how to use this. You can look at this sample app https://github.com/Dev1an/ThermalSensorMonitor
I tested the IOKit code on an iPhone 7 with iOS 12 and an iPhone XS with iOS 16, both are working fine. Obviously the code with the Swift Charts only works on iOS 16.
Upvotes: 0
Reputation: 2869
As @Fogh correctly indicated, that isn't possible using the public API. The data is available in IOKit
, but IOKit
is considered a private framework on iOS. If you're developing for in-house distribution, just use IOKit
(here's an example program that shows how to get the data: https://github.com/eldoogy/EEIOKitListener)
If you're more adventurous and would like to somehow get this data in an App Store app, you can use a class I wrote called UIDeviceListener
that basically steals the data from the system without relying on any private APIs, but I cannot promise you that Apple will approve it... https://github.com/eldoogy/UIDeviceListener
Regardless of which approach you use, you will end up with a dictionary that contains a key called Temperature
, which is an integer representing battery temperature in Celsius (divide by 100 into a float
to get an accurate value).
Upvotes: 4
Reputation: 29886
The manufacturer information could be hosted on your server and you just detect the device running your app, fetch the battery stats, parse and display.
For the battery temperature, if there are no APIs for it I guess that its an approximation using the current CPU usage and GPU usage or something?
Upvotes: 0