From: liangzhenying <867174124@qq.com> Date: Sat, 24 Aug 2024 04:45:24 +0000 (+0800) Subject: 代码整理 X-Git-Tag: 千寻适配d9~41 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=62bb52e739bac75bc436c24fe94715fd3879e487;p=mirrors%2FMouseSdk-MacOS.git 代码整理 --- diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index a2b2e2a..eb8da1d 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/Ble/BleManager.h b/MouseSdk/MouseSdk/Ble/BleManager.h index 1b3a566..450ab20 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.h +++ b/MouseSdk/MouseSdk/Ble/BleManager.h @@ -43,8 +43,7 @@ ///直连蓝牙设备,该函数已经处理了startScanDevice、connectPeripheral和stopScanDevice的调用 /// - Parameter bleName:蓝牙设备名称 - (void)connectBleDeviceWithName:(NSString *)bleName; -//断开设备链接 -- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result; + /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; @@ -52,8 +51,5 @@ /// 获取信号强度 - (void)getRSSI:(void (^)(NSString *value,NSError *error))complete; -/////开始鉴权 -//-(void)startAuthentication; - @end diff --git a/MouseSdk/MouseSdk/Ble/BleManager.m b/MouseSdk/MouseSdk/Ble/BleManager.m index 9789e87..a59f61f 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.m +++ b/MouseSdk/MouseSdk/Ble/BleManager.m @@ -396,39 +396,6 @@ // dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), _block); } -- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result { - self.activeDisconnectCallBack = result; - - _isActiveDisconnect = YES; - if (self.connectPeripheral == nil) { - if (result) { - result(YES); - } - return; - } - - if (self.connectPeripheral.state == CBPeripheralStateDisconnected) { - if (result) { - result(YES); - } - return; - } - - int sec = 0; - if (self.connectPeripheral.state == CBPeripheralStateConnecting) { - sec = 10; - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ - [self.centralManager cancelPeripheralConnection:self.connectPeripheral]; - }); - - }else if (self.connectPeripheral.state == CBPeripheralStateConnected) { - [self.centralManager cancelPeripheralConnection:self.connectPeripheral]; - } - - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(sec+10 * NSEC_PER_SEC)), dispatch_get_main_queue(), _disconnectBlock); -// [self removeMonitoring]; -} - /// 连接设备 - (void)startScanDevice{ [self showLog:[NSString stringWithFormat:@"(BleManager) startScanDevice:开始扫描"]]; @@ -463,7 +430,7 @@ info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; info.CPI_list = @"1,2,3,4"; info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; - info.brand = @""; + info.brand = @"NNMouse"; info.sn = @""; info.mac = @""; completation(info,nil); @@ -488,23 +455,6 @@ } -///// 鉴权 -//-(void)startAuthentication{ -// //设置代理 -// [TlwMouse shared].delegate = self; -// //切换当前连接类型 -// [[TlwMouse shared] switchConnect:AiMouseConnectType_bluetooth]; -// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 -// [self showLog:@"(BleManager) startAuthentication 开始鉴权"]; -// //鉴权 -// [[TlwMouse shared] startAuthentication:^(AiMouseAuthState state) { -// [self showLog:[NSString stringWithFormat:@"(BleManager) startAuthentication 鉴权结果 = %@",@(state)]]; -// if(self.authCallBack){ -// self.authCallBack(state == AiMouseAuthState_Success); -// } -// }]; -//} - #pragma mark - other //自动连接,获取已配对的蓝牙,取第一个连接 - (void)autoConnectDevice{ diff --git a/MouseSdk/MouseSdk/Ble/DeviceManger.h b/MouseSdk/MouseSdk/Ble/DeviceManger.h index 9cf626d..562ba7b 100644 --- a/MouseSdk/MouseSdk/Ble/DeviceManger.h +++ b/MouseSdk/MouseSdk/Ble/DeviceManger.h @@ -10,10 +10,6 @@ NS_ASSUME_NONNULL_BEGIN -///用于connectStateCallBack的connectState -#define connectState_Success @"success" -#define connectState_Fail @"fail" - @interface DeviceManger : NSObject @property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型 @@ -39,18 +35,6 @@ NS_ASSUME_NONNULL_BEGIN ///开始监听鼠标的链接状态 - (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack; -//#pragma mark ------- 蓝牙的开始连接 -///// 根据蓝牙设备名称直连蓝牙设备 超时时间15s -///// - Parameter bleName: 设备名 -//- (void)connectBleDeviceWithName:(NSString *)bleName; -/////断开蓝牙设备链接 -//- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result; -// -//#pragma mark ------- USB的开始连接 -/////开始尝试连接usb 超时时间15s -//- (void)connectHidDevice; - - #pragma mark ------- 共用 /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; @@ -68,11 +52,6 @@ NS_ASSUME_NONNULL_BEGIN /// - finished: 设置结果回调 - (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished; -/////开始鉴权 -//-(void)startAuthentication; - -///// 开始鉴权 -//- (void)startAuthentication:(void (^)(BOOL isSuccess))completation; /// 设置录音模式 /// - Parameters: diff --git a/MouseSdk/MouseSdk/Ble/DeviceManger.m b/MouseSdk/MouseSdk/Ble/DeviceManger.m index 01ad3a7..3435170 100644 --- a/MouseSdk/MouseSdk/Ble/DeviceManger.m +++ b/MouseSdk/MouseSdk/Ble/DeviceManger.m @@ -14,10 +14,8 @@ @property (nonatomic,strong) BleManager *ble; @property (nonatomic,strong) HidManager *hid; - //connectType 返回为0代表蓝牙,为1代表usb @property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(int connectType, BOOL connected);/// 设备连接状态回调 - @end @implementation DeviceManger @@ -59,42 +57,12 @@ } } -//- (void)switchConnect:(AiMouseConnectType)type{ -// [[TlwMouse shared]] switchConnect:type]; -// -// if ([TlwMouse shared]].connectType == AiMouseConnectType_bluetooth) { -// [self setupBle]; -// [TlwMouse shared]].delegate = self.ble; -// } -// else{ -// [self setupHid]; -// [TlwMouse shared]].delegate = self.hid; -// } -//} - #pragma mark ------ 设备连接相关 - (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack{ self.aiMouseConnectStateDidChangeCallBack = callBack; [self.ble connectBleDeviceWithName:mouseName]; } -- (void)connectBleDeviceWithName:(NSString *)bleName { -// //初始化蓝牙 -// [self setupBle]; - [self.ble connectBleDeviceWithName:bleName]; -} - -//断开蓝牙设备链接 -- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result { - [self.ble disconnectBleDevice:result]; -} - - -- (void)connectHidDevice { -// //初始化hid -// [self setupHid]; - [self.hid connectHidDevice]; -} #pragma mark ------ 设备初始化 - (void)setupHid{ @@ -104,15 +72,6 @@ self.hid.authCallBack = ^(BOOL isSuccess) { if(ws.authCallBack)ws.authCallBack(isSuccess); }; - //改为由aiMouseConnectStateDidChangeCallBack统一监听 -// self.hid.connectStateCallBack = ^(BOOL isConnnect) { -// NSString *state = connectState_Success; -// if (!isConnnect) { -// state = connectState_Fail; -// } -// //单独的usb监听链接接口 -// if(ws.hidConnectStateCallBack)ws.hidConnectStateCallBack(state); -// }; self.hid.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) { if (ws.aiMouseConnectStateDidChangeCallBack) { @@ -176,17 +135,7 @@ self.ble.authCallBack = ^(BOOL isSuccess) { if(ws.authCallBack)ws.authCallBack(isSuccess); }; - - //改为由aiMouseConnectStateDidChangeCallBack统一监听 -// self.ble.didConnectDevice = ^(CBPeripheral *peripheral) { -// //单独的蓝牙监听链接接口 -// if(ws.bleConnectStateCallBack)ws.bleConnectStateCallBack(connectState_Success); -// }; -// self.ble.didDisConnectDevice = ^(CBPeripheral *peripheral) { -// //单独的蓝牙监听链接接口 -// if(ws.bleConnectStateCallBack)ws.bleConnectStateCallBack(connectState_Fail); -// }; - + self.ble.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) { if (ws.aiMouseConnectStateDidChangeCallBack) { ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_bluetooth,connected); @@ -254,7 +203,7 @@ info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; info.CPI_list = @"1,2,3,4"; info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; - info.brand = @""; + info.brand = @"NNMouse"; info.sn = @""; info.mac = @""; completation(info,nil); @@ -280,22 +229,6 @@ finished(false,[NSError errorWithDomain:@"AiMouse SDK Error" code:AiMouseErrorCode_unknown userInfo:@{@"NSLocalizedDescriptionKey":@"请求失败"}]); } -/////设备鉴权 -//-(void)startAuthentication { -// if ([TlwMouse shared]].connectType == AiMouseConnectType_bluetooth) { -// [self.ble startAuthentication]; -// } -// else{ -// [self.hid startAuthentication]; -// } -//} -// -//- (void)startAuthentication:(void (^)(BOOL isSuccess))completation { -// self.authCallBack = completation; -// //设备鉴权 -// [self startAuthentication]; -//} - /// 设置录音模式 - (void)setRecordMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished{ // [[TlwMouse shared] setRecordMode:mode completation:finished]; diff --git a/MouseSdk/MouseSdk/Ble/HidManager.h b/MouseSdk/MouseSdk/Ble/HidManager.h index 681424e..3ac2c64 100644 --- a/MouseSdk/MouseSdk/Ble/HidManager.h +++ b/MouseSdk/MouseSdk/Ble/HidManager.h @@ -12,9 +12,6 @@ NS_ASSUME_NONNULL_BEGIN @interface HidManager : NSObject -@property (nonatomic,copy)void (^connectStateCallBack)(BOOL isConnnect);/// 设备是否连上 -/// -/// @property (nonatomic,copy)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 @property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(BOOL connected);/// 设备连接状态回调 @property (nonatomic, copy)void (^aiMouseSendDeviceInfoDataCallBack)(NSData *data);/// 设备信息数据回调 @@ -31,14 +28,11 @@ NS_ASSUME_NONNULL_BEGIN - (void)open; - (void)close; -///开始尝试连接usb -- (void)connectHidDevice; /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; /// 获取信号强度 - (void)getRSSI:(void (^)(NSString *value,NSError *error))complete; -///// 开始鉴权 -//-(void)startAuthentication; + @end NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/Ble/HidManager.m b/MouseSdk/MouseSdk/Ble/HidManager.m index d145387..d2b9c2c 100644 --- a/MouseSdk/MouseSdk/Ble/HidManager.m +++ b/MouseSdk/MouseSdk/Ble/HidManager.m @@ -8,17 +8,12 @@ #import "HidManager.h" #include #include - #import "BleManager.h" -////供应商id -//const long vendorID = 0x6675; -////产品id -//const long productID = 0x8601; @interface HidManager () { - dispatch_block_t _block; + } @property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理 @property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道 @@ -39,31 +34,11 @@ - (instancetype)init{ self = [super init]; if (self) { - _block = dispatch_block_create(0, ^{ - //执行超时操作 - [self showLog:(@"(HidManager)执行超时操作 ")]; - //连接超时走这里,返回失败 - if (self.deviceInfoCha == nil && self.connectStateCallBack != nil) { - [self showLog:(@"(HidManager)执行超时操作成功,回调连接失败回调 ")]; - if(self.connectStateCallBack){ - self.connectStateCallBack(false); - } - } - }); - } - return self; -} -///开始尝试连接usb,其实就是设置一个超时时间,要是超时了,就返回连接失败,防止一直得不到结果 -- (void)connectHidDevice { - //设备是链接状态 - if (self.deviceInfoCha != nil) { - return; } - dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(15 * NSEC_PER_SEC)), dispatch_get_main_queue(), _block); + return self; } - - (void)open{ //初始化 self.deviceInfoCha = nil; @@ -78,6 +53,10 @@ [self showLog:(@"(HidManager)open:打开hid成功!")]; } //设置匹配设备 + ////供应商id + //const long vendorID = 0x6675; + ////产品id + //const long productID = 0x8601; //供应商id const long vendorID = 0x10D6; //产品id @@ -127,12 +106,7 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH [[TlwMouse shared] switchConnect:AiMouseConnectType_hid]; [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice:回调连接成功 ")]; - [[HidManager shared] cancelBlock]; - - //连上设备 - if ([HidManager shared].connectStateCallBack != nil) { - [HidManager shared].connectStateCallBack(true); - } + //注册消息回调 unsigned char InputBuffer[64]; IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); @@ -144,10 +118,10 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH - (void)openDiveceAction{ IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone); if (result != kIOReturnSuccess) { - NSLog([NSString stringWithFormat:@"打开设备信息失败! %d",result]); + NSLog([NSString stringWithFormat:@"打开hid失败! %d",result]); [self performSelector:@selector(openDiveceAction) withObject:nil afterDelay:2]; }else{ - NSLog(@"打开设备信息成功!"); + NSLog(@"打开hid成功!"); } CFRetain([HidManager shared].deviceInfoCha); IOHIDDeviceScheduleWithRunLoop([HidManager shared].deviceInfoCha, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); @@ -163,20 +137,14 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender, [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidDisAppearDevice:拔出设备 %@ ",inIOHIDDeviceRef]]; //取消执行超时操作 [[HidManager shared] showLog:(@"(HidManager)hidDisAppearDevice:回调连接失败 ")]; - [[HidManager shared] cancelBlock]; - if ([HidManager shared].connectStateCallBack != nil) { - [HidManager shared].connectStateCallBack(false); - } [HidManager shared].deviceInfoCha = nil; -// [[HidManager shared] aiMouseConnectStateDidChange:false]; + [[HidManager shared] tlwMouseConnectStateDidChange:false]; [[TlwMouse shared] didDisconnect]; } -- (void)cancelBlock { - dispatch_block_cancel(_block); // 取消定时的执行 -} + static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){ if (report[0] == 0x01 || report[0] == 0x02) { @@ -312,20 +280,6 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe } #pragma mark - public -//-(void)startAuthentication{ -// //设置代理 -// [TlwMouse shared].delegate = self; -// //切换当前连接类型 -// [[TlwMouse shared] switchConnect:AiMouseConnectType_hid]; -// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 -// [self showLog:@"(HidManager)开始鉴权"]; -// [[TlwMouse shared] startAuthentication:^(AiMouseAuthState state) { -// [self showLog:[NSString stringWithFormat:@"(HidManager)鉴权结果 = %@",@(state)]]; -// if(self.authCallBack){ -// self.authCallBack(state == AiMouseAuthState_Success); -// } -// }]; -//} /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{ [self showLog:@"(HidManager)getDeviceInfo: 获取设备信息"]; @@ -335,22 +289,10 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; info.CPI_list = @"1,2,3,4"; info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; - info.brand = @""; + info.brand = @"NNMouse"; info.sn = @""; info.mac = @""; completation(info,nil); -// @property (nonatomic, assign) int DPI;/// DPI值 数值1 ~ 4 (分别对应 800 1200 1800 2400) -// @property (nonatomic, assign) int version;///软件版本 -// @property (nonatomic, assign) int batteryState;/// 电池状态 -// @property (nonatomic, assign) int batteryValue;/// 电池百分百电量,0-100 -// /// -// @property (nonatomic, strong) NSString *battery_power;/// 电池百分百电量,0-100 -// @property (nonatomic, strong) NSString *CPI;/// CPI值 -// @property (nonatomic, strong) NSString *CPI_list;/// 可设置的CPI参数列表 -// @property (nonatomic, strong) NSString *version;///软件版本 -// @property (nonatomic, strong) NSString *brand;/// 品牌名称 -// @property (nonatomic, strong) NSString *sn;/// 设备序列号 -// @property (nonatomic, strong) NSString *mac;///设备MAC地址 }]; } @@ -400,19 +342,4 @@ static inline int get_data_len(uint8_t *buf, u_long len) { return url_len; } -/// 获取对应属性值 -static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStringRef inKey) { - uint32_t outValue = 0; - if (inIOHIDDeviceRef != NULL) { - CFTypeRef tCFTypeRef = IOHIDDeviceGetProperty(inIOHIDDeviceRef, inKey); - if (tCFTypeRef != NULL && (CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef)) ) { - Boolean result = CFNumberGetValue((CFNumberRef) tCFTypeRef, kCFNumberSInt32Type, &outValue); - if (!result) { - [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)IOHIDDevGetUInt32Property:get %@ failed.", inKey]]; - } - } - } - return outValue; -} - @end diff --git a/MouseSdk/MouseSdk/Tlw/TlwHidManager.m b/MouseSdk/MouseSdk/Tlw/TlwHidManager.m index 23b9d6d..d3ff2b7 100644 --- a/MouseSdk/MouseSdk/Tlw/TlwHidManager.m +++ b/MouseSdk/MouseSdk/Tlw/TlwHidManager.m @@ -90,9 +90,9 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); IOReturn result = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone); if (result != kIOReturnSuccess) { - NSLog([NSString stringWithFormat:@"打开设备信息失败! %d",result]); + NSLog([NSString stringWithFormat:@"打开hid失败! %d",result]); }else{ - NSLog(@"打开设备信息成功!"); + NSLog(@"打开hid成功!"); } CFRetain(inIOHIDDeviceRef); IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode);