From: liangzhenying <867174124@qq.com> Date: Sun, 1 Sep 2024 03:55:11 +0000 (+0800) Subject: 泰凌微查询到tongle连接后才能通讯 X-Git-Tag: 千寻适配d9~21 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=703ad5db3216d8670e87ff314a10987123801515;p=mirrors%2FMouseSdk-MacOS.git 泰凌微查询到tongle连接后才能通讯 --- diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk.xcodeproj/project.pbxproj b/MouseSdk/AiMouseSdk/AiMouseSdk.xcodeproj/project.pbxproj index 80bc4e4..6ef92f2 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/AiMouseSdk/AiMouseSdk.xcodeproj/project.pbxproj @@ -350,7 +350,6 @@ 18862BA72C806BD900D3564D /* hwcontext_qsv.h in Headers */ = {isa = PBXBuildFile; fileRef = 180E7C4B2BD8FC4E008558E8 /* hwcontext_qsv.h */; }; 18862BA82C806BD900D3564D /* aes_ctr.h in Headers */ = {isa = PBXBuildFile; fileRef = 180E7C4D2BD8FC4E008558E8 /* aes_ctr.h */; }; 18862BA92C806BD900D3564D /* vorbis_parser.h in Headers */ = {isa = PBXBuildFile; fileRef = 180E7C762BD8FC4E008558E8 /* vorbis_parser.h */; }; - 18862BAA2C806BD900D3564D /* TlwRecordManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AF12C7603E800AC5223 /* TlwRecordManager.h */; }; 18862BAB2C806BD900D3564D /* avcodec.h in Headers */ = {isa = PBXBuildFile; fileRef = 180E7C622BD8FC4E008558E8 /* avcodec.h */; }; 18862BAC2C806BD900D3564D /* pixdesc.h in Headers */ = {isa = PBXBuildFile; fileRef = 180E7C0C2BD8FC4E008558E8 /* pixdesc.h */; }; 18862BAE2C806BD900D3564D /* AiMouseAuth.m in Sources */ = {isa = PBXBuildFile; fileRef = 18FCA3962C7F2AD100C44A37 /* AiMouseAuth.m */; }; @@ -369,7 +368,6 @@ 18862BBC2C806BD900D3564D /* AFNetworkReachabilityManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 184A70FE2BDA4C040025A332 /* AFNetworkReachabilityManager.m */; }; 18862BBD2C806BD900D3564D /* AiRecordManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 180E7B4E2BD7BD81008558E8 /* AiRecordManager.m */; }; 18862BBE2C806BD900D3564D /* TlwAudioManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AF52C7603E800AC5223 /* TlwAudioManager.m */; }; - 18862BBF2C806BD900D3564D /* TlwRecordManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AE82C7603E800AC5223 /* TlwRecordManager.m */; }; 18862BC02C806BD900D3564D /* AiFrameManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 180E7B4D2BD7BD81008558E8 /* AiFrameManager.m */; }; 18862BC12C806BD900D3564D /* AiAudioManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 180E7B512BD7BD82008558E8 /* AiAudioManager.m */; }; 18862BC22C806BD900D3564D /* AiConnectManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 189C15852C05726600676A80 /* AiConnectManager.m */; }; @@ -405,13 +403,11 @@ 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 */; }; - 18EF7AF72C7603E800AC5223 /* TlwRecordManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AE82C7603E800AC5223 /* TlwRecordManager.m */; }; 18EF7AF92C7603E800AC5223 /* adpcm.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AEA2C7603E800AC5223 /* adpcm.h */; }; 18EF7AFA2C7603E800AC5223 /* TlwMouseSdk.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AEB2C7603E800AC5223 /* TlwMouseSdk.m */; }; 18EF7AFB2C7603E800AC5223 /* TlwTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AEC2C7603E800AC5223 /* TlwTools.m */; }; 18EF7AFC2C7603E800AC5223 /* TlwConnectManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AED2C7603E800AC5223 /* TlwConnectManager.m */; }; 18EF7AFD2C7603E800AC5223 /* TlwAudioManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AEE2C7603E800AC5223 /* TlwAudioManager.h */; }; - 18EF7B002C7603E800AC5223 /* TlwRecordManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AF12C7603E800AC5223 /* TlwRecordManager.h */; }; 18EF7B012C7603E800AC5223 /* adpcm.c in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7AF22C7603E800AC5223 /* adpcm.c */; }; 18EF7B022C7603E800AC5223 /* TlwTools.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AF32C7603E800AC5223 /* TlwTools.h */; }; 18EF7B032C7603E800AC5223 /* TlwMouseSdk.h in Headers */ = {isa = PBXBuildFile; fileRef = 18EF7AF42C7603E800AC5223 /* TlwMouseSdk.h */; settings = {ATTRIBUTES = (Public, ); }; }; @@ -616,13 +612,11 @@ 18AD83242BFAE8B9009BDE3B /* AiDeviceInfoManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiDeviceInfoManager.m; sourceTree = ""; }; 18AD83272BFB22DE009BDE3B /* AiSender.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiSender.h; sourceTree = ""; }; 18AD83282BFB22DE009BDE3B /* AiSender.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiSender.m; sourceTree = ""; }; - 18EF7AE82C7603E800AC5223 /* TlwRecordManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TlwRecordManager.m; sourceTree = ""; }; 18EF7AEA2C7603E800AC5223 /* adpcm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = adpcm.h; sourceTree = ""; }; 18EF7AEB2C7603E800AC5223 /* TlwMouseSdk.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TlwMouseSdk.m; sourceTree = ""; }; 18EF7AEC2C7603E800AC5223 /* TlwTools.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TlwTools.m; sourceTree = ""; }; 18EF7AED2C7603E800AC5223 /* TlwConnectManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = TlwConnectManager.m; sourceTree = ""; }; 18EF7AEE2C7603E800AC5223 /* TlwAudioManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TlwAudioManager.h; sourceTree = ""; }; - 18EF7AF12C7603E800AC5223 /* TlwRecordManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TlwRecordManager.h; sourceTree = ""; }; 18EF7AF22C7603E800AC5223 /* adpcm.c */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.c; path = adpcm.c; sourceTree = ""; }; 18EF7AF32C7603E800AC5223 /* TlwTools.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TlwTools.h; sourceTree = ""; }; 18EF7AF42C7603E800AC5223 /* TlwMouseSdk.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = TlwMouseSdk.h; sourceTree = ""; }; @@ -1004,8 +998,6 @@ children = ( 18EF7AF42C7603E800AC5223 /* TlwMouseSdk.h */, 18EF7AEB2C7603E800AC5223 /* TlwMouseSdk.m */, - 18EF7AF12C7603E800AC5223 /* TlwRecordManager.h */, - 18EF7AE82C7603E800AC5223 /* TlwRecordManager.m */, 18EF7AF32C7603E800AC5223 /* TlwTools.h */, 18EF7AEC2C7603E800AC5223 /* TlwTools.m */, 18EF7AF62C7603E800AC5223 /* TlwConnectManager.h */, @@ -1182,7 +1174,6 @@ 180E7CD52BD8FC4E008558E8 /* hwcontext_qsv.h in Headers */, 180E7CD72BD8FC4E008558E8 /* aes_ctr.h in Headers */, 180E7CFC2BD8FC4F008558E8 /* vorbis_parser.h in Headers */, - 18EF7B002C7603E800AC5223 /* TlwRecordManager.h in Headers */, 180E7CE82BD8FC4E008558E8 /* avcodec.h in Headers */, 180E7C962BD8FC4E008558E8 /* pixdesc.h in Headers */, ); @@ -1349,7 +1340,6 @@ 18862BA72C806BD900D3564D /* hwcontext_qsv.h in Headers */, 18862BA82C806BD900D3564D /* aes_ctr.h in Headers */, 18862BA92C806BD900D3564D /* vorbis_parser.h in Headers */, - 18862BAA2C806BD900D3564D /* TlwRecordManager.h in Headers */, 18862BAB2C806BD900D3564D /* avcodec.h in Headers */, 18862BAC2C806BD900D3564D /* pixdesc.h in Headers */, ); @@ -1446,7 +1436,6 @@ 184A710C2BDA4C050025A332 /* AFNetworkReachabilityManager.m in Sources */, 180E7BE42BD7BD84008558E8 /* AiRecordManager.m in Sources */, 18EF7B042C7603E800AC5223 /* TlwAudioManager.m in Sources */, - 18EF7AF72C7603E800AC5223 /* TlwRecordManager.m in Sources */, 180E7BE32BD7BD84008558E8 /* AiFrameManager.m in Sources */, 180E7BE72BD7BD84008558E8 /* AiAudioManager.m in Sources */, 189C15872C05726600676A80 /* AiConnectManager.m in Sources */, @@ -1486,7 +1475,6 @@ 18862BBC2C806BD900D3564D /* AFNetworkReachabilityManager.m in Sources */, 18862BBD2C806BD900D3564D /* AiRecordManager.m in Sources */, 18862BBE2C806BD900D3564D /* TlwAudioManager.m in Sources */, - 18862BBF2C806BD900D3564D /* TlwRecordManager.m in Sources */, 18862BC02C806BD900D3564D /* AiFrameManager.m in Sources */, 18862BC12C806BD900D3564D /* AiAudioManager.m in Sources */, 18862BC22C806BD900D3564D /* AiConnectManager.m in Sources */, diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m index 7f9aa6a..2a37e21 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.m @@ -62,7 +62,8 @@ _logLevel = AiMouseLogLevel_none; _authState = kIsNeedAuth ? AiMouseAuthState_Unknow:AiMouseAuthState_Success; _connectType = AiMouseConnectType_bluetooth; - NSLog(@"[AiMouseSdk]%@",[NSString stringWithFormat:@"AiMouseSdk_v%@",self.version]); +// NSLog(@"[AiMouseSdk]%@",[NSString stringWithFormat:@"AiMouseSdk_v%@",self.version]); + [self.recordManager checkPermissions]; } return self; } @@ -168,9 +169,9 @@ _connectType = type; [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"]; [self aiConnectManager:nil connectStateDidChange:true]; - if (type == AiMouseConnectType_hid ) { - [self.recordManager checkPermissions]; - } +// if (type == AiMouseConnectType_hid ) { +// [self.recordManager checkPermissions]; +// } } else{ if (self.connected) { @@ -247,7 +248,8 @@ #pragma mark - deviceInfoManager -(void)sendDeviceDataToSdk:(NSData *)data{ - if (data.length == 0) { + if (data.length == 0 || (data.length == 6 && [[AiMouseTools dataToHexString:data] hasSuffix:@"f77f"])) { + [self showLog:@"收到异常数据!!!"]; [self switchConnect:self.connectType isConnected:false]; return; } @@ -396,9 +398,9 @@ if (self.delegate && [self.delegate respondsToSelector:@selector(aiMouseAddLog:)]) { [self.delegate aiMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk][frk]%@",log]]; } - if(self.logLevel == AiMouseLogLevel_info){ - NSLog(@"[AiMouseSdk][frk]%@",log); - } +// if(self.logLevel == AiMouseLogLevel_info){ +// NSLog(@"[AiMouseSdk][frk]%@",log); +// } } - (void)checkAuthState{ diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiRecordManager.m b/MouseSdk/AiMouseSdk/AiMouseSdk/AiRecordManager.m index bf8d05b..4c77e62 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiRecordManager.m +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiRecordManager.m @@ -123,6 +123,7 @@ } - (void)checkPermissions{ + [self showLog:@"查询录音授权状态"]; switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) { case AVAuthorizationStatusAuthorized: self.isAllowRecord = true; @@ -151,6 +152,9 @@ [self openSettings]; break; } + + + } - (void)openSettings{ diff --git a/MouseSdk/AiMouseSdk/Manager/BleManager.m b/MouseSdk/AiMouseSdk/Manager/BleManager.m index 24ee7b1..2dc5d3b 100644 --- a/MouseSdk/AiMouseSdk/Manager/BleManager.m +++ b/MouseSdk/AiMouseSdk/Manager/BleManager.m @@ -188,7 +188,7 @@ #pragma mark - public - (void)startScan{ NSArray *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kFrkServiceUUID],[CBUUID UUIDWithString:kTlwServiceUUID]]]; - [self showLog:[NSString stringWithFormat:@"查询配对过的设备%@",devices]]; +// [self showLog:[NSString stringWithFormat:@"查询配对过的设备%@",devices]]; if (devices.count > 0) { [self showLog:[NSString stringWithFormat:@"配对匹配过的设备%@",devices[0].name]]; @@ -214,7 +214,7 @@ if (self.deviceInfoCha ==nil) { return; } - [self showLog:[NSString stringWithFormat:@"向蓝牙设备发送数据 %@",data]]; +// [self showLog:[NSString stringWithFormat:@"向蓝牙设备发送数据 %@",data]]; if (self.productType == AiMouseProductType_tlw) { [self.curentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse]; @@ -247,11 +247,13 @@ [self.timer invalidate]; self.timer = nil; } - dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{ - self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; - [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; - [[NSRunLoop currentRunLoop] run]; - }); +// dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{ +// self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; +// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; +// [[NSRunLoop currentRunLoop] run]; +// }); +// self.timer = [NSTimer timerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; + self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; } - (void)timerAction{ diff --git a/MouseSdk/AiMouseSdk/Manager/DeviceManger.m b/MouseSdk/AiMouseSdk/Manager/DeviceManger.m index eafcbfd..8bdc099 100644 --- a/MouseSdk/AiMouseSdk/Manager/DeviceManger.m +++ b/MouseSdk/AiMouseSdk/Manager/DeviceManger.m @@ -10,9 +10,10 @@ #import "BleManager.h" #import "TlwMouseSdk.h" #import "TimerManager.h" +#import "AiMouseConfig.h" @interface DeviceManger() -@property (nonatomic, strong) AiMouseDeviceInfo *deviceInfo; + @property (nonatomic,strong) NSMutableData *recordData;//录音的数据 @property (nonatomic,strong) BleManager *ble; @property (nonatomic,strong) HidManager *hid; @@ -50,11 +51,11 @@ - (void)setupSDK{ self.tlwSdk = [[TlwMouse alloc] init]; [self.tlwSdk setDelegate:self]; - [self.tlwSdk setLogLevel:(TlwMouseLogLevel_info)]; + [self.tlwSdk setLogLevel:(TlwMouseLogLevel_none)]; self.frkSdk = [AiMouse shared]; [self.frkSdk setDelegate:self]; - [self.frkSdk setLogLevel:AiMouseLogLevel_info]; + [self.frkSdk setLogLevel:AiMouseLogLevel_none]; } @@ -106,6 +107,7 @@ self.hid.logCallBack = ^(NSString * _Nonnull log) { [ws showLog:log]; }; + [self.hid open]; } - (void)setupBle{ @@ -114,7 +116,7 @@ self.ble.connectStateCallBack = ^(NSString *name,BOOL connected) { if (connected) { - [ws.hid close]; +// [ws.hid close]; ws.productType = ws.ble.productType; if (ws.ble.productType == AiMouseProductType_tlw) { @@ -196,37 +198,12 @@ - (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack{ self.aiMouseConnectStateDidChangeCallBack = callBack; [self.ble start]; + [self.hid open]; } ///获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished{ - [self showLog:@"getDeviceInfo request"]; if (self.productType == AiMouseProductType_tlw) { - [self.tlwSdk getDeviceInfo:^(TlwDeviceInfo *deviceInfo, NSError *error) { - NSString *CPI_list = @"800,1200,1800,2400";// 1-4,800 1200 1800 2400 - NSString *cpi = [[CPI_list componentsSeparatedByString:@","] objectAtIndex:deviceInfo.DPI - 1]; - - AiMouseDeviceInfo *info = [[AiMouseDeviceInfo alloc] init]; - info.battery_power = [NSString stringWithFormat:@"%d",deviceInfo.batteryValue] ; - info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; - info.CPI_list = CPI_list;// 1-4,800 1200 1800 2400 - info.CPI = cpi; -// info.brand = @"NNMouse"; -// info.sn = @"MSCL1BKZ12105000042371"; -// info.mac = @"C0310000A583"; - self.deviceInfo = info; - - [self.tlwSdk getSnInfo:^(TlwSnInfo *snInfo, NSError *error) { - if( info != nil){ - info.brand = snInfo.brand; - info.sn = snInfo.sn; - info.mac = snInfo.sn; - finished(info, nil);; - } - else{ - finished(nil,error); - } - }]; - }]; + [self.tlwSdk getFullDeviceInfo:finished]; } else{ [self.frkSdk getDeviceInfo:finished]; @@ -400,9 +377,11 @@ /// - Parameter data: 数据 -(void)tlwMouseSendDeviceInfoData:(NSData *)data{ if (self.tlwSdk.connectType == AiMouseConnectType_bluetooth) { +// [self showLog:@"send to ble"]; [self.ble writeToDevice:data]; } else if (self.tlwSdk.connectType == AiMouseConnectType_hid){ +// [self showLog:@"send to usb"]; [self.hid writeToDevice:data]; } } @@ -500,14 +479,14 @@ } /// hid错误 -(void)tlwHidDataError{ - [self.hid open]; +// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ +// [self.hid open]; +// }); } /// 输出日志 - (void)tlwMouseAddLog:(NSString *)log { - if (self.aiMouseAddLogCallBack) { - self.aiMouseAddLogCallBack(log); - } + [self showLog:log]; } #pragma mark - AiMouseDelegate @@ -586,9 +565,7 @@ /// 输出sdk日志 /// - Parameter log: 日志 - (void)aiMouseAddLog:(NSString *)log{ - if (self.aiMouseAddLogCallBack) { - self.aiMouseAddLogCallBack(log); - } + [self showLog:log]; } /// hid数据异常,需要重新开启监听usb设备 @@ -597,6 +574,9 @@ } #pragma mark - other - (void)showLog:(NSString *)log{ + if (self.aiMouseAddLogCallBack) { + self.aiMouseAddLogCallBack(log); + } NSLog(@"[AiMouseSdk] %@",log); } diff --git a/MouseSdk/AiMouseSdk/Manager/HidManager.h b/MouseSdk/AiMouseSdk/Manager/HidManager.h index 9529136..6b32010 100644 --- a/MouseSdk/AiMouseSdk/Manager/HidManager.h +++ b/MouseSdk/AiMouseSdk/Manager/HidManager.h @@ -21,7 +21,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)open; -- (void)close; +//- (void)close; //写入数据 -(void)writeToDevice:(NSData *)data; diff --git a/MouseSdk/AiMouseSdk/Manager/HidManager.m b/MouseSdk/AiMouseSdk/Manager/HidManager.m index 1625ae4..16719a2 100644 --- a/MouseSdk/AiMouseSdk/Manager/HidManager.m +++ b/MouseSdk/AiMouseSdk/Manager/HidManager.m @@ -29,7 +29,7 @@ - (instancetype)init{ self = [super init]; if (self) { - + } return self; } @@ -38,28 +38,34 @@ /// 匹配到设备回调 static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { if ([DeviceManger shared].isConneted) { - [[HidManager shared] showLog:(@"当前处于连接状态,对监听到有USB 插入 的事件不做处理")]; +// [[HidManager shared] showLog:(@"当前处于连接状态,对监听到有USB 插入 的事件不做处理")]; return; } uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey)); uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey)); - [[HidManager shared] showLog:([NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef])]; + uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); + uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); + if(vid == 0x10D6 && pid == 0xB008){ + [[HidManager shared] showLog:([NSString stringWithFormat:@"发现设备%@,pid = %d,vid = %d ,page = %d, usage = %d",inIOHIDDeviceRef,pid,vid,PrimaryUsagePage,PrimaryUsage])]; [HidManager shared].productType = AiMouseProductType_tlw; [HidManager shared].deviceInfoCha = inIOHIDDeviceRef; //注册消息回调 unsigned char InputBuffer[64]; IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), &hidReceiveData, NULL); - + IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone); if (result != kIOReturnSuccess) { [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]]; }else{ [[HidManager shared] showLog:@"打开设备成功!"]; - [HidManager shared].connectStateCallBack(true) ; +// [HidManager shared].connectStateCallBack(true) ; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [HidManager shared].connectStateCallBack(true); + }); } - CFRetain([HidManager shared].deviceInfoCha); - IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); +// CFRetain([HidManager shared].deviceInfoCha); +// IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); } else if(vid == 0x6675 && pid == 0x8601){ [HidManager shared].productType = AiMouseProductType_frk; @@ -73,8 +79,8 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH if (result != kIOReturnSuccess) { [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备信息失败! %d",result]]; }else{ - CFRetain(inIOHIDDeviceRef); - IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); +// CFRetain(inIOHIDDeviceRef); +// IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); [HidManager shared].deviceInfoCha = inIOHIDDeviceRef; [[HidManager shared] showLog:@"打开设备信息成功!"]; @@ -86,13 +92,13 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ unsigned char InputBuffer[128]; IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); - + IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone); if (result != kIOReturnSuccess) { [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备voice失败! %d",result]]; }else{ - CFRetain(inIOHIDDeviceRef); - IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); +// CFRetain(inIOHIDDeviceRef); +// IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); [HidManager shared].voiceCha = inIOHIDDeviceRef; [[HidManager shared] showLog:@"打开设备voice成功!"]; @@ -109,32 +115,39 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender, [[HidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]]; if([HidManager shared].deviceInfoCha){ - [[HidManager shared] close]; + [HidManager shared].deviceInfoCha = nil; + [HidManager shared].voiceCha = nil; + [HidManager shared].hidManager = nil; [HidManager shared].connectStateCallBack(false) ; } } static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){ - + if([HidManager shared].productType == AiMouseProductType_tlw){ if (report[0] == 0x01 || report[0] == 0x02) { //鼠标键盘活动事件 return; } - NSData *data = [[NSData alloc] initWithBytes:report length:reportLength]; - [[HidManager shared] showLog:[NSString stringWithFormat:@"receive = %@",data]]; - [HidManager shared].receiveInfoDataCallBack(data); + uint32_t primaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey)); + uint32_t primaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey)); + + if(primaryUsagePage == 1 && primaryUsage == 2){ + NSData *data = [[NSData alloc] initWithBytes:report length:reportLength]; +// [[HidManager shared] showLog:[NSString stringWithFormat:@"receive = %@",data]]; + [HidManager shared].receiveInfoDataCallBack(data); + } } else{ NSData *data1 = [[NSData alloc] initWithBytes:report length:reportLength]; - [[HidManager shared] showLog:[NSString stringWithFormat:@"receive = %@",data1]]; +// [[HidManager shared] showLog:[NSString stringWithFormat:@"receive = %@",data1]]; NSInteger len = get_data_len(report, reportLength); NSData *data = [[NSData alloc] initWithBytes:report length:len]; uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey)); uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey)); - + if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ [[AiMouse shared] sendDeviceDataToSdk:data]; } @@ -149,7 +162,7 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [self close]; //初始化 self.deviceInfoCha = nil; - self.voiceCha = nil; + self.voiceCha = nil; self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); @@ -159,17 +172,17 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [self showLog:(@"打开hid成功!")]; } //设置匹配设备 -// ////供应商id -// const long vendorID = 0x6675; -// //产品id -// const long productID = 0x8601; -// //供应商id -// const long vendorID = 0x10D6; -// //产品id -// const long productID = 0xB008; + // ////供应商id + // const long vendorID = 0x6675; + // //产品id + // const long productID = 0x8601; + // //供应商id + // const long vendorID = 0x10D6; + // //产品id + // const long productID = 0xB008; NSMutableDictionary* dict= [NSMutableDictionary dictionary]; -// [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]]; -// [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]]; + // [dict setValue:[NSNumber numberWithLong:productID] forKey:[NSString stringWithCString:kIOHIDProductIDKey encoding:NSUTF8StringEncoding]]; + // [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]]; IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict); //设置匹配到设备回调 IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL); @@ -182,6 +195,23 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe return; } [self showLog:@"关闭hid"]; + + +// if (self.deviceInfoCha ) { +// IOReturn result = IOHIDDeviceSetReport(self.deviceInfoCha, kIOHIDReportTypeInput, 0, NULL, 0); +// if (result!= kIOReturnSuccess) { +// [self showLog:(@"关闭deviceInfoCha 失败!")]; +// return; +// } +// } +// if (self.voiceCha ) { +// IOReturn result = IOHIDDeviceSetReport(self.voiceCha, kIOHIDReportTypeInput, 0, NULL, 0); +// if (result!= kIOReturnSuccess) { +// [self showLog:(@"关闭 voiceCha 失败!")]; +// return; +// } +// } + IOReturn ret = IOHIDManagerClose(self.hidManager, 0); self.hidManager = nil; @@ -189,23 +219,25 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [self showLog:(@"关闭hid失败!")]; }else{ [self showLog:(@"关闭hid成功!")]; + self.deviceInfoCha = nil; + self.voiceCha = nil; + self.hidManager = nil; } - self.deviceInfoCha = nil; - self.voiceCha = nil; } /// 发送数据给设备 -(void)writeToDevice:(NSData *)data{ +// [self showLog:[NSString stringWithFormat:@"hid收到数据:%@",data]]; if(self.deviceInfoCha == nil){ [self showLog:@"sendData: 请先连上设备"]; return; } - [self showLog:[NSString stringWithFormat:@"向usb设备发送数据 %@",data]]; +// [self showLog:[NSString stringWithFormat:@"向usb设备发送数据 %@",data]]; IOReturn ret = IOHIDDeviceSetReport(self.deviceInfoCha, kIOHIDReportTypeOutput, 0, data.bytes, data.length); if (ret != kIOReturnSuccess) { [self showLog:[NSString stringWithFormat:@"发送失败! code = %d",ret]]; }else{ - [self showLog:@"发送成功!"]; +// [self showLog:@"发送成功!"]; } } diff --git a/MouseSdk/AiMouseSdk/Manager/TimerManager.m b/MouseSdk/AiMouseSdk/Manager/TimerManager.m index b64eefa..87d4517 100644 --- a/MouseSdk/AiMouseSdk/Manager/TimerManager.m +++ b/MouseSdk/AiMouseSdk/Manager/TimerManager.m @@ -27,11 +27,12 @@ #pragma mark - timer - (void)addSendTimer{ - dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{ - self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; - [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; - [[NSRunLoop currentRunLoop] run]; - }); +// dispatch_async(dispatch_queue_create("timeoutQueue", DISPATCH_QUEUE_SERIAL), ^{ +// self.timer = [NSTimer timerWithTimeInterval:3 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; +// [[NSRunLoop currentRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes]; +// [[NSRunLoop currentRunLoop] run]; +// }); + self.timer = [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerAction) userInfo:nil repeats:true]; } - (void)timerAction{ diff --git a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h index edf87de..86e604a 100644 --- a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h +++ b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h @@ -65,9 +65,6 @@ typedef NS_ENUM(NSInteger,TlwMouseLogLevel) { @property (nonatomic, weak) id delegate;/// 代理 @property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型 @property (nonatomic, readonly) BOOL connected;//是否已连接 -// -///// 单利 -//+(TlwMouse *)shared; /// 设置日志等级 /// - Parameter level: 等级 @@ -92,6 +89,10 @@ typedef NS_ENUM(NSInteger,TlwMouseLogLevel) { /// - Parameter completation: 如果获取成功,则deviceInfo有值,否则为nil - (void)getDeviceInfo:(void (^)(TlwDeviceInfo *deviceInfo,NSError *error))finished; +/// 获取设备信息 +/// - Parameter completation: 如果获取成功,则deviceInfo有值,否则为nil +- (void)getFullDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished; + /// 获取sn信息 /// - Parameter completation: 如果获取成功,则info有值,否则为nil - (void)getSnInfo:(void (^)(TlwSnInfo *info,NSError *error))finished; diff --git a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.m b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.m index 6c0e8a9..423e6d3 100644 --- a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.m +++ b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.m @@ -8,53 +8,46 @@ #import "TlwMouseSdk.h" #import "TlwAudioManager.h" #import "TlwTools.h" -#import "TlwRecordManager.h" -#import "TlwConnectManager.h" - +#import "AiRecordManager.h" @implementation TlwDeviceInfo -(NSString *)description{ - NSString *info = [NSString stringWithFormat:@"\n version = %d\n DPI = %d\n batteryState = %d\n batteryValue = %d\n",self.version,self.DPI,self.batteryState,self.batteryValue]; + NSString *info = [NSString stringWithFormat:@"version = %d DPI = %d batteryState = %d batteryValue = %d",self.version,self.DPI,self.batteryState,self.batteryValue]; return info; } @end @implementation TlwSnInfo -(NSString *)description{ - NSString *info = [NSString stringWithFormat:@"\n code = %@\n brand = %@\n batch = %@\n sn = %@\n",self.code,self.brand,self.batch,self.sn]; + NSString *info = [NSString stringWithFormat:@"code = %@ brand = %@ batch = %@ sn = %@",self.code,self.brand,self.batch,self.sn]; return info; } @end -@interface TlwMouse() +@interface TlwMouse() @property (nonatomic, assign) TlwMouseLogLevel logLevel; @property (nonatomic, strong) TlwAudioManager *audioManager; -@property (nonatomic, strong) TlwRecordManager *recordManager; -@property (nonatomic, strong) TlwConnectManager *connectManager; +@property (nonatomic, strong) AiRecordManager *recordManager; + +@property (nonatomic,strong)void (^openRecordCallBack)(BOOL isSuccess,NSError *error); +@property (nonatomic,strong)void (^closeRecordCallBaack)(BOOL isSuccess,NSError *error); +@property (nonatomic,strong)void (^setDpiCallBack)(BOOL isSuccess,NSError *error); +@property (nonatomic,strong)void (^getMouseInfoCallBack)(TlwDeviceInfo *deviceInfo,NSError *error); +@property (nonatomic,strong)void (^getSnInfoCallBack)(TlwSnInfo *snInfo,NSError *error); -@property (nonatomic,strong)void (^openRecord)(BOOL isSuccess,NSError *error); -@property (nonatomic,strong)void (^closeRecord)(BOOL isSuccess,NSError *error); -@property (nonatomic,strong)void (^setDpi)(BOOL isSuccess,NSError *error); -@property (nonatomic,strong)void (^getMouseInfo)(TlwDeviceInfo *deviceInfo,NSError *error); -@property (nonatomic,strong)void (^getSnInfo)(TlwSnInfo *snInfo,NSError *error); +@property (nonatomic,strong)void (^getFullInfoCallBack)(AiMouseDeviceInfo *info,NSError *error); +@property (nonatomic,strong)AiMouseDeviceInfo *fullInfo; +@property (nonatomic,assign)NSInteger questTimes; @end @implementation TlwMouse -//+(TlwMouse *)shared{ -// static TlwMouse *manager = nil; -// static dispatch_once_t oncePredicate; -// dispatch_once(&oncePredicate, ^{ -// manager = [[TlwMouse alloc]init]; -// }); -// return manager; -//} -(instancetype)init{ if (self = [super init]) { _logLevel = TlwMouseLogLevel_none; _connectType = AiMouseConnectType_bluetooth; - [self.connectManager start]; NSLog(@"[TlwMouseSdk]%@",[NSString stringWithFormat:@"AiMouseSdk_v%@",self.version]); + [self.recordManager checkPermissions]; } return self; } @@ -77,9 +70,9 @@ return _audioManager; } -- (TlwRecordManager *)recordManager{ +- (AiRecordManager *)recordManager{ if (!_recordManager) { - _recordManager = [[TlwRecordManager alloc] init]; + _recordManager = [[AiRecordManager alloc] init]; __weak typeof(self) ws = self; _recordManager.didRecordData = ^(NSData * _Nonnull data) { [ws showLog:[NSString stringWithFormat:@"%@",data]]; @@ -88,14 +81,28 @@ } return _recordManager; } - -//-(TlwConnectManager *)connectManager{ -// if (!_connectManager) { -// _connectManager = [[TlwConnectManager alloc] init]; -// _connectManager.delegate = self; -// } -// return _connectManager; -//} +- (void)sendOpenCmd{ + uint8_t cmd = 0x0D; + uint8_t len = 0x00; + uint8_t tail = 0xEF; + NSMutableData *mData = NSMutableData.data; + [mData appendData:[self getHidHeader]]; + [mData appendBytes:&cmd length:1]; + [mData appendBytes:&len length:1]; + [mData appendBytes:&tail length:1]; + [self sendToDevice:mData]; +} +- (void)sendDongleCmd{ + uint8_t cmd = 0x01; + uint8_t len = 0x00; + uint8_t tail = 0xEF; + NSMutableData *mData = NSMutableData.data; + [mData appendData:[self getHidHeader]]; + [mData appendBytes:&cmd length:1]; + [mData appendBytes:&len length:1]; + [mData appendBytes:&tail length:1]; + [self sendToDevice:mData]; +} #pragma mark - pub Api - (void)setLogLevel:(TlwMouseLogLevel)level{ @@ -104,31 +111,30 @@ - (void)didDisconnect{ _connected = false; [self stopRecord]; -// [self.connectManager disConnected]; } - (void)switchConnect:(AiMouseConnectType)type isConnected:(BOOL)isConneted{ - if(isConneted ){ - _connected = true; - _connectType = type; - [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"]; - [self TlwConnectManager:self.connectManager connectStateDidChange:true]; - if (type == AiMouseConnectType_hid ) { - [self.recordManager checkPermissions]; - } + if(isConneted ){ + _connected = true; + _connectType = type; + [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"]; + [self TlwConnectManager:nil connectStateDidChange:true]; + if (type == AiMouseConnectType_hid ) { + [self.recordManager checkPermissions]; + [self sendOpenCmd]; + [self sendDongleCmd]; } - else{ - [self stopRecord]; - if (self.connected) { - _connected = false; - [self TlwConnectManager:self.connectManager connectStateDidChange:false]; - } + } + else{ + [self stopRecord]; + if (self.connected) { + _connected = false; + [self TlwConnectManager:nil connectStateDidChange:false]; } + } } - (void)sendAiDataToSdk:(NSData *)data{ - [self.connectManager resetTimeOut]; - [self showLog:[NSString stringWithFormat:@"[receive][Voice] %@",data]]; if(self.connectType == AiMouseConnectType_hid){ } @@ -138,17 +144,18 @@ } -(void)sendDeviceDataToSdk:(NSData *)data{ - [self showLog:[NSString stringWithFormat:@"[receive][info] %@",[TlwTools dataToHexString:data]]]; if(self.connectType == AiMouseConnectType_hid){ + [self showLog:[NSString stringWithFormat:@"[receive][usb][info] %@",[TlwTools dataToHexString:data]]]; [self hidReceiveDeviceData:data]; } else if (self.connectType == AiMouseConnectType_bluetooth){ + [self showLog:[NSString stringWithFormat:@"[receive][ble][info] %@",[TlwTools dataToHexString:data]]]; [self bleReceiveDeviceData:data]; } } - (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ if(self.connectType == AiMouseConnectType_hid){ - self.openRecord = finished; + self.openRecordCallBack = finished; uint8_t cmd = 0x03; uint8_t len = 0x00; uint8_t tail = 0xEF; @@ -160,7 +167,7 @@ [self sendToDevice:mData]; } else if (self.connectType == AiMouseConnectType_bluetooth){ - self.openRecord = finished; + self.openRecordCallBack = finished; uint8_t cmd = 0x03; uint8_t dir = 0x01; uint8_t len = 0x00; @@ -173,7 +180,7 @@ } - (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ if(self.connectType == AiMouseConnectType_hid){ - self.closeRecord = finished; + self.closeRecordCallBaack = finished; uint8_t cmd = 0x04; uint8_t len = 0x00; uint8_t tail = 0xEF; @@ -185,7 +192,7 @@ [self sendToDevice:mData]; } else if (self.connectType == AiMouseConnectType_bluetooth){ - self.closeRecord = finished; + self.closeRecordCallBaack = finished; uint8_t cmd = 0x04; uint8_t dir = 0x01; uint8_t len = 0x00; @@ -200,7 +207,7 @@ - (void)setDPI:(uint8_t)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{ if(self.connectType == AiMouseConnectType_hid){ - self.setDpi = finished; + self.setDpiCallBack = finished; uint8_t type = 0x06; uint8_t len = 0x01; uint8_t tail = 0xEF; @@ -213,7 +220,7 @@ [self sendToDevice:mData]; } else if (self.connectType == AiMouseConnectType_bluetooth){ - self.setDpi = finished; + self.setDpiCallBack = finished; uint8_t cmd = 0x06; uint8_t dir = 0x01; uint8_t len = 0x01; @@ -229,7 +236,7 @@ } - (void)getDeviceInfo:(void (^)(TlwDeviceInfo *deviceInfo,NSError *error))finished{ if(self.connectType == AiMouseConnectType_hid){ - self.getMouseInfo = finished; + self.getMouseInfoCallBack = finished; // 0x05fff1fec09f0114ef uint8_t cmd = 0x0c; uint8_t len = 0x00; @@ -243,7 +250,7 @@ [self sendToDevice:mData]; } else if (self.connectType == AiMouseConnectType_bluetooth){ - self.getMouseInfo = finished; + self.getMouseInfoCallBack = finished; uint8_t cmd = 0x0c; uint8_t dir = 0x01; uint8_t len = 0x00; @@ -257,7 +264,7 @@ - (void)getSnInfo:(void (^)(TlwSnInfo *info,NSError *error))finished{ if(self.connectType == AiMouseConnectType_hid){ - self.getSnInfo = finished; + self.getSnInfoCallBack = finished; uint8_t cmd = 0x12; uint8_t len = 0x00; uint8_t tail = 0xEF; @@ -270,7 +277,7 @@ [self sendToDevice:mData]; } else if (self.connectType == AiMouseConnectType_bluetooth){ - self.getSnInfo = finished; + self.getSnInfoCallBack = finished; uint8_t cmd = 0x12; uint8_t dir = 0x01; uint8_t len = 0x00; @@ -282,9 +289,60 @@ } } + +- (void)getFullDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))finished{ + self.getFullInfoCallBack = finished; + self.questTimes = 0; + [self checkFullInfoAction]; +} + +- (void)checkFullInfoAction{ + self.questTimes++; + if (self.questTimes > 3) { + [self switchConnect:AiMouseConnectType_hid isConnected:false ]; + return; + } + if (self.getFullInfoCallBack) { + [self reGetFullInfoAction]; + } +} +- (void)reGetFullInfoAction{ + __weak typeof(self) ws = self; + [self getDeviceInfo:^(TlwDeviceInfo *deviceInfo, NSError *error) { + if(deviceInfo != nil){ + NSString *CPI_list = @"800,1200,1800,2400";// 1-4,800 1200 1800 2400 + NSString *cpi = [[CPI_list componentsSeparatedByString:@","] objectAtIndex:deviceInfo.DPI - 1]; + + AiMouseDeviceInfo *info = [[AiMouseDeviceInfo alloc] init]; + info.battery_power = [NSString stringWithFormat:@"%d",deviceInfo.batteryValue] ; + info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; + info.CPI_list = CPI_list;// 1-4,800 1200 1800 2400 + info.CPI = cpi; + ws.fullInfo = info; + + [ws getSnInfo:^(TlwSnInfo *snInfo, NSError *error) { + if( info != nil){ + info.brand = snInfo.brand; + info.sn = snInfo.sn; + info.mac = snInfo.sn; + [ws showLog:info.description]; + if(ws.getFullInfoCallBack)ws.getFullInfoCallBack(info, nil); + ws.getFullInfoCallBack = nil; + } + else{ + if(ws.getFullInfoCallBack)ws.getFullInfoCallBack(nil,error); + } + }]; + } + else{ + if(ws.getFullInfoCallBack) ws.getFullInfoCallBack(nil,error); + } + }]; + + [self performSelector:@selector(checkFullInfoAction) withObject:nil afterDelay:1]; +} #pragma mark - ble - (void)bleReceiveAiData:(NSData *)data{ - [self.connectManager resetTimeOut]; [self.audioManager receiveData:data]; } @@ -304,7 +362,7 @@ BOOL success = state == 0; [self showLog:[NSString stringWithFormat:@"语音开启回复 %@",@(success)]]; - self.openRecord(success, nil); + if(self.openRecordCallBack)self.openRecordCallBack(success, nil); break; } case 0x6B: @@ -314,7 +372,7 @@ [[data subdataWithRange:NSMakeRange(2, 1)] getBytes:&state length:1]; BOOL success = state == 0; [self showLog:[NSString stringWithFormat:@"语音停止回复 %@",@(success)]]; - self.closeRecord(success, nil); + if(self.closeRecordCallBaack)self.closeRecordCallBaack(success, nil); break; } case 0x96: @@ -324,7 +382,7 @@ [[data subdataWithRange:NSMakeRange(2, 1)] getBytes:&state length:1]; BOOL success = state == 0; [self showLog:[NSString stringWithFormat:@"设置DPI回复 %@",@(success)]]; - self.setDpi(success, nil); + if(self.setDpiCallBack)self.setDpiCallBack(success, nil); break; } case 0x9c: @@ -347,7 +405,7 @@ info.batteryState = batteryState; info.batteryValue = batteryValue; [self showLog:info.description]; - self.getMouseInfo(info, nil); + if(self.getMouseInfoCallBack)self.getMouseInfoCallBack(info, nil); break; } case 0x9f: @@ -371,7 +429,7 @@ info.batch = batch; info.sn = sn; [self showLog:info.description]; - self.getSnInfo(info, nil); + if(self.getSnInfoCallBack)self.getSnInfoCallBack(info, nil); break; } @@ -385,14 +443,12 @@ // 0x05fff1fec09f0114ef if(data.length < 7 ){ [self showLog:@"收到异常数据"]; - [self switchConnect:AiMouseConnectType_hid isConnected:false ]; - [self.delegate tlwHidDataError]; + // [self switchConnect:AiMouseConnectType_hid isConnected:false ]; return; } if(![[[TlwTools dataToHexString:[data subdataWithRange:NSMakeRange(1, 2)]] lowercaseString] isEqualToString:@"fff1"]){ [self showLog:@"收到异常数据"]; - [self switchConnect:AiMouseConnectType_hid isConnected:false ]; - [self.delegate tlwHidDataError]; + // [self switchConnect:AiMouseConnectType_hid isConnected:false ]; return; } @@ -411,7 +467,7 @@ [[data subdataWithRange:NSMakeRange(6, 1)] getBytes:&state length:1]; BOOL success = state == 0x02; [self showLog:[NSString stringWithFormat:@"语音开启回复 %@",@(success)]]; - self.openRecord(true, nil); + if(self.openRecordCallBack)self.openRecordCallBack(true, nil); [self startRecord]; break; } @@ -425,7 +481,7 @@ [self showLog:[NSString stringWithFormat:@"语音停止回复 %@",@(success)]]; [self stopRecord]; - self.closeRecord(true, nil); + if(self.closeRecordCallBaack)self.closeRecordCallBaack(success, nil); break; } case 0x96: @@ -436,7 +492,7 @@ BOOL success = state == 0x02; [self showLog:[NSString stringWithFormat:@"设置DPI回复 %@",@(success)]]; - self.setDpi(true, nil); + if(self.setDpiCallBack)self.setDpiCallBack(true, nil); break; } case 0x9c: @@ -459,10 +515,20 @@ info.batteryState = batteryState; info.batteryValue = batteryValue; [self showLog:info.description]; - self.getMouseInfo(info, nil); + if(self.getMouseInfoCallBack)self.getMouseInfoCallBack(info, nil); break; } + case 0x9d: + { + uint16_t version; + [[data subdataWithRange:NSMakeRange(7, 2)] getBytes:&version length:2]; + uint8_t state; + [[data subdataWithRange:NSMakeRange(9, 1)] getBytes:&state length:1]; + + [self showLog:[NSString stringWithFormat:@"dongle version = %d,bind = %d",version,state]]; + break; + } case 0x9f: { NSData *subData = [data subdataWithRange:NSMakeRange(7, 1)]; @@ -496,7 +562,7 @@ info.batch = batch; info.sn = sn; [self showLog:info.description]; - self.getSnInfo(info, nil); + if(self.getSnInfoCallBack)self.getSnInfoCallBack(info, nil); break; } @@ -506,7 +572,7 @@ } - (NSData *)getHidHeader{ uint8_t head1 = 0x05; - uint16_t head2 = 0xFFF1; + uint16_t head2 = 0xF1FF; uint8_t head3 = 0xfe; uint8_t head4 = 0x00; @@ -531,11 +597,11 @@ - (void)showLog:(NSString *)log{ if (self.delegate && [self.delegate respondsToSelector:@selector(tlwMouseAddLog:)]) { - [self.delegate tlwMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk][tlw]%@",log]]; - } - if(self.logLevel == TlwMouseLogLevel_info){ - NSLog(@"[AiMouseSdk][tlw]%@",log); + [self.delegate tlwMouseAddLog:[NSString stringWithFormat:@"[AiMouseSdk][tlw] %@",log]]; } + // if(self.logLevel == TlwMouseLogLevel_info){ + // NSLog(@"[AiMouseSdk][tlw]%@",log); + // } } @@ -547,7 +613,7 @@ [data appendBytes:&d length:1]; } } - [self showLog:[NSString stringWithFormat:@"[send][info] %@",data]]; + [self showLog:[NSString stringWithFormat:@"[send][info] %@",[TlwTools dataToHexString:data]]]; [self.delegate tlwMouseSendDeviceInfoData:data]; } #pragma mark - deviceInfo @@ -562,31 +628,18 @@ } #pragma mark - TlwConnectManagerDelegate --(void)TlwConnectManager:(TlwConnectManager *)manager connectStateDidChange:(BOOL)connected{ +-(void)TlwConnectManager:(NSString *)manager connectStateDidChange:(BOOL)connected{ _connected = connected; if([self.delegate respondsToSelector:@selector(tlwMouseConnectStateDidChange:)]) [self.delegate tlwMouseConnectStateDidChange:connected]; } --(void)TlwConnectManagerReSendData:(TlwConnectManager *)manager{ - // __weak typeof(self) ws = self; - // [self.deviceInfoManager getVersion:^(NSString * _Nonnull version, NSError * _Nonnull error) { - // if (error.code == AiMouseErrorCode_timeOut) { - // [ws.connectManager addTimeOut]; - // } - // }]; -} --(void)TlwConnectManager:(TlwConnectManager *)manager outputLog:(NSString *)log{ - [self showLog:log]; -} - (NSString *)getBrand:(NSData *)data{ - NSMutableString *mText = [[NSMutableString alloc] init]; NSString *text = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; for (NSInteger i = 0; i < text.length;i++) { if ([[text substringWithRange:NSMakeRange(i, 1)] isEqual:@"0"]) { return [text substringWithRange:NSMakeRange(0, i)]; } } - return text; } @end diff --git a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.h b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.h deleted file mode 100644 index 3d499c6..0000000 --- a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.h +++ /dev/null @@ -1,25 +0,0 @@ -// -// TlwRecordManager.h -// AudioRecorder -// -// Created by incar on 2024/3/29. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface TlwRecordManager : NSObject -@property (nonatomic,assign)BOOL recording; -@property (nonatomic,strong)NSData *startCmd; -@property (nonatomic,readonly)NSData *stopCmd; -@property (nonatomic,strong)void (^didRecordData)(NSData *data); - -- (void)checkPermissions; - -- (void)start; - -- (void)stop; -@end - -NS_ASSUME_NONNULL_END diff --git a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.m b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.m deleted file mode 100644 index 24e06b1..0000000 --- a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwRecordManager.m +++ /dev/null @@ -1,194 +0,0 @@ -// -// TlwRecordManager.m -// AudioRecorder -// -// Created by incar on 2024/3/29. -// - -#import "TlwRecordManager.h" -#import - -@interface TlwRecordManager() -@property (nonatomic,assign)BOOL isAllowRecord; -@property (nonatomic,strong)AVAudioEngine *audioEngine; -@property (nonatomic,strong)AVAudioFile *outputFile; - -@end - -@implementation TlwRecordManager -//+(TlwRecordManager *)shared{ -// static TlwRecordManager *manager = nil; -// static dispatch_once_t oncePredicate; -// dispatch_once(&oncePredicate, ^{ -// manager = [[TlwRecordManager alloc]init]; -// }); -// return manager; -//} - - --(instancetype)init{ - if (self = [super init ]) { - self.isAllowRecord = false; -// [self checkPermissions]; - } - return self; -} - -- (void)start{ - if (self.isAllowRecord == false) { - return; - } - // let audioSession = AVAudioSession.sharedInstance() - // do { - // try audioSession.setCategory(.playAndRecord, options: .defaultToSpeaker) - // try audioSession.setActive(true, options: .notifyOthersOnDeactivation) - // catch let error as NSError { - // print("ERROR:", error) - // } - self.recording = true; - - self.audioEngine = [[AVAudioEngine alloc] init]; - AVAudioInputNode *input = self.audioEngine.inputNode; - AVAudioNodeBus bus = 0; - AVAudioFormat *inputFormat = [input inputFormatForBus:bus]; - // NSString *fullPath = [NSString stringWithFormat:@"%@1.pcm",NSTemporaryDirectory()]; - // NSURL *fileURL = [[[NSFileManager defaultManager] URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask].firstObject URLByAppendingPathComponent:@"record.caf"]; - // [self showLog:fileURL.absoluteString]; - // NSError *err0; - // self.outputFile = [[AVAudioFile alloc] initForWriting:fileURL settings:inputFormat.settings commonFormat:inputFormat.commonFormat interleaved:inputFormat.isInterleaved error:&err0]; - // if (err0) { - // [self showLog:[NSString stringWithFormat:@"设置写入路径 %@", err0.description]]; - // } - // else{ - // [self showLog:@"写入路径设置成功"]; - // } - // self.recordData = [[NSMutableData alloc] init]; - __weak typeof(self) ws = self; - [input installTapOnBus:bus bufferSize:32 format:inputFormat block:^(AVAudioPCMBuffer * _Nonnull buffer, AVAudioTime * _Nonnull when) { - NSData *data = [ws pcm_downsample:buffer]; - if (ws.didRecordData) { - ws.didRecordData(data); - } -// NSLog(@"%@",data); -// NSError *err0; - // [ws.outputFile writeFromBuffer:buffer error:&err0]; - // if (err0) { - // [self showLog:[NSString stringWithFormat:@"写入失败 %@", err0.description]]; - // } - // else{ - // [self showLog:@"写入成功"]; - // } - }]; - - NSError *err1; - [self.audioEngine prepare]; - [self.audioEngine startAndReturnError:&err1]; - if (err1) { - [self showLog:[NSString stringWithFormat:@"启动录音失败 %@", err1.description]]; - } - else{ - [self showLog:@"启动成功"]; - } -} - -- (void)stop{ - [self.audioEngine stop]; - self.recording = false; - // [self finishRecored:self.recordData]; -} - -- (void)checkPermissions{ - switch ([AVCaptureDevice authorizationStatusForMediaType:AVMediaTypeAudio]) { - case AVAuthorizationStatusAuthorized: - self.isAllowRecord = true; - [self showLog:@"录音已经授权"]; - break; - case AVAuthorizationStatusNotDetermined: - { - [self showLog:@"录音未授权"]; - [AVCaptureDevice requestAccessForMediaType:AVMediaTypeAudio completionHandler:^(BOOL granted) { - if (granted) { - self.isAllowRecord = true; - [self showLog:@"录音已经授权"]; - } - else{ - [self showLog:@"已拒绝"]; - } - }]; - } - break; - case AVAuthorizationStatusDenied: - [self showLog:@"已拒绝"]; - [self openSettings]; - break; - default: - [self showLog:@"已拒绝"]; - [self openSettings]; - break; - } -} - -- (void)openSettings{ -// NSURL *url = [NSURL URLWithString:@""]; -} - -- (void)showLog:(NSString *)log{ - NSLog(@"%@",log); -} - -//将录音数据保存 -- (void)finishRecored:(NSData *)data{ - NSString *doc = NSTemporaryDirectory(); - NSString *fullPath = [NSString stringWithFormat:@"%@1.pcm",doc]; - NSError *err; - [data writeToFile:fullPath options:NSDataWritingAtomic error:&err]; - if (err) { - [self showLog:[NSString stringWithFormat:@"录音保存失败 %@",err.description]]; - } - else{ - [self showLog:[NSString stringWithFormat:@"录音保存成功 path = %@",fullPath]]; - } -} - --(NSData*) pcm_downsample:(AVAudioPCMBuffer*)buffer { - //位深转换 float32 转 int16_t - NSData* pData = [[NSData alloc] initWithBytes:buffer.floatChannelData[0] length:buffer.frameLength * 4]; - Byte*dataByte = (Byte*)[pData bytes]; - NSUInteger srcSize = pData.length/4; - Float32 vf32[srcSize]; - memcpy(vf32, &dataByte[0], pData.length); - int16_t bArr[srcSize]; - int r; - for(size_t i =0; i < srcSize; ++i) { - float x = vf32[i]; - float c; - c = ((x < -1) ? -1: ((x >1) ?1: x)); - c = c +1; - r = (int)(c *32767.5f); - r = r -32768; - bArr[i] = (short)r; - } - //码率转换 44100 转 16000 - uint32_t last_pos = (uint32_t)(srcSize -1); - uint32_t dstSize = (srcSize/44100.0)*16000; - int16_t bArr16[dstSize]; - for(uint32_t idx =0; idx < dstSize; idx++) { - float index = ((float) idx *44100.0) / (16000.0); - uint32_t p1 = (uint32_t) index; - float coef = index - p1; - uint32_t p2 = (p1 == last_pos) ? last_pos : p1 +1; - bArr16[idx] = (int16_t) ((1.0f- coef) * bArr[p1] + coef * bArr[p2]); - } - NSMutableData *data1 = [[NSMutableData alloc] init]; - for(NSUInteger i =0; i < dstSize; i++) { - unsigned char valChar[2]; - valChar[0] =0xff& bArr16[i]; - valChar[1] = (0xff00& bArr16[i]) >>8; - [data1 appendBytes:valChar length:2]; - } - return data1; -} - - - -@end diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index f9405c9..22d11df 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 73c71aa..c3ddbcd 100644 --- a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -5,25 +5,20 @@ version = "2.0"> + BreakpointExtensionID = "Xcode.Breakpoint.RuntimeIssueBreakpoint"> + breakpointStackSelectionBehavior = "1" + type = "65535"> + BreakpointExtensionID = "Xcode.Breakpoint.SymbolicBreakpoint"> + symbolName = "" + moduleName = ""> - - - - - - - - - - - - - - - - - - diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 4ea3ace..e0cbb55 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -226,7 +226,6 @@ if(connected){ ws.typeLabel.stringValue = connectType == 0 ?@"连接模式:蓝牙":@"连接模式:hid"; ws.connectLabel.stringValue = connected?@"连接状态:已连接":@"连接状态:未连接"; - [ws geviceInfoAction]; } else{ ws.typeLabel.stringValue = @"连接模式:--"; @@ -263,7 +262,7 @@ }]; } - (void)setDPI:(NSMenuItem*)sender { - [[DeviceManger shared] setCPI:1600 completation:^(BOOL isSuccess, NSError * _Nonnull error) { + [[DeviceManger shared] setCPI:800 completation:^(BOOL isSuccess, NSError * _Nonnull error) { }]; }