我想为 Mac 写一个小应用程序,我需要通过蓝牙连接的鼠标和键盘的电池百分比。任何人都可以告诉我是否有一些 API 可以做到这一点?
问问题
650 次
1 回答
1
我知道这有点晚了,在你问这个问题之后已经超过 18 个月了,但这里有一些我在电池状态中使用的代码:
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.
}
}
}
}
希望对您有所帮助...如果您使用 Spotlight 来查找应用程序 IORegistryExplorer,这可以帮助您找出在字典中可以使用哪些其他键来查找其他有用信息(例如设备的名称或类型。)
于 2012-02-26T22:06:31.653 回答