@property (nonatomic, strong) NSString *updateDeviceSecret;//更新的deviceSecret
@property (nonatomic, strong) AiConnectManager *connectManager;
-
+@property (nonatomic, assign) BOOL isAuthing;
@end
@implementation AiMouse
}
-(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;