移除hid,只有移除的是当前连接的设备,才会断开连接
authorliangzhenying <867174124@qq.com>
Fri, 23 May 2025 09:02:16 +0000 (17:02 +0800)
committerliangzhenying <867174124@qq.com>
Fri, 23 May 2025 09:02:16 +0000 (17:02 +0800)
MouseSdk/AiMouseSdk/Manager/HidManager.m

index 42d2868..a0237df 100644 (file)
@@ -262,70 +262,63 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
 }
 
 #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;
@@ -338,7 +331,7 @@ void deviceApplier(const void *value, void *context) {
     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;
@@ -561,7 +554,7 @@ static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStr
         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]];
             }
         }
     }