泰凌微获取到usb模式dongle连接成功后才会给app回调
authorliangzhenying <867174124@qq.com>
Tue, 3 Sep 2024 08:01:50 +0000 (16:01 +0800)
committerliangzhenying <867174124@qq.com>
Tue, 3 Sep 2024 08:01:50 +0000 (16:01 +0800)
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m
MouseSdk/AiMouseSdk/Manager/HidManager.m
MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.m
MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate

index 1f83a0e..39f9e81 100644 (file)
     }
     self.isAuthing = true;
     __weak typeof(self) ws = self;
-    [self startAuthentication:AiMouseAuthType_none finished:^(AiMouseAuthState state) {
+    [self startAuthentication:AiMouseAuthType_BeiKeZhiHui finished:^(AiMouseAuthState state) {
         ws.isAuthing = false;
         [ws showLog:state == AiMouseAuthState_Success? @"鉴权成功":@"鉴权失败"];
         if(ws.delegate) [ws.delegate aiMouseAuthenticationResult:state];
index 9875582..9251584 100644 (file)
@@ -64,10 +64,7 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
             return;
         }else{
             [[HidManager shared] showLog:@"打开设备成功!"];
-//            [HidManager shared].connectStateCallBack(true) ;
-            dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-                [HidManager shared].connectStateCallBack(true);
-            });
+            [HidManager shared].connectStateCallBack(true) ;
         }
 //        CFRetain([HidManager shared].deviceInfoCha);
 //        IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);
index c7a8526..4e2ee8c 100644 (file)
@@ -29,6 +29,9 @@
 @property (nonatomic, strong) TlwAudioManager *audioManager;
 @property (nonatomic, strong) AiRecordManager *recordManager;
 
+@property (nonatomic,strong)void (^dongleStateCallBack)(BOOL success);
+@property (nonatomic,assign)NSInteger dongleTimes;
+
 @property (nonatomic,strong)void (^openRecordCallBack)(BOOL isSuccess,NSError *error);
 @property (nonatomic,strong)void (^closeRecordCallBaack)(BOOL isSuccess,NSError *error);
 @property (nonatomic,strong)void (^setDpiCallBack)(BOOL isSuccess,NSError *error);
     [mData appendBytes:&tail length:1];
     [self sendToDevice:mData];
 }
-- (void)sendDongleCmd{
-    uint8_t cmd = 0x01;
-    uint8_t len = 0x00;
-    uint8_t tail = 0xEF;
-    NSMutableData *mData = NSMutableData.data;
-    [mData appendData:[self getHidHeader]];
-    [mData appendBytes:&cmd length:1];
-    [mData appendBytes:&len length:1];
-    [mData appendBytes:&tail length:1];
-    [self sendToDevice:mData];
-}
 
 #pragma mark - pub Api
 - (void)setLogLevel:(TlwMouseLogLevel)level{
 }
 
 - (void)switchConnect:(AiMouseConnectType)type isConnected:(BOOL)isConneted{
+    [NSObject cancelPreviousPerformRequestsWithTarget:self];
     if(isConneted ){
         _connected = true;
         _connectType = type;
         [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"];
-        [self TlwConnectManager:nil connectStateDidChange:true];
+        
         if (type == AiMouseConnectType_hid ) {
             [self.recordManager checkPermissions];
-            [self sendOpenCmd];
-            [self sendDongleCmd];
+//            [self sendOpenCmd];
+            [self checkDongleState];
         }
+        else{
+            [self TlwConnectManager:nil connectStateDidChange:true];
+        }
+//        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(4 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
+//            [HidManager shared].connectStateCallBack(true);
+//        });
     }
     else{
         [self stopRecord];
         [mData appendBytes:&value length:1];
         [self sendToDevice:mData];
     }
-    
-    
 }
 - (void)getDeviceInfo:(void (^)(TlwDeviceInfo *deviceInfo,NSError *error))finished{
     if(self.connectType == AiMouseConnectType_hid){
 
 
 - (void)getFullDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished{
-    if (self.getFullInfoCallBack) {
-        return;
-    }
     self.getFullInfoCallBack = finished;
     self.questTimes = 0;
     [self checkFullInfoAction];
 - (void)checkFullInfoAction{
     self.questTimes++;
     if (self.questTimes > 3) {
+        self.getFullInfoCallBack(nil,[NSError errorWithDomain:@"AiMouse SDK Error" code:AiMouseErrorCode_unknown userInfo:@{@"NSLocalizedDescriptionKey":@"请求失败"}]);
         [self switchConnect:AiMouseConnectType_hid isConnected:false ];
         return;
     }
     
     [self performSelector:@selector(checkFullInfoAction) withObject:nil afterDelay:1];
 }
+#pragma mark - check dongle
+- (void)checkDongleState{
+    self.dongleTimes = 0;
+    __weak typeof(self) ws = self;
+    [self checkDongleStateAction:^(BOOL success) {
+        ws.dongleStateCallBack = nil;
+        if (success) {
+            [ws TlwConnectManager:nil connectStateDidChange:true];
+        }
+    }];
+}
+- (void)checkDongleStateAction:(void (^)(BOOL success))finished{
+    self.dongleStateCallBack = finished;
+    [self performSelector:@selector(sendDongleCmd) withObject:nil afterDelay:1];
+}
+- (void)sendDongleCmd{
+    self.dongleTimes++;
+    if ( self.dongleStateCallBack == nil) {
+        return;
+    }
+    if (self.dongleTimes > 2) {
+        [self TlwConnectManager:nil connectStateDidChange:false];
+        return;
+    }
+    uint8_t cmd = 0x0D;
+    uint8_t len = 0x00;
+    uint8_t tail = 0xEF;
+    NSMutableData *mData = NSMutableData.data;
+    [mData appendData:[self getHidHeader]];
+    [mData appendBytes:&cmd length:1];
+    [mData appendBytes:&len length:1];
+    [mData appendBytes:&tail length:1];
+    [self sendToDevice:mData];
+    [self performSelector:@selector(sendDongleCmd) withObject:nil afterDelay:1];
+}
 #pragma mark - ble
 - (void)bleReceiveAiData:(NSData *)data{
     [self.audioManager receiveData:data];
             [[data subdataWithRange:NSMakeRange(9, 1)] getBytes:&state length:1];
             
             [self showLog:[NSString stringWithFormat:@"dongle version = %d,bind = %d",version,state]];
+            if(self.dongleStateCallBack)self.dongleStateCallBack(state == 1);
             break;
         }
         case 0x9f:
     }
 }
 
-#pragma mark - TlwConnectManagerDelegate
+#pragma mark - other
 -(void)TlwConnectManager:(NSString *)manager connectStateDidChange:(BOOL)connected{
     _connected = connected;
     if([self.delegate respondsToSelector:@selector(tlwMouseConnectStateDidChange:)]) [self.delegate tlwMouseConnectStateDidChange:connected];
index 31d80b2..06271af 100644 (file)
Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ