/// 设备移除回调
static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) {
[[HidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]];
- if([HidManager shared].deviceInfoCha){
+ if([HidManager shared].deviceInfoCha == inIOHIDDeviceRef){
[HidManager shared].deviceInfoCha = nil;
[HidManager shared].voiceCha = nil;
- [HidManager shared].hidManager = nil;
+// [HidManager shared].hidManager = nil;
[HidManager shared].connectStateCallBack(false) ;
}
}
}
#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;
- 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成功!")];
+
+ 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设备"];
}
- //设置匹配设备
- // ////供应商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);
}
+// 定义C函数作为集合应用函数
+void deviceApplier(const void *value, void *context) {
+ IOHIDDeviceRef inIOHIDDeviceRef = (IOHIDDeviceRef)value;
+
+ if ([DeviceManger shared].isConneted) {
+ // [[HidManager shared] showLog:(@"当前处于连接状态,对监听到有USB 插入 的事件不做处理")];
+ return;
+ }
+ uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey));
+ 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]];
+ if(vid == 3468 && pid == 786){
+ if (page == 65282 && usage == 2) {
+ [HidManager shared].productType = AiMouseProductType_m8;
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+ unsigned char InputBuffer[64];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].connectStateCallBack(true) ;
+ }
+ }
+ // else if (page == 1 && usage == 2){
+ // [HidManager shared].productType = AiMouseProductType_m8;
+ // [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+ // unsigned char InputBuffer[64];
+ // IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
+ //
+ // IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
+ // if (result != kIOReturnSuccess) {
+ // [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
+ // [[AiDeviceAuthManager shared] showHidAlert];
+ // return;
+ // }else{
+ // [[HidManager shared] showLog:@"打开设备成功!"];
+ // // [HidManager shared].connectStateCallBack(true) ;
+ // }
+ // }
+ return;
+ }
+ if((vid == 0x248a && pid == 0x6092)){
+ [HidManager shared].productType = AiMouseProductType_keyboard;
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+ unsigned char InputBuffer[64];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].connectStateCallBack(true) ;
+ }
+ return;
+ }
+ else if((vid == 0x1ea7 && pid == 0x0064)
+ || (vid == 0x1ea7 && pid == 0x0066)
+ || (vid == 0x32c2 && pid == 0x0066)
+ ){
+ [HidManager shared].productType = AiMouseProductType_n;
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+ unsigned char InputBuffer[64];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].connectStateCallBack(true) ;
+ }
+ }
+ else if(vid == 0x10D6 && pid == 0xB008){
+ [HidManager shared].productType = AiMouseProductType_tlw;
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+ unsigned char InputBuffer[64];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [[HidManager shared] showLog:@"打开设备成功!"];
+ [HidManager shared].connectStateCallBack(true) ;
+ }
+ }
+ else if(vid == 0x6675 && pid == 0x8601){
+ [HidManager shared].productType = AiMouseProductType_frk;
+ AiMouse.shared.tlw2 = false;
+ if(page == 1 && usage == 0){
+ unsigned char InputBuffer[128];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
+ IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备信息失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+
+ [[HidManager shared] showLog:@"打开设备信息成功!"];
+ if([HidManager shared].voiceCha != nil){
+ [HidManager shared].connectStateCallBack(true);
+ }
+ }
+ }
+ else if(page == 65283 && usage == 0){
+ unsigned char InputBuffer[128];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备voice失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [HidManager shared].voiceCha = inIOHIDDeviceRef;
+
+ [[HidManager shared] showLog:@"打开设备voice成功!"];
+ if([HidManager shared].deviceInfoCha != nil){
+ [HidManager shared].connectStateCallBack(true);
+ }
+ }
+ }
+ }
+ else if(vid == 0x6675 && pid == 0x8602){
+ [HidManager shared].productType = AiMouseProductType_frk;
+ AiMouse.shared.tlw2 = true;
+ [HidManager shared].tlw2Receiver = [[Tlw2ReceiveManager alloc] init];
+ if(page == 65519 && usage == 0){
+ unsigned char InputBuffer[128];
+ IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL);
+
+ IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone);
+ if (result != kIOReturnSuccess) {
+ [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备voice失败! %d",result]];
+ [[AiDeviceAuthManager shared] showHidAlert];
+ return;
+ }else{
+ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
+
+ [[HidManager shared] showLog:@"打开设备voice成功!"];
+ if([HidManager shared].deviceInfoCha != nil){
+ [HidManager shared].connectStateCallBack(true);
+ }
+ }
+ }
+ }
+}
+
+
+
- (void)close{
if (self.hidManager == nil) {
return;