| 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
/// - 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
/// - 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
/// - Parameter connected: 是否连接
- (void)aiMouseConnectStateDidChange:(BOOL)connected;
+/// 鉴权结果返回
+/// - Parameter authState: 鉴权状态
+- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState;
+
/// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备
/// - Parameter data: 数据
- (void)aiMouseSendDeviceInfoData:(NSData *)data;
@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功能才可用)
/// 单利
/// - 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特征传过来的数据
}
-(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;
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
-/// 鉴权
--(void)startAuthentication;
@end
[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{
/// 获取设备信息
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
--(void)startAuthentication;
-
@end
NS_ASSUME_NONNULL_END
[[AiMouse shared] switchConnect:AiMouseConnectType_hid];
}
--(void)startAuthentication{
- if ([AiMouse shared].connectType == AiMouseConnectType_hid) {
- [self.hid startAuthentication];
- } else {
- [self.ble startAuthentication];
- }
-}
@end
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation;
-/// 鉴权
--(void)startAuthentication;
-
- (void)showLog:(NSString *)log;
@end
if(PrimaryUsagePage == 1 && PrimaryUsage == 0){
//设备信息
[HidManager shared].deviceInfoCha = inIOHIDDeviceRef;
- //开始鉴权
- [[HidManager shared] startAuthentication];
+ [[HidManager shared] connectDevice];
}
else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){
//ai
[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(结束录音)
[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{
- (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