我对 Mac OS X 操作系统的接触有限,现在我开始使用 Xcode 并正在研究 I/O 套件。我需要在命令行工具下在 Xcode 中创建一个程序,以便列出 Mac 系统中连接的所有 USB 设备。请有过这方面经验的人帮帮我。如果有人可以向我提供示例代码,那么它将很有用,因为我正在寻找起点。
你可以适应USB私有数据样本 http://developer.apple.com/library/mac/samplecode/USBPrivateDataSample/Listings/USBPrivateDataSample_c.html根据您的需要,该示例设置一个通知程序,列出当前连接的设备,然后等待设备连接/分离。如果这样做,您将需要删除usbVendor
and usbProduct
匹配字典,因此所有 USB 设备都会匹配。
或者,您可以使用IOServiceGetMatchingServices https://developer.apple.com/library/mac/documentation/IOKit/Reference/IOKitLib_header_reference/Reference/reference.html#jumpTo_66使用由创建的字典来获取所有当前匹配服务的迭代器IOServiceMatching(kIOUSBDeviceClassName)
.
这是一个简短的示例(我从未运行过):
#include <IOKit/IOKitLib.h>
#include <IOKit/usb/IOUSBLib.h>
int main(int argc, const char *argv[])
{
CFMutableDictionaryRef matchingDict;
io_iterator_t iter;
kern_return_t kr;
io_service_t device;
/* set up a matching dictionary for the class */
matchingDict = IOServiceMatching(kIOUSBDeviceClassName);
if (matchingDict == NULL)
{
return -1; // fail
}
/* Now we have a dictionary, get an iterator.*/
kr = IOServiceGetMatchingServices(kIOMasterPortDefault, matchingDict, &iter);
if (kr != KERN_SUCCESS)
{
return -1;
}
/* iterate */
while ((device = IOIteratorNext(iter)))
{
/* do something with device, eg. check properties */
/* ... */
/* And free the reference taken before continuing to the next item */
IOObjectRelease(device);
}
/* Done, release the iterator */
IOObjectRelease(iter);
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)