背景:项目需要在MAC上通过USB传输信息,进行交互。
实现:
1、导入依赖头文件
#include <IOKit/hid/IOHIDLib.h>
2、初始化IOHIDManager
IOHIDManagerRef managerRef = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
3、进行配对设置,可以过滤其他USB设备。
1)无配对设备
IOHIDManagerSetDeviceMatching(HIDManager, NULL);
2)单类设备配对
NSMutableDictionary* dict= [NSMutableDictionary dictionary];
[dict setValue:pid forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
[dict setValue:vid forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
IOHIDManagerSetDeviceMatching(managerRef, (__bridge CFMutableDictionaryRef)dict);
3)多种设备配对设置
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:dict];
IOHIDManagerSetDeviceMatchingMultiple(managerRef, (__bridge CFMutableArrayRef)arr);
实现:
1、导入依赖头文件
#include <IOKit/hid/IOHIDLib.h>
2、初始化IOHIDManager
IOHIDManagerRef managerRef = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
3、进行配对设置,可以过滤其他USB设备。
1)无配对设备
IOHIDManagerSetDeviceMatching(HIDManager, NULL);
2)单类设备配对
NSMutableDictionary* dict= [NSMutableDictionary dictionary];
[dict setValue:pid forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
[dict setValue:vid forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
IOHIDManagerSetDeviceMatching(managerRef, (__bridge CFMutableDictionaryRef)dict);
3)多种设备配对设置
NSMutableArray *arr = [NSMutableArray array];
[arr addObject:dict];
IOHIDManagerSetDeviceMatchingMultiple(managerRef, (__bridge CFMutableArrayRef)arr);