From: liangzhenying <867174124@qq.com> Date: Fri, 31 May 2024 08:12:28 +0000 (+0800) Subject: 鉴权流程优化,隐藏掉鉴权接口 X-Git-Tag: 千寻适配d9~62 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=395b3c5aea237f7607c2eaeb3973e3f94e878fa7;p=mirrors%2FMouseSdk-MacOS.git 鉴权流程优化,隐藏掉鉴权接口 --- diff --git "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" index 2278936..23359c8 100644 --- "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" +++ "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" @@ -16,6 +16,7 @@ | 1.0.13 | 鉴权流程优化 | 梁振营 | 2024-05-20 | | 1.0.17 | 增加命令控制录音功能 | 梁振营 | 2024-05-28 | | 1.0.19 | 新增连接状态回调 | 梁振营 | 2024-05-29 | +| 1.0.21 | 鉴权逻辑优化,隐藏鉴权接口 | 梁振营 | 2024-05-31 | @@ -65,10 +66,6 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide /// - Parameter type: 连接类型 - (void)switchConnect:(AiMouseConnectType)type; -/// 开始鉴权 -/// - Parameter finished: 鉴权结果回调 -- (void)startAuthentication:(void (^)(AiMouseAuthState state))complete; - /// 传输业务数据给sdk /// - Parameter data:ai特征传过来的数据 - (void)sendAiDataToSdk:(NSData *)data; @@ -154,6 +151,10 @@ libz.tbd、libbz2.tbd、libiconv.tbd、liblzma5.tbd、CoreAudio.framework、Vide /// - Parameter connected: 是否连接 - (void)aiMouseConnectStateDidChange:(BOOL)connected; +/// 设备鉴权结果返回 +/// - Parameter authState: 鉴权状态 +- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState; + /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 /// - Parameter data: 数据 - (void)aiMouseSendDeviceInfoData:(NSData *)data; diff --git "a/Document/\345\205\245\351\227\250\346\214\207\345\215\227.pdf" "b/Document/\345\205\245\351\227\250\346\214\207\345\215\227.pdf" new file mode 100644 index 0000000..8e49f3e Binary files /dev/null and "b/Document/\345\205\245\351\227\250\346\214\207\345\215\227.pdf" differ diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h index 9034aa8..e3b8f07 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h @@ -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 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特征传过来的数据 diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m index b638c00..cc79486 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m @@ -64,7 +64,7 @@ } -(NSString *)version{ - return @"1.0.20"; + return @"1.0.21"; } -(AiDeviceInfoManager *)deviceInfoManager{ @@ -149,13 +149,16 @@ //开始鉴权 - (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:@"开始鉴权"]; @@ -331,7 +334,7 @@ - (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]; } @@ -360,6 +363,15 @@ _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; diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 13106f9..06b8c83 100644 Binary files a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MouseSdk/MouseSdk/BleManager.h b/MouseSdk/MouseSdk/BleManager.h index 11ce35b..536301f 100644 --- a/MouseSdk/MouseSdk/BleManager.h +++ b/MouseSdk/MouseSdk/BleManager.h @@ -35,8 +35,6 @@ /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; -/// 鉴权 --(void)startAuthentication; @end diff --git a/MouseSdk/MouseSdk/BleManager.m b/MouseSdk/MouseSdk/BleManager.m index 4503207..a728600 100644 --- a/MouseSdk/MouseSdk/BleManager.m +++ b/MouseSdk/MouseSdk/BleManager.m @@ -119,8 +119,7 @@ [AiMouse shared].delegate = self; //切换当前连接类型 [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth]; - //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 - [self startAuthentication]; + } } } @@ -139,7 +138,14 @@ } #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{ @@ -208,15 +214,7 @@ - (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{ diff --git a/MouseSdk/MouseSdk/DeviceManger.h b/MouseSdk/MouseSdk/DeviceManger.h index ec9f0db..7918f92 100644 --- a/MouseSdk/MouseSdk/DeviceManger.h +++ b/MouseSdk/MouseSdk/DeviceManger.h @@ -24,8 +24,6 @@ NS_ASSUME_NONNULL_BEGIN /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; --(void)startAuthentication; - @end NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/DeviceManger.m b/MouseSdk/MouseSdk/DeviceManger.m index 11ecd45..77f411b 100644 --- a/MouseSdk/MouseSdk/DeviceManger.m +++ b/MouseSdk/MouseSdk/DeviceManger.m @@ -101,11 +101,4 @@ [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; } --(void)startAuthentication{ - if ([AiMouse shared].connectType == AiMouseConnectType_hid) { - [self.hid startAuthentication]; - } else { - [self.ble startAuthentication]; - } -} @end diff --git a/MouseSdk/MouseSdk/HidManager.h b/MouseSdk/MouseSdk/HidManager.h index 92ab919..427f198 100644 --- a/MouseSdk/MouseSdk/HidManager.h +++ b/MouseSdk/MouseSdk/HidManager.h @@ -20,9 +20,6 @@ NS_ASSUME_NONNULL_BEGIN - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; -/// 鉴权 --(void)startAuthentication; - - (void)showLog:(NSString *)log; @end diff --git a/MouseSdk/MouseSdk/HidManager.m b/MouseSdk/MouseSdk/HidManager.m index 5ff36f0..2913e42 100644 --- a/MouseSdk/MouseSdk/HidManager.m +++ b/MouseSdk/MouseSdk/HidManager.m @@ -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{ diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index c4f969b..e8ba4ad 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -34,42 +34,6 @@ - (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; @@ -88,15 +52,6 @@ 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; @@ -141,84 +96,6 @@ [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) { @@ -240,6 +117,9 @@ - (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{ @@ -266,7 +146,4 @@ }; } -- (void)authAction{ - [[DeviceManger shared] startAuthentication]; -} @end