}
#pragma mark - public
-//- (void)open{
-// [self close];
-// //初始化
-// self.deviceInfoCha = nil;
-// self.voiceCha = nil;
-// self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
-// IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
-// IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
-// if (ret != kIOReturnSuccess) {
-// [self showLog:(@"打开hid失败!")];
-// }else{
-// [self showLog:(@"打开hid成功!")];
-// }
-// //设置匹配设备
-// // ////供应商id
-// // const long vendorID = 0x6675;
-// // //产品id
-// // const long productID = 0x8601;
-// // //供应商id
-// // const long vendorID = 0x10D6;
-// // //产品id
-// // const long productID = 0xB008;
-// NSMutableDictionary* dict= [NSMutableDictionary dictionary];
-// // [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
-// // [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
-// IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
-// //设置匹配到设备回调
-// IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL);
-// //设置移除设备回调
-// IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
-//}
-
- (void)open{
-// [self close];
+ [self close];
//初始化
self.deviceInfoCha = nil;
self.voiceCha = nil;
-
- if(self.hidManager == nil){
- self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
- IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
- IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
- if (ret != kIOReturnSuccess) {
- [self showLog:(@"打开hid失败!")];
- }else{
- [self showLog:(@"打开hid成功!")];
- }
- NSMutableDictionary* dict= [NSMutableDictionary dictionary];
- IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
- IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
- }
-
- // 在需要使用的地方调用
- CFSetRef devices = IOHIDManagerCopyDevices(self.hidManager);
- if (devices) {
- [self showLog:[NSString stringWithFormat:@"找到 %ld 个HID设备", CFSetGetCount(devices)]];
- // 使用C函数遍历集合
- CFSetApplyFunction(devices, deviceApplier, NULL);
- CFRelease(devices);
- } else {
- [self showLog:@"未找到HID设备"];
+ self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+ IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
+ IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
+ if (ret != kIOReturnSuccess) {
+ [self showLog:(@"打开hid失败!")];
+ }else{
+ [self showLog:(@"打开hid成功!")];
}
+ //设置匹配设备
+ NSMutableDictionary* dict= [NSMutableDictionary dictionary];
+ // [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]];
+ // [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]];
+ IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
+ //设置匹配到设备回调
+ IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL);
+ //设置移除设备回调
+ IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
}
+//- (void)open{
+// if(self.hidManager == nil){
+// self.deviceInfoCha = nil;
+// self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone);
+// IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode);
+// IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L);
+// if (ret != kIOReturnSuccess) {
+// [self showLog:(@"打开hid失败!")];
+// }else{
+// [self showLog:(@"打开hid成功!")];
+// }
+// NSMutableDictionary* dict= [NSMutableDictionary dictionary];
+// IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict);
+// IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL);
+// }
+//
+// if (self.deviceInfoCha != nil) {
+// [HidManager shared].connectStateCallBack(true);
+// return;
+// }
+//
+// // 在需要使用的地方调用
+// CFSetRef devices = IOHIDManagerCopyDevices(self.hidManager);
+// if (devices) {
+// [self showLog:[NSString stringWithFormat:@"找到 %ld 个HID设备", CFSetGetCount(devices)]];
+// // 使用C函数遍历集合
+// CFSetApplyFunction(devices, deviceApplier, NULL);
+// CFRelease(devices);
+// } else {
+// [self showLog:@"未找到HID设备"];
+// }
+//}
+
// 定义C函数作为集合应用函数
void deviceApplier(const void *value, void *context) {
IOHIDDeviceRef inIOHIDDeviceRef = (IOHIDDeviceRef)value;
uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey));
uint32_t page = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey));
uint32_t usage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
- // [[HidManager shared] showLog: [NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef]];
+ [[HidManager shared] showLog: [NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef]];
if(vid == 3468 && pid == 786){
if (page == 65282 && usage == 2) {
[HidManager shared].productType = AiMouseProductType_m8;
if (tCFTypeRef != NULL && (CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef)) ) {
Boolean result = CFNumberGetValue((CFNumberRef) tCFTypeRef, kCFNumberSInt32Type, &outValue);
if (!result) {
- [[HidManager shared] showLog:[NSString stringWithFormat:@"HidManager IOHIDDevGetUInt32Property:get %@ failed.", inKey]];
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"IOHIDDevGetUInt32Property:get %@ failed.", inKey]];
}
}
}