我想为Mac编写一个小应用程序,我需要通过蓝牙连接鼠标和键盘的电池百分比。任何人都可以告诉我是否有一些API来做到这一点?
答案 0 :(得分: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.
}
}
}
}
希望有帮助...如果您使用聚光灯找到应用程序IORegistryExplorer,这可以帮助找出哪些其他键可能在字典中使用以查找其他有用信息(如设备的名称或类型) 。)