鉴权流程优化,隐藏掉鉴权接口
authorliangzhenying <867174124@qq.com>
Fri, 31 May 2024 08:12:28 +0000 (16:12 +0800)
committerliangzhenying <867174124@qq.com>
Fri, 31 May 2024 08:12:28 +0000 (16:12 +0800)
12 files changed:
Document/AiMouseSdk接口文档.md
Document/入门指南.pdf [new file with mode: 0644]
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h
MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m
MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate
MouseSdk/MouseSdk/BleManager.h
MouseSdk/MouseSdk/BleManager.m
MouseSdk/MouseSdk/DeviceManger.h
MouseSdk/MouseSdk/DeviceManger.m
MouseSdk/MouseSdk/HidManager.h
MouseSdk/MouseSdk/HidManager.m
MouseSdk/MouseSdk/ViewController.m

index 2278936..23359c8 100644 (file)
@@ -16,6 +16,7 @@
 | 1.0.13 | 鉴权流程优化                                        | 梁振营 | 2024-05-20 |\r
 | 1.0.17 | 增加命令控制录音功能                                | 梁振营 | 2024-05-28 |\r
 | 1.0.19 | 新增连接状态回调                                    | 梁振营 | 2024-05-29 |\r
+| 1.0.21 | 鉴权逻辑优化,隐藏鉴权接口                          | 梁振营 | 2024-05-31 |\r
 \r
 \r
 \r
@@ -65,10 +66,6 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide
 /// - Parameter type: 连接类型\r
 - (void)switchConnect:(AiMouseConnectType)type;\r
 \r
-/// 开始鉴权\r
-/// - Parameter finished: 鉴权结果回调\r
-- (void)startAuthentication:(void (^)(AiMouseAuthState state))complete;\r
-\r
 /// 传输业务数据给sdk\r
 /// - Parameter data:ai特征传过来的数据\r
 - (void)sendAiDataToSdk:(NSData *)data;\r
@@ -154,6 +151,10 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide
 /// - Parameter connected: 是否连接\r
 - (void)aiMouseConnectStateDidChange:(BOOL)connected;\r
 \r
+/// 设备鉴权结果返回\r
+/// - Parameter authState: 鉴权状态\r
+- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState;\r
+\r
 /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备\r
 /// - Parameter data: 数据\r
 - (void)aiMouseSendDeviceInfoData:(NSData *)data;\r
diff --git a/Document/入门指南.pdf b/Document/入门指南.pdf
new file mode 100644 (file)
index 0000000..8e49f3e
Binary files /dev/null and "b/Document/\345\205\245\351\227\250\346\214\207\345\215\227.pdf" differ
index 9034aa8..e3b8f07 100644 (file)
@@ -48,6 +48,10 @@ typedef NS_ENUM(NSInteger,AiMouseAuthState) {
 /// - Parameter connected: 是否连接
 - (void)aiMouseConnectStateDidChange:(BOOL)connected;
 
+/// 鉴权结果返回
+/// - Parameter authState: 鉴权状态
+- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState;
+
 /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备
 /// - Parameter data: 数据
 - (void)aiMouseSendDeviceInfoData:(NSData *)data;
@@ -73,8 +77,8 @@ typedef NS_ENUM(NSInteger,AiMouseAuthState) {
 @interface AiMouse : NSObject
 @property (nonatomic, readonly) NSString *version;/// 版本号
 @property (nonatomic, weak) id<AiMouseDelegate> delegate;/// 代理
-@property (nonatomic, readonly) BOOL connected;//是否已连接
 @property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型
+@property (nonatomic, readonly) BOOL connected;//是否已连接
 @property (nonatomic, readonly) AiMouseAuthState authState;/// 鉴权状态,默认是Unknow。所以必须在连上蓝牙设备找到鉴权特征后,需要主动调用startAuthentication方法去鉴权。(只有鉴权成功,AI功能才可用)
 
 /// 单利
@@ -88,9 +92,9 @@ typedef NS_ENUM(NSInteger,AiMouseAuthState) {
 /// - Parameter type: 连接类型
 - (void)switchConnect:(AiMouseConnectType)type;
 
-/// 开始鉴权
-/// - Parameter finished: 鉴权结果回调
-- (void)startAuthentication:(void (^)(AiMouseAuthState state))complete;
+///// 开始鉴权
+///// - Parameter finished: 鉴权结果回调
+//- (void)startAuthentication:(void (^)(AiMouseAuthState state))complete;
 
 /// 传输业务数据给sdk
 /// - Parameter data:ai特征传过来的数据
index b638c00..cc79486 100644 (file)
@@ -64,7 +64,7 @@
 }
 
 -(NSString *)version{
-    return @"1.0.20";
+    return @"1.0.21";
 }
 
 -(AiDeviceInfoManager *)deviceInfoManager{
 //开始鉴权
 - (void)startAuthentication:(void (^)(AiMouseAuthState))complete{
     [self showLog:@"获取设备信息"];
-    _authState = AiMouseAuthState_Unknow;
+//    _authState = AiMouseAuthState_Unknow;
     self.authBlock = complete;
     __block typeof(self) ws = self;
     [self getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
         if (error ) {
-            [ws showLog:@"鉴权失败"];
-            ws.authBlock(AiMouseAuthState_Failure);
+            if(ws.authBlock == AiMouseAuthState_Unknow){
+                [ws showLog:@"鉴权失败"];
+                ws.authBlock(AiMouseAuthState_Failure);
+
+            }
         }
         else{
             [ws showLog:@"开始鉴权"];
 - (void)authResult:(BOOL)isSuccess{
     _authState = isSuccess ? AiMouseAuthState_Success:AiMouseAuthState_Failure;
     [self showLog:isSuccess?@"鉴权成功":@"鉴权失败"];
-    self.authBlock(_authState);
+    if(self.authBlock) self.authBlock(_authState);
     
     [self.deviceInfoManager setAuthStateToDevice:isSuccess];
 }
     _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];
+            }];
+        });
+    }
 }
 -(void)aiConnectManagerReSendData:(AiConnectManager *)manager{
     __weak typeof(self) ws = self;
index 13106f9..06b8c83 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 11ce35b..536301f 100644 (file)
@@ -35,8 +35,6 @@
 /// 获取设备信息
 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
 
-/// 鉴权
--(void)startAuthentication;
 
 @end
 
index 4503207..a728600 100644 (file)
             [AiMouse shared].delegate = self;
             //切换当前连接类型
             [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth];
-            //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用
-            [self startAuthentication];
+
         }
     }
 }
 }
 
 #pragma mark - AiDelegate
-
+/// 设备鉴权结果返回
+/// - Parameter authState: 鉴权状态
+-(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{
+    [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]];
+    if(self.authCallBack){
+        self.authCallBack(authState == AiMouseAuthState_Success);
+    }
+}
 /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备
 /// - Parameter data: 数据
 -(void)aiMouseSendDeviceInfoData:(NSData *)data{
 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{
     [[AiMouse shared] getDeviceInfo:completation];
 }
-/// 鉴权
--(void)startAuthentication{
-    [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) {
-        [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]];
-        if(self.authCallBack){
-            self.authCallBack(state == AiMouseAuthState_Success);
-        }
-    }];
-}
+
 #pragma mark - other
 //自动连接,获取已配对的蓝牙,取第一个连接
 - (void)autoConnectDevice{
index ec9f0db..7918f92 100644 (file)
@@ -24,8 +24,6 @@ NS_ASSUME_NONNULL_BEGIN
 /// 获取设备信息
 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
 
--(void)startAuthentication;
-
 @end
 
 NS_ASSUME_NONNULL_END
index 11ecd45..77f411b 100644 (file)
     [[AiMouse shared] switchConnect:AiMouseConnectType_hid];
 }
 
--(void)startAuthentication{
-    if ([AiMouse shared].connectType == AiMouseConnectType_hid) {
-        [self.hid startAuthentication];
-    } else {
-        [self.ble startAuthentication];
-    }
-}
 @end
index 92ab919..427f198 100644 (file)
@@ -20,9 +20,6 @@ NS_ASSUME_NONNULL_BEGIN
 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
 
 
-/// 鉴权
--(void)startAuthentication;
-
 - (void)showLog:(NSString *)log;
 @end
 
index 5ff36f0..2913e42 100644 (file)
@@ -90,8 +90,7 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH
     if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
         //设备信息
         [HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
-        //开始鉴权
-        [[HidManager shared] startAuthentication];
+        [[HidManager shared] connectDevice];
     }
     else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
         //ai
@@ -139,6 +138,14 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
     [self sendData:data toDevice:self.deviceInfoCha];
 }
 
+/// 设备鉴权结果返回
+/// - Parameter authState: 鉴权状态
+-(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{
+    [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]];
+    if(self.authCallBack){
+        self.authCallBack(authState == AiMouseAuthState_Success);
+    }
+}
 /// 鼠标按键指令回调
 /// - Parameter data: 数据。
 /// 前  第一次单击31(开始录音), 第二次单击32(结束录音) , 按下21(开始录音)抬起22(结束录音)
@@ -174,21 +181,13 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe
     [HidManager shared].connectStateCallBack(connected);
 }
 #pragma mark - public
--(void)startAuthentication{
+-(void)connectDevice{
     //连上设备
 //    [HidManager shared].connectStateCallBack(true);
     //设置代理
     [AiMouse shared].delegate = self;
     //切换当前连接类型
     [[AiMouse shared] switchConnect:AiMouseConnectType_hid];
-    //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用
-    [self showLog:@"开始鉴权"];
-    [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) {
-        [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]];
-        if(self.authCallBack){
-            self.authCallBack(state == AiMouseAuthState_Success);
-        }
-    }];
 }
 /// 获取设备信息
 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{
index c4f969b..e8ba4ad 100644 (file)
 
 - (void)setupUI{
     self.title = @"AI SDK";
-    {
-        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height + 240, 100, 45)];
-        btn.bordered = true;
-        btn.title = @"openAction";
-        [btn setTarget:self];
-        [btn setAction:@selector(openAction)];
-        btn.state = 1;
-        [self.view addSubview:btn];
-    }
-    {
-        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height + 160, 100, 45)];
-        btn.bordered = true;
-        btn.title = @"closeAction";
-        [btn setTarget:self];
-        [btn setAction:@selector(closeAction)];
-        btn.state = 1;
-        [self.view addSubview:btn];
-    }
-    {
-        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height + 80, 100, 45)];
-        btn.bordered = true;
-        btn.title = @"set";
-        [btn setTarget:self];
-        [btn setAction:@selector(setAction)];
-        btn.state = 1;
-        [self.view addSubview:btn];
-    }
-    {
-        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height, 100, 45)];
-        btn.bordered = true;
-        btn.title = @"get";
-        [btn setTarget:self];
-        [btn setAction:@selector(getAction)];
-        btn.state = 1;
-        [self.view addSubview:btn];
-    }
     {
         NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 80, 100, 45)];
         btn.bordered = true;
         btn.state = 1;
         [self.view addSubview:btn];
     }
-    {
-        NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 250, 100, 45)];
-        btn.bordered = true;
-        btn.title = @"鉴权";
-        [btn setTarget:self];
-        [btn setAction:@selector(authAction)];
-        btn.state = 1;
-        [self.view addSubview:btn];
-    }
     {
         NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 80, 300, 35)];
         label.editable = NO;
     [self presentViewControllerAsModalWindow:vc];
 }
 
-- ( void)openAction{
-    [[AiMouse shared] openRecord:^(BOOL isSuccess, NSError *error) {
-        if(error == nil){
-            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %@",@(isSuccess)]);
-        }
-        else{
-            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-        }
-    }];
-}
-- ( void)closeAction{
-    [[AiMouse shared] closeRecord:^(BOOL isSuccess, NSError *error) {
-        if(error == nil){
-            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %@",@(isSuccess)]);
-        }
-        else{
-            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-        }
-    }];
-}
-- (void)setAction{
-    [[AiMouse shared] setRecordMode:0 completation:^(BOOL isSuccess, NSError *error) {
-        NSLog(@"%@",[NSString stringWithFormat:@"设置结果 = %@",@(isSuccess)]);
-    }];
-    
-    
-//    [[AiMouse shared] setUrl:@"https://www.baidu.com" completation:^(BOOL isSuccess, NSError *error) {
-//        NSLog(@"%@",[NSString stringWithFormat:@"设置结果 = %@",@(isSuccess)]);
-//    }];
-    
-//    [[AiMouse shared] setUrlState:1 completation:^(BOOL isSuccess, NSError *error) {
-//        NSLog(@"%@",[NSString stringWithFormat:@"设置结果 = %@",@(isSuccess)]);
-//    }];
-//    
-//    [[AiMouse shared] setMouseMode:0 completation:^(BOOL isSuccess, NSError *error) {
-//        NSLog(@"%@",[NSString stringWithFormat:@"设置结果 = %@",@(isSuccess)]);
-//    }];
-}
-
-- (void)getAction{
-    [[AiMouse shared] getRecordMode:^(int mode, NSError *error) {
-        if(error == nil){
-            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %@",@(mode)]);
-        }
-        else{
-            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-        }
-    }];
-    
-    
-//    [[AiMouse shared] getUrl:^(NSString *url, NSError *error) {
-//        if(error == nil){
-//            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %@",url]);
-//        }
-//        else{
-//            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-//        }
-//    }];
-    
-//    [[AiMouse shared] getUrlState:^(int state, NSError *error) {
-//        if(error == nil){
-//            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %d",state]);
-//        }
-//        else{
-//            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-//        }
-//    }];
-    
-//    [[AiMouse shared] getMouseModel:^(int state, NSError *error) {
-//        if(error == nil){
-//            NSLog(@"%@",[NSString stringWithFormat:@"获取结果 = %d",state]);
-//        }
-//        else{
-//            NSLog(@"%@",[NSString stringWithFormat:@"错误 = %@",error.description]);
-//        }
-//    }];
-}
-
 /// 获取设备信息
 - (void)geviceInfoAction{
     [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
 - (void)showAuthResult:(BOOL)isSuccess{
     dispatch_async(dispatch_get_main_queue(), ^{
         self.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败";
+        if(isSuccess){
+            [self geviceInfoAction];
+        }
     });
 }
 - (void)connectStateDidChange:(AiMouseConnectType)type connectState:(BOOL)isConnected{
     };
 }
 
-- (void)authAction{
-    [[DeviceManger shared] startAuthentication];
-}
 @end