Access Mouse & Keyboard Battery Status via API
I want to write a little App for Mac and i need the battery percentage of mouse and keyboard connected via Bluetooth. Anyone can t开发者_如何学运维ell me if there are some API to do this?
I know that this is a little late, being over 18 months after you asked the question, but here's a bit of the code I use in Battery Status:
mach_port_t masterPort;
kern_return_t kr;
io_iterator_t ite;
io_object_t obj = 0;
CFMutableDictionaryRef properties;
kr = IOMasterPort(bootstrap_port, &masterPort);
if (kr != KERN_SUCCESS)
printf("IOMasterPort() failed: %x\n", kr);
kr = IORegistryCreateIterator(masterPort,
kIOServicePlane,
true,
&ite);
while ((obj = IOIteratorNext(ite)))
{
kr = IORegistryEntryCreateCFProperties(obj,
&properties,
kCFAllocatorDefault,
kNilOptions);
if ((kr != KERN_SUCCESS) || !properties) {
printf("IORegistryEntryCreateCFProperties error %x\n", kr);
}
else
{
CFNumberRef percent = (CFNumberRef) CFDictionaryGetValue(properties, CFSTR("BatteryPercent"));
if (percent)
{
SInt32 s;
if(!CFNumberGetValue(percent, kCFNumberSInt32Type, &s))
{
printf("***CFNumber overflow***\n");
}
else
{
NSDictionary *deviceProperties = (__bridge NSDictionary *)properties;
//Use the key @"BatteryPercent" in this dictionary to access the battery percent of any bluetooth mouse, keyboard, or trackpad connected.
}
}
}
}
Hope that helps... if you use spotlight to find the app IORegistryExplorer, that can help with figuring out what other keys might be good to use in the dictionary to find other useful information (like the name or type of the device.)
精彩评论