\r
## 文档修改记录\r
\r
-| 版本 | 修改内容 | 修改人 | 修改时间 |\r
-| ----- | --------------------------------------------------- | ------ | ---------- |\r
-| 1.0.0 | 初始版本 | 梁振营 | 2024-03-8 |\r
-| 1.0.1 | 鼠标按键指令修改 | 梁振营 | 2024-03-18 |\r
-| 1.0.2 | 鉴权流程修改,只有鉴权成功了,SDK相关功能才可以使用 | 梁振营 | 2024-03-19 |\r
-| 1.0.3 | 按键指令修改 | 梁振营 | 2024-03-20 |\r
-| 1.0.4 | 支持USB通讯 | 梁振营 | 2024-04-01 |\r
-| 1.0.5 | 支持静态库.a | 梁振营 | 2024-04-22 |\r
-| 1.0.6 | 增加鉴权 | 梁振营 | 2024-04-30 |\r
+| 版本 | 修改内容 | 修改人 | 修改时间 |\r
+| ------ | --------------------------------------------------- | ------ | ---------- |\r
+| 1.0.0 | 初始版本 | 梁振营 | 2024-03-8 |\r
+| 1.0.1 | 鼠标按键指令修改 | 梁振营 | 2024-03-18 |\r
+| 1.0.2 | 鉴权流程修改,只有鉴权成功了,SDK相关功能才可以使用 | 梁振营 | 2024-03-19 |\r
+| 1.0.3 | 按键指令修改 | 梁振营 | 2024-03-20 |\r
+| 1.0.4 | 支持USB通讯 | 梁振营 | 2024-04-01 |\r
+| 1.0.5 | 支持静态库.a | 梁振营 | 2024-04-22 |\r
+| 1.0.6 | 增加鉴权 | 梁振营 | 2024-04-30 |\r
+| 1.0.13 | 鉴权流程优化 | 梁振营 | 2024-05-20 |\r
\r
\r
\r
184A71252BDE2DA60025A332 /* AiSecretModel.m in Sources */ = {isa = PBXBuildFile; fileRef = 184A71232BDE2DA60025A332 /* AiSecretModel.m */; };
189F3B7D2BDE5254007035F9 /* AiAuthManager2.h in Headers */ = {isa = PBXBuildFile; fileRef = 189F3B7B2BDE5254007035F9 /* AiAuthManager2.h */; };
189F3B7E2BDE5254007035F9 /* AiAuthManager2.m in Sources */ = {isa = PBXBuildFile; fileRef = 189F3B7C2BDE5254007035F9 /* AiAuthManager2.m */; };
+ 18AD83252BFAE8B9009BDE3B /* AiDeviceInfoManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 18AD83232BFAE8B9009BDE3B /* AiDeviceInfoManager.h */; };
+ 18AD83262BFAE8B9009BDE3B /* AiDeviceInfoManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AD83242BFAE8B9009BDE3B /* AiDeviceInfoManager.m */; };
+ 18AD83292BFB22DE009BDE3B /* AiSender.h in Headers */ = {isa = PBXBuildFile; fileRef = 18AD83272BFB22DE009BDE3B /* AiSender.h */; };
+ 18AD832A2BFB22DE009BDE3B /* AiSender.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AD83282BFB22DE009BDE3B /* AiSender.m */; };
/* End PBXBuildFile section */
/* Begin PBXFileReference section */
184A71232BDE2DA60025A332 /* AiSecretModel.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiSecretModel.m; sourceTree = "<group>"; };
189F3B7B2BDE5254007035F9 /* AiAuthManager2.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiAuthManager2.h; sourceTree = "<group>"; };
189F3B7C2BDE5254007035F9 /* AiAuthManager2.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiAuthManager2.m; sourceTree = "<group>"; };
+ 18AD83232BFAE8B9009BDE3B /* AiDeviceInfoManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiDeviceInfoManager.h; sourceTree = "<group>"; };
+ 18AD83242BFAE8B9009BDE3B /* AiDeviceInfoManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiDeviceInfoManager.m; sourceTree = "<group>"; };
+ 18AD83272BFB22DE009BDE3B /* AiSender.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiSender.h; sourceTree = "<group>"; };
+ 18AD83282BFB22DE009BDE3B /* AiSender.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiSender.m; sourceTree = "<group>"; };
/* End PBXFileReference section */
/* Begin PBXFrameworksBuildPhase section */
180E7BF62BD8FC4E008558E8 /* ffmpeg */,
180E7AA92BD7BB12008558E8 /* AiMouseSdk.h */,
180E7AAB2BD7BB12008558E8 /* AiMouseSdk.m */,
+ 18AD83232BFAE8B9009BDE3B /* AiDeviceInfoManager.h */,
+ 18AD83242BFAE8B9009BDE3B /* AiDeviceInfoManager.m */,
+ 18AD83272BFB22DE009BDE3B /* AiSender.h */,
+ 18AD83282BFB22DE009BDE3B /* AiSender.m */,
180E7AB42BD7BD7D008558E8 /* AiFrameManager.h */,
180E7B4D2BD7BD81008558E8 /* AiFrameManager.m */,
180E7B472BD7BD7F008558E8 /* AiAuthManager.h */,
184A710E2BDA4C050025A332 /* AFCompatibilityMacros.h in Headers */,
180E7B572BD7BD83008558E8 /* AiAudioConverter.h in Headers */,
180E7CAD2BD8FC4E008558E8 /* attributes.h in Headers */,
+ 18AD83252BFAE8B9009BDE3B /* AiDeviceInfoManager.h in Headers */,
180E7C932BD8FC4E008558E8 /* murmur3.h in Headers */,
180E7CE52BD8FC4E008558E8 /* version.h in Headers */,
180E7CC62BD8FC4E008558E8 /* tea.h in Headers */,
180E7CDA2BD8FC4E008558E8 /* avassert.h in Headers */,
180E7CCA2BD8FC4E008558E8 /* channel_layout.h in Headers */,
180E7CC42BD8FC4E008558E8 /* bswap.h in Headers */,
+ 18AD83292BFB22DE009BDE3B /* AiSender.h in Headers */,
180E7CF02BD8FC4F008558E8 /* xvmc.h in Headers */,
180E7CC22BD8FC4E008558E8 /* aes.h in Headers */,
180E7CAF2BD8FC4E008558E8 /* hwcontext_d3d11va.h in Headers */,
files = (
180E7BE52BD7BD84008558E8 /* AiMouseTools2.m in Sources */,
184A71252BDE2DA60025A332 /* AiSecretModel.m in Sources */,
+ 18AD832A2BFB22DE009BDE3B /* AiSender.m in Sources */,
184A71202BDD11C70025A332 /* AiTrippleModel.m in Sources */,
180E7AAC2BD7BB12008558E8 /* AiMouseSdk.m in Sources */,
180E7BE12BD7BD84008558E8 /* AiAudioConverter.m in Sources */,
180E7BDF2BD7BD84008558E8 /* AiMouseConfig.m in Sources */,
184A710D2BDA4C050025A332 /* AFSecurityPolicy.m in Sources */,
180E7B552BD7BD83008558E8 /* AiAudioDecoder.m in Sources */,
+ 18AD83262BFAE8B9009BDE3B /* AiDeviceInfoManager.m in Sources */,
184A71092BDA4C050025A332 /* AFURLSessionManager.m in Sources */,
180E7BE82BD7BD84008558E8 /* AiAuthManager.m in Sources */,
);
NS_ASSUME_NONNULL_BEGIN
-typedef void(^SetDeviceSecretBlock)(BOOL isSuccess);
+typedef void(^AiCmdFinishBlock)(BOOL isSuccess);
@interface AiAuthManager : NSObject
@property (nonatomic,strong)void (^outputLog)(NSString *log);
-@property (nonatomic,strong)void (^setDeviceSecret)(NSString *value,SetDeviceSecretBlock finished);
+@property (nonatomic,strong)void (^setDeviceSecret)(NSString *value,AiCmdFinishBlock finished);
@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);
- (void)loginWith:(AiMouseDeviceInfo *)info;
NSString *deviceSecret = [dict valueForKeyPath:@"data.deviceSecret"];
if(code.integerValue == 20000 && deviceSecret){
[self sendDeviceSecretCmd:deviceSecret];
- [self showLog:@"激活成功"];
+// [self showLog:@"激活成功"];
}
else{
[self showLog:dict[@"msg"]];
--- /dev/null
+//
+// AiDeviceInfoManager.h
+// AiMouseSdk
+//
+// Created by incar on 2024/5/20.
+//
+
+#import <Foundation/Foundation.h>
+#import "AiMouseSdk.h"
+#import "AiAuthManager.h"
+
+NS_ASSUME_NONNULL_BEGIN
+
+@interface AiDeviceInfoManager : NSObject
+@property (nonatomic,strong)void (^sendToBleBlock)(NSData *data);
+@property (nonatomic,strong)void (^outputLogBlock)(NSString *log);
+
+-(void)receiveData:(NSData *)data;
+
+- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))complete;
+
+/// 获取信号强度
+/// - Parameter completation: 信号强度回调
+- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete;
+
+- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished;
+
+- (void)setDeviceSecret:(NSString *)value completation:(AiCmdFinishBlock)complete;
+
+/// 设置鉴权
+- (void)setAuthStateToDevice:(BOOL)isSuccess;
+@end
+
+NS_ASSUME_NONNULL_END
--- /dev/null
+//
+// AiDeviceInfoManager.m
+// AiMouseSdk
+//
+// Created by incar on 2024/5/20.
+//
+
+#import "AiDeviceInfoManager.h"
+#import "AiMouseTools.h"
+#import "AiMouseTools2.h"
+#import "AiMouseConfig.h"
+#import "AiSender.h"
+
+
+
+@interface AiDeviceInfoManager()<AiSenderDelegate>
+@property (nonatomic, strong) AiSender *sender;
+@property (nonatomic, strong) AiMouseDeviceInfo *deviceInfo;
+@property (nonatomic, strong) void (^deviceInfoBlock)(AiMouseDeviceInfo *deviceInfo,NSError *error);
+@property (nonatomic, strong) void (^setCPIBlock)(BOOL isSuccess,NSError *error);
+@property (nonatomic, strong) void (^getRSSIBlock)(NSString *value,NSError *error);
+@property (nonatomic, strong) AiCmdFinishBlock setDsBlock;
+@property (nonatomic, strong) void (^authBlock)(AiMouseAuthState state);
+@end
+
+
+@implementation AiDeviceInfoManager
+
+-(void)receiveData:(NSData *)data{
+ if(![self.sender isAnswer:data]){
+ return;
+ }
+
+ AiSender *sender = self.sender;
+ [self.sender remove];
+ self.sender = nil;
+
+ NSString *str = [AiMouseTools dataToUtf8String:data];
+ NSString *str2 = [AiMouseTools2 fixDeviceData:str];
+ NSDictionary *dict = [AiMouseTools stringToDictionary:str2];
+// [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
+// [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
+ [self showLog:[NSString stringWithFormat:@"receive %@",str2]];
+ if (dict == nil) {
+ if(data.length > 7 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 7)]] .lowercaseString isEqualToString:@"7b22534e223a22"] ){
+ //SN异常
+ [self showLog:@"SN未设置!!!"];
+ self.deviceInfo.sn = @"";
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_MAC"}];
+ }
+ else if(data.length > 15 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 15)]] .lowercaseString isEqualToString:@"7b2264657669636553656372657422"] ){
+ //deviceSecret异常
+ [self showLog:@"deviceSecret未设置"];
+ [self.deviceInfo setValue:nil forKey:@"deviceSecret"];
+ if(self.deviceInfoBlock){
+ [self showLog:self.deviceInfo.description];
+ self.deviceInfoBlock(self.deviceInfo, nil);
+ self.deviceInfoBlock= nil;
+ }
+ }
+ //鼠标移动数据,不处理
+ return;
+ }
+
+// [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
+// [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
+// [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
+ if(dict[@"set_config"]){
+ if([dict[@"set_config"] intValue] == 1){
+ if ([sender.sendDict objectForKey:@"set_CPI"]) {
+ //设置cpi
+ self.setCPIBlock(true, nil);
+ self.setCPIBlock = nil;
+ return;
+ }
+ if([sender.sendDict objectForKey:@"set_deviceSecret"]){
+ //deviceSecret
+ self.setDsBlock(true);
+ self.setDsBlock = nil;
+ }
+ }
+ else{
+ NSError *err = [NSError errorWithDomain:@"AiMouse SDK Error" code:1001 userInfo:@{@"NSLocalizedDescriptionKey":@"请求超时"}];
+ if ([sender.sendDict objectForKey:@"set_CPI"]) {
+ //设置cpi
+ if(self.setCPIBlock) self.setCPIBlock(false, err);
+ self.setDsBlock = nil;
+ return;
+ }
+ if ([sender.sendDict objectForKey:@"set_deviceSecret"]){
+ //设置deviceSecret
+ if(self.setDsBlock) self.setDsBlock(false);
+ self.setDsBlock = nil;
+ return;
+ }
+ }
+ }
+ else{
+ if (dict[@"battery_power"]) {
+ self.deviceInfo.battery_power = dict[@"battery_power"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_CPI"}];
+ }
+ else if(dict[@"CPI"]) {
+ self.deviceInfo.CPI = dict[@"CPI"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_CPI_list"}];
+ }
+ else if(dict[@"CPI_list"]) {
+ self.deviceInfo.CPI_list = dict[@"CPI_list"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_version"}];
+ }
+ else if(dict[@"version"]) {
+ self.deviceInfo.version = dict[@"version"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_brand"}];
+ }
+ else if(dict[@"brand"]) {
+ self.deviceInfo.brand = dict[@"brand"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_SN"}];
+ }
+ else if(dict[@"SN"]) {
+ self.deviceInfo.sn = dict[@"SN"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_MAC"}];
+ }
+ else if(dict[@"MAC"]) {
+ self.deviceInfo.mac = dict[@"MAC"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_productKey"}];
+ }
+ else if(dict[@"productKey"]) {
+ [self.deviceInfo setValue:dict[@"productKey"] forKey:@"productKey"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_secretKey"}];
+ }
+ else if(dict[@"secretKey"]) {
+ [self.deviceInfo setValue:dict[@"secretKey"] forKey:@"secretKey"];
+ [self sendDeviceInfoCmd:@{@"cmd":@"get_deviceSecret"}];
+ }
+ else if(dict[@"deviceSecret"]) {
+ [self.deviceInfo setValue:dict[@"deviceSecret"] forKey:@"deviceSecret"];
+ if(self.deviceInfoBlock){
+ [self showLog:self.deviceInfo.description];
+ self.deviceInfoBlock(self.deviceInfo, nil);
+ self.deviceInfoBlock= nil;
+ }
+ }
+ else if(dict[@"rssi"]) {
+ if(self.getRSSIBlock){
+ self.getRSSIBlock(dict[@"rssi"],nil);
+ }
+ }
+ }
+}
+
+- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))complete{
+ if(self.sender){
+ complete(nil,[NSError errorWithDomain:@"AiMouse SDK Error" code:1002 userInfo:@{@"NSLocalizedDescriptionKey":@"有命令正在执行中..."}]);
+ [self showLog:@"有命令正在执行中..."];
+ return;
+ }
+
+ self.deviceInfo = [[AiMouseDeviceInfo alloc] init];
+ self.deviceInfoBlock = complete;
+ NSDictionary *data = @{@"cmd":@"get_battery_power"};
+ [self sendDeviceInfoCmd:data];
+
+ // NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_VMouse_SN":[NSString stringWithFormat:@"%@",@"1234567890"]}];
+ // [self sendDeviceInfoData:data];
+}
+
+
+/// 获取信号强度
+/// - Parameter completation: 信号强度回调
+- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete{
+ self.getRSSIBlock = complete;
+ NSDictionary *data = @{@"cmd":@"get_rssi"};
+ [self sendDeviceInfoCmd:data];
+}
+- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{
+ self.setCPIBlock = finished;
+ NSDictionary *data = @{@"cmd":@"set_config",@"set_CPI":[NSString stringWithFormat:@"%ld",value]};
+ [self sendDeviceInfoCmd:data];
+}
+
+- (void)setDeviceSecret:(NSString *)value completation:(AiCmdFinishBlock)complete{
+ self.setDsBlock = complete;
+ NSDictionary *data = @{@"cmd":@"set_config",@"set_deviceSecret":value};
+ [self sendDeviceInfoCmd:data];
+}
+
+-(void)setAuthStateToDevice:(BOOL)isSuccess {
+// NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_activated_dtate":isSuccess?@"1":@"0"}];
+// [self sendDeviceInfoData:data];
+ NSDictionary *data = @{@"cmd":@"set_config",@"set_activated_dtate":isSuccess?@"1":@"0"};
+ [self sendDeviceInfoCmd:data];
+}
+
+#pragma mark - AiSenderDelegate
+- (void)aiSender:(AiSender *)sender reSendCmd:(NSDictionary *)dict{
+ [self sendCmdToBle:dict];
+}
+
+-(void)aiSenderRecieveTimeOut:(AiSender *)sender{
+ [self.sender remove];
+ self.sender = nil;
+
+ [self showLog:@"指令超时..."];
+
+ NSString *cmd = [sender.sendDict objectForKey:@"cmd"];
+ if (![cmd isKindOfClass:[NSString class]] ) {
+ return;
+ }
+
+ NSError *err = [NSError errorWithDomain:@"AiMouse SDK Error" code:1001 userInfo:@{@"NSLocalizedDescriptionKey":@"请求超时"}];
+ if ([sender.sendDict objectForKey:@"set_CPI"]) {
+ //设置cpi
+ if(self.setCPIBlock) self.setCPIBlock(false, err);
+ self.setDsBlock = nil;
+ return;
+ }
+ if ([sender.sendDict objectForKey:@"set_deviceSecret"]){
+ //设置deviceSecret
+ if(self.setDsBlock) self.setDsBlock(false);
+ self.setDsBlock = nil;
+ return;
+ }
+ if ([sender.sendDict objectForKey:@"set_activated_dtate"]){
+ //设置是否已鉴权
+ return;
+ }
+
+ if ([cmd isEqualTo:@"get_rssi"]){
+ if(self.getRSSIBlock) self.getRSSIBlock(nil, err);
+ self.getRSSIBlock = nil;
+ return;
+ }
+
+ if(self.deviceInfoBlock) self.deviceInfoBlock(nil,err);
+ self.deviceInfoBlock = nil;
+}
+
+- (void)aiSender:(AiSender *)sender outputLog:(NSString *)log{
+ [self showLog:log];
+}
+#pragma mark - others
+- (void)sendDeviceInfoCmd:(NSDictionary *)dict{
+ if(self.sender){
+ [self showLog:@"有命令正在执行中.."];
+ return;
+// [self.sender remove];
+// self.sender = nil;
+ }
+ self.sender = [[AiSender alloc] initWithCmd:dict];
+ self.sender.delegate = self;
+
+ [self sendCmdToBle:dict];
+}
+- (void)sendCmdToBle:(NSDictionary *)dict{
+// NSString *str = [AiMouseTools dataToUtf8String:data];
+// NSMutableString *str1 = [NSMutableString stringWithString:str];
+//
+// NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str1];
+// NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
+// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
+// if (self.delegate) {
+// [self.delegate aiMouseSendDeviceInfoData:sendData];
+// }
+
+ NSData *data = [AiMouseTools dictionaryToData:dict];
+ NSString *str = [AiMouseTools dataToUtf8String:data];
+ NSArray *list = [self splitCMD:str];
+
+ if(list.count > 1){
+ {
+ //开始标志
+ NSString *cmd = @"-$$\0";
+ [self sendToBle:cmd];
+ }
+ //指令
+ for (NSInteger i = 0; i < list.count; i++) {
+ NSString *cmd = [NSString stringWithFormat:@"%@\0",list[i]];
+ [self sendToBle:cmd];
+ }
+ {
+ //结束标志
+ NSString *cmd = @"$$-\0";
+ [self sendToBle:cmd];
+ }
+ }
+ else{
+ NSString *cmd = [NSString stringWithFormat:@"-$$%@$$-\n",str];
+ [self sendToBle:cmd];
+ }
+}
+- (void)sendToBle:(NSString *)cmd{
+ NSData *data = [cmd dataUsingEncoding:NSUTF8StringEncoding];
+ [self showLog:[NSString stringWithFormat:@"send %@",cmd]];
+ if (self.sendToBleBlock) {
+ self.sendToBleBlock(data);
+ }
+}
+//切割
+- (NSArray<NSString *> *)splitCMD:(NSString *)cmd{
+ NSMutableArray *list = [[NSMutableArray alloc] init];
+ NSString *str = cmd;
+ NSInteger len = 30;
+ while (str.length > 0) {
+ if (str.length > len) {
+ NSString *sub = [str substringToIndex:len];
+ [list addObject:sub];
+ str = [str substringFromIndex:len];
+ }
+ else{
+ [list addObject:str];
+ break;
+ }
+ }
+ return list;
+}
+
+- (void)showLog:(NSString *)log{
+ self.outputLogBlock(log);
+}
+
+@end
- (void)sendDeviceDataToSdk:(NSData *)data;
/// 获取设备信息
-/// - Parameter completation: 如果获取成功,则deviceInfo有值,否则为空
+/// - Parameter completation: 如果获取成功,则deviceInfo有值,否则为nil
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))complete;
/// 获取信号强度
-/// - Parameter completation: 信号强度回调
-- (void)getRSSI:(void (^)(NSString *value))complete;
+/// - Parameter completation: 信号强度回调,如果获取成功,则value有值,否则为nil
+- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete;
/// 设置CPI
/// - Parameters:
/// - finished: 设置结果回调
- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished;
-- (void)setDS;
+
@end
//
#import "AiMouseSdk.h"
-
+#import "AiDeviceInfoManager.h"
#import "AiFrameManager.h"
#import "AiAuthManager.h"
#import "AiAudioManager.h"
@property (nonatomic, strong) AiFrameManager *frameManager;
@property (nonatomic, strong) AiAuthManager *authManager;
@property (nonatomic, strong) AiAudioManager *audioManager;
-@property (nonatomic, strong) AiMouseDeviceInfo *deviceInfo;
@property (nonatomic, strong) AiRecordManager *recordManager;
-@property (nonatomic, strong) void (^deviceInfoBlock)(AiMouseDeviceInfo *deviceInfo,NSError *error);
-@property (nonatomic, strong) void (^setCPIBlock)(BOOL isSuccess,NSError *error);
-@property (nonatomic, strong) void (^getRSSIBlock)(NSString *value);
-@property (nonatomic, strong) SetDeviceSecretBlock deviceSecretBlock;
@property (nonatomic, strong) void (^authBlock)(AiMouseAuthState state);
-@property (nonatomic, strong) NSString *updateDS;//更新deviceSecret
-@property (nonatomic, strong) NSTimer *dsTimer;
+@property (nonatomic, strong) AiDeviceInfoManager *deviceInfoManager;
+@property (nonatomic, strong) NSString *updateDeviceSecret;//更新的deviceSecret
@end
@implementation AiMouse
}
-(NSString *)version{
- return @"1.0.11";
+ return @"1.0.16";
}
-(AiFrameManager *)frameManager{
if (!_frameManager) {
return _frameManager;
}
+-(AiDeviceInfoManager *)deviceInfoManager{
+ if (!_deviceInfoManager) {
+ _deviceInfoManager = [[AiDeviceInfoManager alloc] init];
+ __weak typeof(self) ws = self;
+ _deviceInfoManager.sendToBleBlock = ^(NSData * _Nonnull data) {
+ if (ws.delegate) {
+ [ws.delegate aiMouseSendDeviceInfoData:data];
+ }
+ };
+ _deviceInfoManager.outputLogBlock = ^(NSString * _Nonnull log) {
+ [ws showLog:log];
+ };
+ }
+ return _deviceInfoManager;;
+}
+
-(AiAuthManager *)authManager{
if (!_authManager) {
_authManager = [[AiAuthManager alloc] init];
_authManager.outputLog = ^(NSString * _Nonnull log) {
[ws showLog:log];
};
- _authManager.setDeviceSecret = ^(NSString * _Nonnull value, SetDeviceSecretBlock _Nonnull finished) {
+ _authManager.setDeviceSecret = ^(NSString * _Nonnull value, AiCmdFinishBlock _Nonnull finished) {
[ws setDeviceSecret:value completation:finished];
};
_authManager.authCallBack = ^(BOOL isSuccess) {
self.authBlock = complete;
__block typeof(self) ws = self;
[self getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
- [ws showLog:@"开始鉴权"];
- [ws.authManager loginWith:deviceInfo];
+ if (error ) {
+ [ws showLog:@"鉴权失败"];
+ ws.authBlock(AiMouseAuthState_Failure);
+ }
+ else{
+ [ws showLog:@"开始鉴权"];
+ [ws.authManager loginWith:deviceInfo];
+ }
}];
}
}
-(void)sendDeviceDataToSdk:(NSData *)data{
- NSString *str = [AiMouseTools dataToUtf8String:data];
- NSString *str2 = [AiMouseTools2 fixDeviceData:str];
- NSDictionary *dict = [AiMouseTools stringToDictionary:str2];
-// [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
- [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
- [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
- if (dict == nil) {
- if(data.length > 7 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 7)]] .lowercaseString isEqualToString:@"7b22534e223a22"] ){
- //SN异常
- [self showLog:@"SN未设置!!!"];
- self.deviceInfo.sn = @"";
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_MAC"}]];
- }
- else if(data.length > 15 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 15)]] .lowercaseString isEqualToString:@"7b2264657669636553656372657422"] ){
- //deviceSecret异常
- [self showLog:@"deviceSecret未设置"];
- self.deviceInfo.deviceSecret = nil;
- if(self.deviceInfoBlock){
- [self showLog:self.deviceInfo.description];
- self.deviceInfoBlock(self.deviceInfo, nil);
- self.deviceInfoBlock= nil;
- }
- }
- //鼠标移动数据,不处理
- return;
- }
-
-// [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
-// [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
-// [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
- if(dict[@"set_config"]){
- if([dict[@"set_config"] intValue] == 1){
- if(self.setCPIBlock){
- self.setCPIBlock(true, nil);
- self.setCPIBlock = nil;
- }
- if(self.deviceSecretBlock){
- self.deviceSecretBlock(true);
- self.deviceSecretBlock = nil;
- [self removeSendTimer];
- }
- }
- else{
- if(self.setCPIBlock){
- self.setCPIBlock(false, [NSError errorWithDomain:@"AiMouse SDK Error" code:-1 userInfo:@{@"NSLocalizedDescriptionKey":@"设置失败"}]);
- self.setCPIBlock = nil;
- }
- if(self.deviceSecretBlock){
- self.deviceSecretBlock(false);
- self.deviceSecretBlock = nil;
- }
- }
- }
- else{
- if (dict[@"battery_power"]) {
- self.deviceInfo.battery_power = dict[@"battery_power"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_CPI"}]];
- }
- else if(dict[@"CPI"]) {
- self.deviceInfo.CPI = dict[@"CPI"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_CPI_list"}]];
- }
- else if(dict[@"CPI_list"]) {
- self.deviceInfo.CPI_list = dict[@"CPI_list"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_version"}]];
- }
- else if(dict[@"version"]) {
- self.deviceInfo.version = dict[@"version"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_brand"}]];
- }
- else if(dict[@"brand"]) {
- self.deviceInfo.brand = dict[@"brand"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_SN"}]];
- }
- else if(dict[@"SN"]) {
- self.deviceInfo.sn = dict[@"SN"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_MAC"}]];
- }
- else if(dict[@"MAC"]) {
- self.deviceInfo.mac = dict[@"MAC"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_productKey"}]];
- }
- else if(dict[@"productKey"]) {
- self.deviceInfo.productKey = dict[@"productKey"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_secretKey"}]];
- }
- else if(dict[@"secretKey"]) {
- self.deviceInfo.secretKey = dict[@"secretKey"];
- [self sendDeviceInfoData:[AiMouseTools dictionaryToData:@{@"cmd":@"get_deviceSecret"}]];
- }
- else if(dict[@"deviceSecret"]) {
- self.deviceInfo.deviceSecret = dict[@"deviceSecret"];
- if(self.deviceInfoBlock){
- [self showLog:self.deviceInfo.description];
- self.deviceInfoBlock(self.deviceInfo, nil);
- self.deviceInfoBlock= nil;
- }
- }
- else if(dict[@"rssi"]) {
- if(self.getRSSIBlock){
- self.getRSSIBlock(dict[@"rssi"]);
- }
- }
- else{
- if(self.deviceInfoBlock){
- [self showLog:self.deviceInfo.description];
- self.deviceInfoBlock(nil, [NSError errorWithDomain:@"AiMouse SDK Error" code:-1 userInfo:@{@"NSLocalizedDescriptionKey":@"获取设备信息失败"}]);
- self.deviceInfoBlock= nil;
- }
- }
- }
+ [self.deviceInfoManager receiveData:data];
}
- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))complete{
- self.deviceInfo = [[AiMouseDeviceInfo alloc] init];
- self.deviceInfoBlock = complete;
- NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"get_battery_power"}];
- [self sendDeviceInfoData:data];
-
-// NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_VMouse_SN":[NSString stringWithFormat:@"%@",@"1234567890"]}];
-// [self sendDeviceInfoData:data];
+ [self.deviceInfoManager getDeviceInfo:complete];
}
/// 获取信号强度
/// - Parameter completation: 信号强度回调
-- (void)getRSSI:(void (^)(NSString *value))complete{
- self.getRSSIBlock = complete;
- NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"get_rssi"}];
- [self sendDeviceInfoData:data];
+- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete{
+ [self.deviceInfoManager getRSSI:complete];
}
- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{
- self.setCPIBlock = finished;
- NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_CPI":[NSString stringWithFormat:@"%ld",value]}];
- [self sendDeviceInfoData:data];
+ [self.deviceInfoManager setCPI:value completation:finished];
}
-- (void)setDeviceSecret:(NSString *)value completation:(SetDeviceSecretBlock)complete{
- self.updateDS = value;
- self.deviceSecretBlock = complete;
- NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_deviceSecret":value}];
- [self sendDeviceInfoData:data];
- [self addSendTimer];
+- (void)setDeviceSecret:(NSString *)value completation:(AiCmdFinishBlock)complete{
+ self.updateDeviceSecret = value;
+
+ __weak typeof(self) ws = self;
+ [self.deviceInfoManager setDeviceSecret:value completation:^(BOOL isSuccess) {
+ if (isSuccess) {
+ [ws showLog:@"写入deviceSecret成功,查询是否获读取成功..."];
+ //更新成功,检查是否读取到
+ [ws.deviceInfoManager getDeviceInfo:^(AiMouseDeviceInfo * _Nonnull deviceInfo, NSError * _Nonnull error) {
+ if ([deviceInfo.deviceSecret isEqualToString:value]) {
+ [ws showLog:@"写入deviceSecret成功,读取成功!"];
+ complete(true);
+ }
+ else{
+ //查不到,继续写入
+ [ws showLog:@"查询不到deviceSecret,继续写入!!!"];
+ [ws setDeviceSecret:ws.updateDeviceSecret completation:complete];
+ }
+ }];
+ }
+ else{
+ [ws showLog:@"写入deviceSecret失败!"];
+ [ws setDeviceSecret:ws.updateDeviceSecret completation:complete];
+ }
+ }];
}
- (void)setDS{
// NSLog(@"写入ds成功========");
}];
}
-- (void)addSendTimer{
- [self removeSendTimer];
- dispatch_async(dispatch_queue_create("DeviceQueue", DISPATCH_QUEUE_SERIAL), ^{
- self.dsTimer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(dsTimerAction) userInfo:nil repeats:true];
- [[NSRunLoop currentRunLoop] addTimer:self.dsTimer forMode:NSRunLoopCommonModes];
- [[NSRunLoop currentRunLoop] run];
- });
-}
-- (void)removeSendTimer{
- if (self.dsTimer && [self.dsTimer isValid]) {
- [self.dsTimer invalidate];
- }
-}
-- (void)dsTimerAction{
- if(self.updateDS && self.deviceSecretBlock){
- [self setDeviceSecret:self.updateDS completation:self.deviceSecretBlock];
- }
- else{
- [self removeSendTimer];
- }
-}
#pragma mark - other
-- (void)sendDeviceInfoData:(NSData *)data{
-// NSString *str = [AiMouseTools dataToUtf8String:data];
-// NSMutableString *str1 = [NSMutableString stringWithString:str];
-//
-// NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str1];
-// NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
-// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
-// if (self.delegate) {
-// [self.delegate aiMouseSendDeviceInfoData:sendData];
-// }
-
-// NSString *str = [AiMouseTools dataToUtf8String:data];
- NSString *str = [AiMouseTools dataToUtf8String:data];
- NSArray *list = [self splitCMD:str];
-
-
- if(list.count > 1){
- {
- NSString *sendStr = @"-$$\0";
- NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
-// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
- if (self.delegate) {
- [self.delegate aiMouseSendDeviceInfoData:sendData];
- }
- }
- for (NSInteger i = 0; i < list.count; i++) {
- NSString *sendStr = [NSString stringWithFormat:@"%@\0",list[i]];
- NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
-// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
- if (self.delegate) {
- [self.delegate aiMouseSendDeviceInfoData:sendData];
- }
- }
- {
- NSString *sendStr = @"$$-\0";
- NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
-// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
- if (self.delegate) {
- [self.delegate aiMouseSendDeviceInfoData:sendData];
- }
- }
- }
- else{
- NSString *sendStr = [NSString stringWithFormat:@"-$$%@$$-\n",str];
- NSData *sendData = [sendStr dataUsingEncoding:NSUTF8StringEncoding];
-// [self showLog:[NSString stringWithFormat:@"发送的设备信息数据 = %@",sendStr]];
- if (self.delegate) {
- [self.delegate aiMouseSendDeviceInfoData:sendData];
- }
- }
-}
-
-- (NSArray<NSString *> *)splitCMD:(NSString *)cmd{
- NSMutableArray *list = [[NSMutableArray alloc] init];
- NSString *str = cmd;
- NSInteger len = 30;
- while (str.length > 0) {
- if (str.length > len) {
- NSString *sub = [str substringToIndex:len];
- [list addObject:sub];
- str = [str substringFromIndex:len];
- }
- else{
- [list addObject:str];
- break;
- }
- }
- return list;
-}
- (void)sendRecordData:(NSData *)data{
if (self.authState != AiMouseAuthState_Success){
_authState = isSuccess ? AiMouseAuthState_Success:AiMouseAuthState_Failure;
[self showLog:isSuccess?@"鉴权成功":@"鉴权失败"];
self.authBlock(_authState);
- [self setAuthStateToDevice:isSuccess];
+
+ [self.deviceInfoManager setAuthStateToDevice:isSuccess];
}
- (void)showLog:(NSString *)log{
if (self.delegate) {
}
#pragma mark - deviceInfo
-- (void)setAuthStateToDevice:(BOOL)isSuccess{
- NSData *data = [AiMouseTools dictionaryToData:@{@"cmd":@"set_config",@"set_activated_dtate":isSuccess?@"1":@"0"}];
- [self sendDeviceInfoData:data];
-}
- (void)startRecord{
// [self showLog:@"开始录音"];
--- /dev/null
+//
+// AiSender.h
+// AiMouseSdk
+//
+// Created by incar on 2024/5/20.
+//
+
+#import <Foundation/Foundation.h>
+
+NS_ASSUME_NONNULL_BEGIN
+@class AiSender;
+@protocol AiSenderDelegate <NSObject>
+
+- (void)aiSender:(AiSender *)sender reSendCmd:(NSDictionary *)dict;
+
+- (void)aiSenderRecieveTimeOut:(AiSender *)sender;
+
+- (void)aiSender:(AiSender *)sender outputLog:(NSString *)log;
+
+@end
+
+@interface AiSender : NSObject
+@property (nonatomic, weak) id<AiSenderDelegate> delegate;/// 代理
+@property (nonatomic, strong) NSDictionary *sendDict;
+
+-(instancetype)initWithCmd:(NSDictionary *)dict;
+
+- (void)remove;
+
+- (BOOL)isAnswer:(NSData *)data;
+
+- (BOOL)isSameData:(NSDictionary *)dict;
+
+@end
+
+NS_ASSUME_NONNULL_END
--- /dev/null
+//
+// AiSender.m
+// AiMouseSdk
+//
+// Created by incar on 2024/5/20.
+//
+
+#import "AiSender.h"
+#import "AiMouseTools.h"
+#import "AiMouseTools2.h"
+@interface AiSender()
+@property (nonatomic, assign) NSInteger number;
+@property (nonatomic, strong) NSTimer *timer;
+@end
+
+@implementation AiSender
+
+-(void)dealloc{
+// [self showLog:@"AiSender销毁"];
+}
+
+-(instancetype)initWithCmd:(NSDictionary *)dict{
+ if (self = [super init]) {
+ self.number = 0;
+ self.sendDict = dict;
+ [self addSendTimer];
+ }
+ return self;
+}
+
+-(void)remove{
+ [self removeSendTimer];
+}
+
+- (BOOL)isAnswer:(NSData *)data{
+ NSString *str = [AiMouseTools dataToUtf8String:data];
+ NSString *str2 = [AiMouseTools2 fixDeviceData:str];
+ NSDictionary *dict = [AiMouseTools stringToDictionary:str2];
+// [self showLog:[NSString stringWithFormat:@"总data = %@",[AiMouseTools dataToHexString:data]]];
+// [self showLog:[NSString stringWithFormat:@"fix前 = %@",str]];
+// [self showLog:[NSString stringWithFormat:@"fix后 = %@",str2]];
+ if (dict == nil) {
+ if(data.length > 7 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 7)]] .lowercaseString isEqualToString:@"7b22534e223a22"] ){
+ //SN异常
+ return true;
+ }
+ else if(data.length > 15 && [[AiMouseTools dataToHexString:[data subdataWithRange:NSMakeRange(0, 15)]] .lowercaseString isEqualToString:@"7b2264657669636553656372657422"] ){
+ //deviceSecret异常
+ return true;
+ }
+ //鼠标移动数据,不处理
+ return false;
+ }
+
+ return true;
+}
+
+
+- (BOOL)isSameData:(NSDictionary *)dict{
+ return [dict isEqual:self.sendDict];
+}
+#pragma mark - timer
+- (void)addSendTimer{
+ [self removeSendTimer];
+// dispatch_async(dispatch_queue_create("DeviceQueue", DISPATCH_QUEUE_SERIAL), ^{
+// self.number = 0;
+// self.timer = [NSTimer timerWithTimeInterval:1 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
+// [[NSRunLoop currentRunLoop] run];
+// });
+ self.timer = [NSTimer timerWithTimeInterval:0.2 target:self selector:@selector(timerAction) userInfo:nil repeats:true];
+ [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
+}
+- (void)removeSendTimer{
+ if (self.timer && [self.timer isValid]) {
+ [self.timer invalidate];
+ }
+}
+
+- (void)timerAction{
+ self.number++;
+ if (self.number == 5) {
+ [self removeSendTimer];
+ if (self.delegate && [self.delegate respondsToSelector:@selector(aiSenderRecieveTimeOut:)]) {
+ [self.delegate aiSenderRecieveTimeOut:self];
+ }
+ return;
+ }
+// [self showLog:[NSString stringWithFormat:@"第 %ld 次超时...",self.number]];
+ if (self.delegate && [self.delegate respondsToSelector:@selector(aiSender:reSendCmd:)]) {
+ [self.delegate aiSender:self reSendCmd:self.sendDict];
+ }
+}
+
+- (void)showLog:(NSString *)log{
+ if (self.delegate && [self.delegate respondsToSelector:@selector(aiSender:outputLog:)]) {
+ [self.delegate aiSender:self outputLog:log];
+ }
+}
+@end
}
/// 输出日志
- (void)aiMouseAddLog:(NSString *)log {
+// [self showLog:log];
}
}
/// 输出日志
- (void)aiMouseAddLog:(NSString *)log {
-
+// [self showLog:log];
}
#pragma mark - public
-(void)startAuthentication{
- (void)setupUI{
self.title = @"AI SDK";
- {
- NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height + 80, 100, 45)];
- btn.bordered = true;
- btn.title = @"设置CPI";
- [btn setTarget:self];
- [btn setAction:@selector(cpiAction)];
- 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 = @"写入deviceScrect";
- [btn setTarget:self];
- [btn setAction:@selector(deviceScrectAction)];
- 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 = @"设置CPI";
+// [btn setTarget:self];
+// [btn setAction:@selector(cpiAction)];
+// 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 = @"写入deviceScrect";
+// [btn setTarget:self];
+// [btn setAction:@selector(deviceScrectAction)];
+// 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;
- (void)cpiAction{
[[AiMouse shared] setCPI:1000 completation:^(BOOL isSuccess, NSError *error) {
- NSLog(@"1");
+ NSLog(@"%@",[NSString stringWithFormat:@"cpi设置结果 = %@",@(isSuccess)]);
}];
}
- (void)deviceScrectAction{
- [[AiMouse shared] setDS];
+// [[AiMouse shared] setDS];
}
/// 获取设备信息
[[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) {
if (deviceInfo) {
NSString *info = deviceInfo.description;
- NSLog(@"%@",info);
-
dispatch_async(dispatch_get_main_queue(), ^{
self.deviceInfoLabel.stringValue = info;
});