usb模式打开hid逻辑修改
authorliangzhenying <867174124@qq.com>
Thu, 22 May 2025 07:58:01 +0000 (15:58 +0800)
committerliangzhenying <867174124@qq.com>
Thu, 22 May 2025 07:58:01 +0000 (15:58 +0800)
MouseSdk/AiMouseSdk/Manager/DeviceManger.m
MouseSdk/AiMouseSdk/Manager/HidManager.m
MouseSdk/AiMouseSdk/Manager/TimerManager.m

index e060cf2..b31b725 100644 (file)
@@ -19,7 +19,7 @@
 #import "M8MouseSdk.h"
 #import "LogManager.h"
 
-@interface DeviceManger()<TlwMouseDelegate,AiMouseDelegate,NMouseSdkDelegate,KeyboardSdkDelegate>
+@interface DeviceManger()<TlwMouseDelegate,AiMouseDelegate,NMouseSdkDelegate,KeyboardSdkDelegate,M8MouseSdkDelegate>
 //@property (nonatomic, readonly) int appType;//1威鼠不鉴权;2威鼠字节鉴权;3千寻不鉴权;4千寻字节鉴权
 @property (nonatomic,strong) NSMutableData *recordData;//录音的数据
 @property (nonatomic,strong) BleManager *ble;
@@ -51,7 +51,7 @@
 - (instancetype)init{
     self = [super init];
     if (self) {
-        self.version = @"MouseSdk_v2025050601";
+        self.version = @"MouseSdk_v2025052202";
     }
     return self;
 }
index c5f6eff..42d2868 100644 (file)
@@ -205,10 +205,10 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
 /// 设备移除回调
 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) ;
     }
 }
@@ -262,38 +262,236 @@ 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;
-    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;
index 31e7535..556807b 100644 (file)
@@ -32,7 +32,7 @@
 //        [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
 //        [[NSRunLoop currentRunLoop] run];
 //    });
-    self.timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+    self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
 }
 
 - (void)timerAction{