鉴权实际改成只要收到数据,没有鉴权成功的,都会去请求鉴权
authorliangzhenying <867174124@qq.com>
Wed, 5 Jun 2024 06:35:57 +0000 (14:35 +0800)
committerliangzhenying <867174124@qq.com>
Wed, 5 Jun 2024 06:35:57 +0000 (14:35 +0800)
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m
MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MouseSdk/MouseSdk/BleManager.m
MouseSdk/MouseSdk/HidManager.m

index e3b8f07..ce5bbd0 100644 (file)
@@ -88,6 +88,10 @@ typedef NS_ENUM(NSInteger,AiMouseAuthState) {
 /// - Parameter level: 等级
 - (void)setLogLevel:(AiMouseLogLevel)level;
 
+
+/// 断开连接
+- (void)didDisconnect;
+
 /// 切换连接类型
 /// - Parameter type: 连接类型
 - (void)switchConnect:(AiMouseConnectType)type;
index a501d16..1785a8a 100644 (file)
@@ -39,7 +39,7 @@
 @property (nonatomic, strong) NSString *updateDeviceSecret;//更新的deviceSecret
 
 @property (nonatomic, strong) AiConnectManager *connectManager;
-
+@property (nonatomic, assign) BOOL isAuthing;
 @end
 
 @implementation AiMouse
@@ -64,7 +64,7 @@
 }
 
 -(NSString *)version{
-    return @"1.0.23";
+    return @"1.0.27";
 }
 
 -(AiDeviceInfoManager *)deviceInfoManager{
 - (void)setLogLevel:(AiMouseLogLevel)level{
     _logLevel = level;
 }
+- (void)didDisconnect{
+    [self showLog:@"系统断开连接"];
+    [self.connectManager disConnected];
+}
 - (void)switchConnect:(AiMouseConnectType)type{
     if(type != _connectType && self.connected){
         [self.connectManager disConnected];
 - (void)startAuthentication:(void (^)(AiMouseAuthState))complete{
 //    [self showLog:@"获取设备信息"];
 //    _authState = AiMouseAuthState_Unknow;
+    [self showLog:@"开始鉴权"];
     self.authBlock = complete;
     __block typeof(self) ws = self;
     [self getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
         if (error ) {
-            if(ws.authBlock == AiMouseAuthState_Unknow){
-                [ws showLog:@"鉴权失败"];
-                ws.authBlock(AiMouseAuthState_Failure);
-
-            }
+            ws.authBlock(AiMouseAuthState_Failure);
         }
         else{
-            [ws showLog:@"开始鉴权"];
             [ws.authManager loginWith:deviceInfo];
         }
     }];
     [self.connectManager resetTimeOut];
     
     [self showLog:[NSString stringWithFormat:@"[receive][Ai] %@",data]];
+    
+    if (self.authState != AiMouseAuthState_Success){
+        [self showLog:@"设备还未鉴权!!!"];
+        return;
+    }
     if(self.connectType == AiMouseConnectType_hid){
 //        0AFFFF12BC32
         if(data.length == 6){
     [self.connectManager resetTimeOut];
     [self showLog:[NSString stringWithFormat:@"[receive][device] %@",data]];
     [self.deviceInfoManager receiveData:data];
+    [self checkAuthState];
 }
 - (void)setDeviceSecret:(NSString *)value completation:(AiCmdFinishBlock)complete{
     self.updateDeviceSecret = value;
 }
 #pragma mark - other
 - (void)sendRecordData:(NSData *)data{
-    if (self.authState != AiMouseAuthState_Success){
-        [self showLog:@"设备还未鉴权!!!"];
-        return;
-    };
+//    if (self.authState != AiMouseAuthState_Success){
+//        [self showLog:@"设备还未鉴权!!!"];
+//        return;
+//    };
     [self showLog:[NSString stringWithFormat:@"[audio] %@",data]];
     if (self.delegate)  [self.delegate aiMouseSendRecordData:data];
 }
 - (void)sendCmdData:(NSData *)data{
-    if (self.authState != AiMouseAuthState_Success){
-        [self showLog:@"设备还未鉴权!!!"];
-        return;
-    }
+//    if (self.authState != AiMouseAuthState_Success){
+//        [self showLog:@"设备还未鉴权!!!"];
+//        return;
+//    }
     [self showLog:[NSString stringWithFormat:@"[send] %@",data]];
     if (self.delegate) [self.delegate aiMouseSendCmdData:data];
 }
 
 - (void)authResult:(BOOL)isSuccess{
     _authState = isSuccess ? AiMouseAuthState_Success:AiMouseAuthState_Failure;
-    [self showLog:isSuccess?@"鉴权成功":@"鉴权失败"];
     [self.deviceInfoManager setAuthStateToDevice:isSuccess];
     
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
     }
 }
 
+- (void)checkAuthState{
+    if (self.authState != AiMouseAuthState_Success) {
+        if (self.isAuthing) {
+            return;
+        }
+        self.isAuthing = true;
+        __weak typeof(self) ws = self;
+        [self startAuthentication:^(AiMouseAuthState state) {
+            ws.isAuthing = false;
+            [ws showLog:state == AiMouseAuthState_Success? @"鉴权成功":@"鉴权失败"];
+            if(ws.delegate) [ws.delegate aiMouseAuthenticationResult:state];
+        }];
+    }
+}
 #pragma mark - deviceInfo
 - (void)startRecord{
 //    [self showLog:@"开始录音"];
 
 #pragma mark - AiConnectManagerDelegate
 -(void)aiConnectManager:(AiConnectManager *)manager connectStateDidChange:(BOOL)connected{
+    if (connected == false) {
+        _authState = AiMouseAuthState_Unknow;
+    }
     _connected = connected;
     if([self.delegate respondsToSelector:@selector(aiMouseConnectStateDidChange:)]) [self.delegate aiMouseConnectStateDidChange:connected];
 //    [self showLog:[NSString stringWithFormat:@"状态改变 = %@",@(connected)]];
     
-    if (connected) {
-        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
-            __weak typeof(self) ws = self;
-            [self startAuthentication:^(AiMouseAuthState state) {
-                if(ws.delegate) [ws.delegate aiMouseAuthenticationResult:state];
-            }];
-        });
-    }
+//    [self checkAuthState];
 }
 -(void)aiConnectManagerReSendData:(AiConnectManager *)manager{
     __weak typeof(self) ws = self;
index b45de7d..22813d7 100644 (file)
Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ
index a728600..d63c66e 100644 (file)
@@ -90,7 +90,8 @@
     if(self.didDisConnectDevice){
         self.didDisConnectDevice(peripheral);
     }
-//    [self autoConnectDevice];
+//    [self aiMouseConnectStateDidChange:false];
+    [[AiMouse shared] didDisconnect];
 }
 - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{
     [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]];
index 2913e42..80f344f 100644 (file)
@@ -108,7 +108,7 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,
     uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey));
 
     if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
-        [HidManager shared].connectStateCallBack(false);
+        [[AiMouse shared] didDisconnect];
     }
 }
 
@@ -135,7 +135,10 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
 /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备
 /// - Parameter data: 数据
 -(void)aiMouseSendDeviceInfoData:(NSData *)data{
-    [self sendData:data toDevice:self.deviceInfoCha];
+    if([AiMouse shared].connected){
+        [self sendData:data toDevice:self.deviceInfoCha];
+    }
+   
 }
 
 /// 设备鉴权结果返回