这是我的代码似乎没有检测到任何设备:
#include <CoreFoundation/CoreFoundation.h>#include <IOKit/IOKitlib.h>#include <IOKit/IOMessage.h>#include <IOKit/IOCFPlugIn.h>#include <IOKit/usb/IoUSBlib.h>#define kMyvendorID 1452int List_devices(voID);int List_devices(voID){ cfmutabledictionaryRef matchingDict; io_iterator_t iter; kern_return_t kr; io_service_t device; CFNumberRef numberRef; long usbvendor = kMyvendorID; /* set up a matching dictionary for the class */ matchingDict = IOServiceMatching(kIoUSBDeviceClassname); // Interested in instances of class // IoUSBDevice and its subclasses if (matchingDict == NulL) { fprintf(stderr,"IOServiceMatching returned NulL.\n"); return -1; } // We are interested in all USB devices (as opposed to USB interfaces). The Common Class Specification // tells us that we need to specify the IDvendor,IDProduct,and bcdDevice fIElds,or,if we're not interested // in particular bcdDevices,just the IDvendor and IDProduct. Note that if we were trying to match an // IoUSBInterface,we would need to set more values in the matching dictionary (e.g. IDvendor,// bInterfaceNumber and bConfigurationValue. // Create a CFNumber for the IDvendor and set the value in the dictionary numberRef = CFNumberCreate(kcfAllocatorDefault,kcfNumberSInt32Type,&usbvendor); CFDictionarySetValue(matchingDict,CFSTR(kUSBvendorID),numberRef); CFRelease(numberRef); numberRef = NulL; /* Now we have a dictionary,get an iterator.*/ kr = IOServiceGetMatchingServices(kIOMasterPortDefault,matchingDict,&iter); if (kr != KERN_SUCCESS) return -1; /* iterate */ while ((device = IOIteratorNext(iter))) { io_name_t devicename; CFStringRef devicenameAsCFString; /* do something with device,eg. check propertIEs */ /* ... */ /* And free the reference taken before continuing to the next item */ // Get the USB device's name. kr = IORegistryEntryGetname(device,devicename); if (KERN_SUCCESS != kr) { devicename[0] = 'long vID = 1452; //Apple vendor ID CFNumberRef refvendorID = CFNumberCreate (kcfAllocatorDefault,kcfNumberIntType,&vID); CFDictionarySetValue (matchingDict,CFSTR ("IDvendor"),refvendorID); CFRelease(refvendorID); //all product by same vendor CFDictionarySetValue (matchingDict,CFSTR ("IDProduct"),CFSTR("*"));'; } devicenameAsCFString = CFStringCreateWithCString(kcfAllocatorDefault,devicename,kcfStringEnCodingASCII); // Dump our data to stderr just to see what it looks like. fprintf(stderr,"devicename: "); CFShow(devicenameAsCFString); IOObjectRelease(device); } /* Done,release the iterator */ IOObjectRelease(iter); return 1;}int main(int argc,char* argv[]){ while(1){ List_devices(); sleep(1); } return 0;}
值得注意的是:如果我将一个产品ID添加到matchingDict并插入这样的设备,它将发现设备没有问题(不更改供应商ID).但我无法单独使用供应商ID找到它.
解决方法 要获取属于特定供应商的所有产品的列表,您可以在产品ID字段中使用通配符.示例匹配条件如下: 总结以上是内存溢出为你收集整理的macos – Cocoa:按供应商ID检测USB设备全部内容,希望文章能够帮你解决macos – Cocoa:按供应商ID检测USB设备所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)