【发布时间】:2010-08-23 14:03:35
【问题描述】:
我想为 Mac 编写一个小应用程序,我需要通过蓝牙连接的鼠标和键盘的电池百分比。任何人都可以告诉我是否有一些 API 可以做到这一点?
【问题讨论】:
-
有点远,但你也许可以用 applescript 做到这一点
标签: macos keyboard mouse power-management
我想为 Mac 编写一个小应用程序,我需要通过蓝牙连接的鼠标和键盘的电池百分比。任何人都可以告诉我是否有一些 API 可以做到这一点?
【问题讨论】:
标签: macos keyboard mouse power-management
我知道这有点晚了,在您提出问题后 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,这可以帮助您确定字典中哪些其他键可能适合用于查找其他有用信息(例如设备的名称或类型) .)
【讨论】: