From: liangzhenying <867174124@qq.com> Date: Tue, 19 Mar 2024 08:06:35 +0000 (+0800) Subject: sdk里封装鉴权判断。假如设备还未鉴权,不允许使用SDK X-Git-Tag: 阿里鉴权~13 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=bc03c4bab137a671ff6a06c85fd34afc02e375f7;p=mirrors%2FMouseSdk-MacOS.git sdk里封装鉴权判断。假如设备还未鉴权,不允许使用SDK --- 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 5fb91b9..1313ca9 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" @@ -4,10 +4,11 @@ ## 文档修改记录 -| 版本 | 修改内容 | 修改人 | 修改时间 | -| ----- | ---------------- | ------ | ---------- | -| 1.0.0 | 初始版本 | 梁振营 | 2024-03-8 | -| 1.0.1 | 鼠标按键指令修改 | 梁振营 | 2024-03-18 | +| 版本 | 修改内容 | 修改人 | 修改时间 | +| ----- | --------------------------------------------------- | ------ | ---------- | +| 1.0.0 | 初始版本 | 梁振营 | 2024-03-8 | +| 1.0.1 | 鼠标按键指令修改 | 梁振营 | 2024-03-18 | +| 1.0.2 | 鉴权流程修改,只有鉴权成功了,SDK相关功能才可以使用 | 梁振营 | 2024-03-19 | @@ -31,9 +32,12 @@ -## 蓝牙相关接口 +## SDK相关接口 ```objective-c +/// 单利 ++(AiMouse *)shared; + /// 开始鉴权 - (void)startAuthentication; @@ -46,16 +50,16 @@ - (void)sendAiDataToSdk:(NSData *)data; ``` -## 蓝牙相关代理 +## SDK相关代理 ```objective-c /// 鉴权状态回调 -/// - Parameter isSuccess:是否鉴权成功 -- (void)aiMouseAuthCallBack:(BOOL)isSuccess; +/// - Parameter state:鉴权状态 +- (void)aiMouseAuthStateDidChange:(AiMouseAuthState)state; -/// 鉴权数据回调,需要将此数据转发给蓝牙设备 +/// 鉴权数据回调,需要将此数据通过鉴权特征转发给蓝牙设备 /// - Parameter data: 数据 - (void)aiMouseSendAuthData:(NSData *)data; @@ -99,6 +103,8 @@ if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID]) { self.authCha = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; + //开始鉴权 + [[AiMouse shared] startAuthentication]; } else if ([characteristic.UUID.UUIDString isEqualToString:kAiUUID]) { self.aiCha = characteristic; @@ -132,17 +138,13 @@ ```objective-c /// 鉴权状态回调 - /// - Parameter isSuccess:是否鉴权成功 - -(void)aiMouseAuthCallBack:(BOOL)isSuccess{ - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(isSuccess)]]; - if(self.authCallBack){ - self.authCallBack(isSuccess); - } + /// - Parameter state:鉴权状态 + -(void)aiMouseAuthStateDidChange:(AiMouseAuthState)state{ + [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]]; } /// 鉴权数据回调,需要将此数据转发给蓝牙设备 /// - Parameter data: 数据 - (void)aiMouseSendAuthData:(NSData *)data{ - [self showLog:[NSString stringWithFormat:@"[send] %@",[AiTools dataToHexString:data]]]; [self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse]; } /// 鼠标按键指令回调 @@ -176,25 +178,13 @@ } ``` -- 判断鉴权 - - ``` - [[AiMouse shared] startAuthentication]; - ``` - - 鉴权回调 - ``` - /// - Parameter isSuccess:是否鉴权成功 - - - (void)aiMouseAuthCallBack:(BOOL)isSuccess; - ``` - - - -## 数据交互流程 -鉴权特征数据走鉴权接口,AI特征数据走AI接口数据交互流程 + +## 注意事项 +- 鉴权数据使用的是鉴权特征,走鉴权接口;AI数据使用的是AI特征,走AI接口。 +- 只有鉴权成功了以后,AI相关功能才可用。authState鉴权状态,默认是Unknow。所以必须在连上蓝牙设备找到鉴权特征后,需要主动调用startAuthentication方法去鉴权。(只有鉴权成功,AI键,语音键、M键才可用) +- SDK只封装了蓝牙相关数据处理,APP自己维护蓝牙设备的扫描连接。蓝牙设备一段时间没有任何操作的话,会自动断开连接。APP可以在检测到断开后重连。 diff --git "a/Document/MouseSDK\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213-\350\256\276\345\244\207\351\211\264\346\235\203.eddx" "b/Document/MouseSDK\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213-\350\256\276\345\244\207\351\211\264\346\235\203.eddx" new file mode 100644 index 0000000..6edc513 Binary files /dev/null and "b/Document/MouseSDK\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213-\350\256\276\345\244\207\351\211\264\346\235\203.eddx" differ diff --git "a/Document/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.eddx" "b/Document/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.eddx" index b43ef7c..2825627 100644 Binary files "a/Document/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.eddx" and "b/Document/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.eddx" differ diff --git "a/Document/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.eddx" "b/Document/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.eddx" index dfe3d46..19f0f9e 100644 Binary files "a/Document/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.eddx" and "b/Document/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.eddx" differ diff --git a/MouseSdk/AiMouseSdk/AiMouse.h b/MouseSdk/AiMouseSdk/AiMouse.h index 1871b65..49d6cdf 100644 --- a/MouseSdk/AiMouseSdk/AiMouse.h +++ b/MouseSdk/AiMouseSdk/AiMouse.h @@ -9,13 +9,19 @@ NS_ASSUME_NONNULL_BEGIN +typedef NS_ENUM(NSInteger,AiMouseAuthState) { + AiMouseAuthState_Unknow = 0,///未知 + AiMouseAuthState_Success,///鉴权成功 + AiMouseAuthState_Failure///鉴权失败 +}; + @protocol AiMouseDelegate /// 鉴权状态回调 -/// - Parameter isSuccess:是否鉴权成功 -- (void)aiMouseAuthCallBack:(BOOL)isSuccess; +/// - Parameter state:鉴权状态 +- (void)aiMouseAuthStateDidChange:(AiMouseAuthState)state; -/// 鉴权数据回调,需要将此数据转发给蓝牙设备 +/// 鉴权数据回调,需要将此数据通过鉴权特征转发给蓝牙设备 /// - Parameter data: 数据 - (void)aiMouseSendAuthData:(NSData *)data; @@ -32,6 +38,7 @@ NS_ASSUME_NONNULL_BEGIN @interface AiMouse : NSObject @property (nonatomic, readonly) NSString *version;/// 版本号 @property (nonatomic, weak) id delegate;/// 代理 +@property (nonatomic, readonly) AiMouseAuthState authState;/// 鉴权状态,默认是Unknow。所以必须在连上蓝牙设备找到鉴权特征后,需要主动调用startAuthentication方法去鉴权。(只有鉴权成功,AI键,语音键、M键才可用) /// 单利 +(AiMouse *)shared; diff --git a/MouseSdk/AiMouseSdk/AiMouse.m b/MouseSdk/AiMouseSdk/AiMouse.m index 046e57c..0c08a72 100644 --- a/MouseSdk/AiMouseSdk/AiMouse.m +++ b/MouseSdk/AiMouseSdk/AiMouse.m @@ -27,6 +27,13 @@ return manager; } +-(instancetype)init{ + if (self = [super init]) { + _authState = AiMouseAuthState_Unknow; + } + return self; +} + -(NSString *)version{ return @"1.0.1"; } @@ -52,9 +59,7 @@ [ws sendAuthData:data]; }; _authManager.authCallBack = ^(BOOL isSuccess) { - if (ws.delegate) { - [ws.delegate aiMouseAuthCallBack:isSuccess]; - } + [ws authResult:isSuccess]; }; } return _authManager; @@ -78,6 +83,7 @@ } //开始鉴权 - (void)startAuthentication{ + _authState = AiMouseAuthState_Unknow; [self.authManager login]; } @@ -111,15 +117,24 @@ } - (void)sendRecordData:(NSData *)data{ + if (self.authState != AiMouseAuthState_Success) return; if (self.delegate) { [self.delegate aiMouseSendRecordData:data]; } } - (void)sendCmdData:(NSData *)data{ + if (self.authState != AiMouseAuthState_Success) return; if (self.delegate) { [self.delegate aiMouseSendCmdData:data]; } } + +- (void)authResult:(BOOL)isSuccess{ + _authState = isSuccess ? AiMouseAuthState_Success:AiMouseAuthState_Failure; + if (self.delegate) { + [self.delegate aiMouseAuthStateDidChange:self.authState]; + } +} - (void)showLog:(NSString *)log{ // NSLog(@"[sdk]%@",log); } diff --git a/MouseSdk/AliIotConnectKitSDK/AliIotConnectKit/ThirdLib/IotRtc/libiotRtc.a b/MouseSdk/AliIotConnectKitSDK/AliIotConnectKit/ThirdLib/IotRtc/libiotRtc.a index f23e4e8..4570a5c 100644 Binary files a/MouseSdk/AliIotConnectKitSDK/AliIotConnectKit/ThirdLib/IotRtc/libiotRtc.a and b/MouseSdk/AliIotConnectKitSDK/AliIotConnectKit/ThirdLib/IotRtc/libiotRtc.a differ diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 6f27b06..8c43101 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.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist index a37bea2..072f2d0 100644 --- a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -18,6 +18,53 @@ endingLineNumber = "210" landmarkName = "writeDeviceInfoToBle(deviceSecret:)" landmarkType = "7"> + + + + + + + + diff --git a/MouseSdk/MouseSdk/BleManager.h b/MouseSdk/MouseSdk/BleManager.h index 6e3749e..3967af4 100644 --- a/MouseSdk/MouseSdk/BleManager.h +++ b/MouseSdk/MouseSdk/BleManager.h @@ -30,8 +30,6 @@ NS_ASSUME_NONNULL_BEGIN /// - Parameter peripheral: 蓝牙设备 - (void)connectPeripheral:(CBPeripheral *)peripheral; -/// 开始鉴权 -- (void)startAuthentication; @end NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/BleManager.m b/MouseSdk/MouseSdk/BleManager.m index 68c64bb..7238ff9 100644 --- a/MouseSdk/MouseSdk/BleManager.m +++ b/MouseSdk/MouseSdk/BleManager.m @@ -109,6 +109,8 @@ if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID]) { self.authCha = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; + //开始鉴权 + [[AiMouse shared] startAuthentication]; } else if ([characteristic.UUID.UUIDString isEqualToString:kAiUUID]) { self.aiCha = characteristic; @@ -132,11 +134,11 @@ #pragma mark - AiDelegate /// 鉴权状态回调 -/// - Parameter isSuccess:是否鉴权成功 --(void)aiMouseAuthCallBack:(BOOL)isSuccess{ - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(isSuccess)]]; +/// - Parameter state:鉴权状态 +-(void)aiMouseAuthStateDidChange:(AiMouseAuthState)state{ + [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(state)]]; if(self.authCallBack){ - self.authCallBack(isSuccess); + self.authCallBack(state == AiMouseAuthState_Success); } } /// 鉴权数据回调,需要将此数据转发给蓝牙设备 @@ -178,7 +180,6 @@ #pragma mark - public /// 连接设备 - (void)startScanDevice{ - NSLog(@"banben***** = %@",[[AiMouse shared] version]); [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}]; } @@ -193,11 +194,6 @@ [self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : [NSNumber numberWithBool:YES]}]; } -/// 开始鉴权 -- (void)startAuthentication{ - [[AiMouse shared] startAuthentication]; -} - #pragma mark - other //自动连接,获取已配对的蓝牙,取第一个连接 - (void)autoConnectDevice{ @@ -206,10 +202,10 @@ return; } NSArray *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; - for (CBPeripheral *peripheral in devices) { + if (devices.count > 0) { + CBPeripheral *peripheral = devices[0]; [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",peripheral.name]]; [self connectPeripheral:peripheral]; - break; } // 10s自动重连 [self performSelector:@selector(autoConnectDevice) withObject:nil afterDelay:10]; diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 06e4dd9..58c0008 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -37,7 +37,7 @@ self.title = @"AI SDK"; { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 120, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 160, 100, 45)]; btn.bordered = true; btn.title = @"扫描"; [btn setTarget:self]; @@ -45,15 +45,6 @@ btn.state = 1; [self.view addSubview:btn]; } - { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 200, 100, 45)]; - btn.bordered = true; - btn.title = @"鉴权"; - [btn setTarget:self]; - [btn setAction:@selector(checkAuth)]; - btn.state = 1; - [self.view addSubview:btn]; - } { NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 120, 200, 35)]; label.editable = NO; @@ -67,7 +58,7 @@ NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 200, 200, 35)]; label.editable = NO; label.bordered = NO; - label.stringValue = @"鉴权状态:--"; + label.stringValue = @"鉴权状态:unknow"; label.backgroundColor = NSColor.clearColor; self.autoLabel = label; [self.view addSubview:label]; @@ -87,18 +78,12 @@ } - (void)checkAuth{ - [[BleManager shared] startAuthentication]; +// [[BleManager shared] startAuthentication]; } - (void)showAuthResult:(BOOL)isSuccess{ dispatch_async(dispatch_get_main_queue(), ^{ - self.autoLabel.stringValue = isSuccess?@"鉴权结果:鉴权成功":@"鉴权结果:鉴权失败"; -// NSAlert *alert = [NSAlert new]; -// [alert addButtonWithTitle:@"确定"]; -// [alert setMessageText:isSuccess?@"鉴权成功":@"鉴权失败"]; -// [alert setAlertStyle:NSAlertStyleInformational]; -// [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) { -// }]; + self.autoLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败"; }); } diff --git "a/MouseSdk_Mac_V1.0.1/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.pdf" "b/MouseSdk_Mac_V1.0.1/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.pdf" index d0fea61..1617375 100644 Binary files "a/MouseSdk_Mac_V1.0.1/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.pdf" and "b/MouseSdk_Mac_V1.0.1/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.pdf" differ diff --git "a/MouseSdk_Mac_V1.0.1/MouseSdk\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.pdf" "b/MouseSdk_Mac_V1.0.1/MouseSdk\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.pdf" new file mode 100644 index 0000000..9251b02 Binary files /dev/null and "b/MouseSdk_Mac_V1.0.1/MouseSdk\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.pdf" differ diff --git "a/MouseSdk_Mac_V1.0.1/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.pdf" "b/MouseSdk_Mac_V1.0.1/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.pdf" new file mode 100644 index 0000000..7fdbc4f Binary files /dev/null and "b/MouseSdk_Mac_V1.0.1/MouseSdk\350\277\236\346\216\245\346\265\201\347\250\213.pdf" differ diff --git "a/MouseSdk_Mac_V1.0.1/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.png" "b/MouseSdk_Mac_V1.0.1/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.png" deleted file mode 100644 index ba4b941..0000000 Binary files "a/MouseSdk_Mac_V1.0.1/\346\225\260\346\215\256\344\272\244\344\272\222\346\265\201\347\250\213.png" and /dev/null differ diff --git "a/MouseSdk_Mac_V1.0.1/\350\277\236\346\216\245\346\265\201\347\250\213.png" "b/MouseSdk_Mac_V1.0.1/\350\277\236\346\216\245\346\265\201\347\250\213.png" deleted file mode 100644 index 0020ae6..0000000 Binary files "a/MouseSdk_Mac_V1.0.1/\350\277\236\346\216\245\346\265\201\347\250\213.png" and /dev/null differ