From: liangzhenying <867174124@qq.com> Date: Mon, 26 Aug 2024 09:10:28 +0000 (+0800) Subject: 修复usb模式连接失败问题 X-Git-Tag: 千寻适配d9~39 X-Git-Url: http://git.snicker.com.cn/css/static/gitweb.css?a=commitdiff_plain;h=f11b6b71f9d923b1f6cca31a0a3f120798c5c5d9;p=mirrors%2FMouseSdk-MacOS.git 修复usb模式连接失败问题 --- diff --git a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h index 022cf89..66a0379 100644 --- a/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h +++ b/MouseSdk/AiMouseSdk/AiMouseSdk/AiMouseSdk.h @@ -32,7 +32,7 @@ typedef NS_ENUM(NSInteger,AiMouseLogLevel) { typedef NS_ENUM(NSInteger,AiMouseConnectType) { AiMouseConnectType_bluetooth = 0,///蓝牙 - AiMouseConnectType_hid,///hid + AiMouseConnectType_hid = 1,///hid }; typedef NS_ENUM(NSInteger,AiMouseAuthState) { diff --git a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h index 06a012e..6800dc3 100644 --- a/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h +++ b/MouseSdk/AiMouseSdk/TlwMouseSdk/TlwMouseSdk.h @@ -13,6 +13,7 @@ @property (nonatomic, assign) int version;///软件版本 @property (nonatomic, assign) int batteryState;/// 电池状态 @property (nonatomic, assign) int batteryValue;/// 电池百分百电量,0-100 + @end /// 日志等级 diff --git a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj index 96b520a..b4f9ef1 100644 --- a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj @@ -14,6 +14,7 @@ 180E7D192BD90029008558E8 /* VideoToolbox.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18F324A52B93385100CABA2E /* VideoToolbox.framework */; }; 180E7D1A2BD90032008558E8 /* CoreAudio.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18F325772B95737F00CABA2E /* CoreAudio.framework */; }; 180E7D1B2BD9005D008558E8 /* liblzma.5.tbd in Frameworks */ = {isa = PBXBuildFile; fileRef = 18CB080B2BA401D0005F5854 /* liblzma.5.tbd */; }; + 181F976D2C7C6E11002FCF68 /* AiHidManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 181F976B2C7C6E10002FCF68 /* AiHidManager.m */; }; 182B548D2BB7EC9B009F53B6 /* DeviceManger.m in Sources */ = {isa = PBXBuildFile; fileRef = 182B548C2BB7EC9B009F53B6 /* DeviceManger.m */; }; 185EBE042C79D16600B2503B /* TimerManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 185EBE032C79D16600B2503B /* TimerManager.m */; }; 1867E5A92BB2706F008FC5FB /* HidManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 1867E5A82BB2706F008FC5FB /* HidManager.m */; }; @@ -28,7 +29,6 @@ 18BF1E222B83066200B8D90F /* BleManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E212B83066200B8D90F /* BleManager.m */; }; 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E752B832D2E00B8D90F /* ScanVC.m */; }; 18CC59962C787AAB0009FCC8 /* DeviceManager2.m in Sources */ = {isa = PBXBuildFile; fileRef = 18CC59952C787AAB0009FCC8 /* DeviceManager2.m */; }; - 18EF7B0A2C7605BF00AC5223 /* MouseSdkManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7B092C7605BF00AC5223 /* MouseSdkManager.m */; }; 18EF7B0D2C76079E00AC5223 /* TlwVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7B0C2C76079E00AC5223 /* TlwVC.m */; }; 18EF7B102C7607C100AC5223 /* FrkVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7B0F2C7607C100AC5223 /* FrkVC.m */; }; 18EF7B172C760A5B00AC5223 /* TlwBleManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18EF7B122C760A5A00AC5223 /* TlwBleManager.m */; }; @@ -74,6 +74,8 @@ /* Begin PBXFileReference section */ 0311DF3AD67ED792549B96BD /* libPods-MouseSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-MouseSdk.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 180E7AB22BD7BBD9008558E8 /* libAiMouseSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; path = libAiMouseSdk.a; sourceTree = BUILT_PRODUCTS_DIR; }; + 181F976B2C7C6E10002FCF68 /* AiHidManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AiHidManager.m; sourceTree = ""; }; + 181F976C2C7C6E10002FCF68 /* AiHidManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AiHidManager.h; sourceTree = ""; }; 182B548B2BB7EC9B009F53B6 /* DeviceManger.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeviceManger.h; sourceTree = ""; }; 182B548C2BB7EC9B009F53B6 /* DeviceManger.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DeviceManger.m; sourceTree = ""; }; 185EBE022C79D16600B2503B /* TimerManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TimerManager.h; sourceTree = ""; }; @@ -103,8 +105,6 @@ 18CB080B2BA401D0005F5854 /* liblzma.5.tbd */ = {isa = PBXFileReference; lastKnownFileType = "sourcecode.text-based-dylib-definition"; name = liblzma.5.tbd; path = usr/lib/liblzma.5.tbd; sourceTree = SDKROOT; }; 18CC59942C787AAB0009FCC8 /* DeviceManager2.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = DeviceManager2.h; sourceTree = ""; }; 18CC59952C787AAB0009FCC8 /* DeviceManager2.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = DeviceManager2.m; sourceTree = ""; }; - 18EF7B082C7605BF00AC5223 /* MouseSdkManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = MouseSdkManager.h; sourceTree = ""; }; - 18EF7B092C7605BF00AC5223 /* MouseSdkManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkManager.m; sourceTree = ""; }; 18EF7B0B2C76079E00AC5223 /* TlwVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = TlwVC.h; sourceTree = ""; }; 18EF7B0C2C76079E00AC5223 /* TlwVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = TlwVC.m; sourceTree = ""; }; 18EF7B0E2C7607C100AC5223 /* FrkVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = FrkVC.h; sourceTree = ""; }; @@ -194,10 +194,10 @@ 18EF7B072C7604BF00AC5223 /* Ble */, 18EF7B062C7604AA00AC5223 /* Tlw */, 18C31D6F2BBAA6CC0089D656 /* Info.plist */, + 181F976C2C7C6E10002FCF68 /* AiHidManager.h */, + 181F976B2C7C6E10002FCF68 /* AiHidManager.m */, 18BF1DCD2B81AB7C00B8D90F /* AppDelegate.h */, 18BF1DCE2B81AB7C00B8D90F /* AppDelegate.m */, - 18EF7B082C7605BF00AC5223 /* MouseSdkManager.h */, - 18EF7B092C7605BF00AC5223 /* MouseSdkManager.m */, 18BF1DD02B81AB7C00B8D90F /* ViewController.h */, 18BF1DD12B81AB7C00B8D90F /* ViewController.m */, 18EF7B0E2C7607C100AC5223 /* FrkVC.h */, @@ -456,6 +456,7 @@ 18EF7B192C760A5B00AC5223 /* TlwDeviceManger.m in Sources */, 18F3235D2B8ED78300CABA2E /* ScanCell.m in Sources */, 18BF1DD22B81AB7C00B8D90F /* ViewController.m in Sources */, + 181F976D2C7C6E11002FCF68 /* AiHidManager.m in Sources */, 18BF1DD92B81AB7F00B8D90F /* main.m in Sources */, 18EF7B172C760A5B00AC5223 /* TlwBleManager.m in Sources */, 185EBE042C79D16600B2503B /* TimerManager.m in Sources */, @@ -467,7 +468,6 @@ 18EF7B102C7607C100AC5223 /* FrkVC.m in Sources */, 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */, 18EF7B1D2C760FF700AC5223 /* TlwScanVC.m in Sources */, - 18EF7B0A2C7605BF00AC5223 /* MouseSdkManager.m in Sources */, 18BF1E222B83066200B8D90F /* BleManager.m in Sources */, 18BF1DCF2B81AB7C00B8D90F /* AppDelegate.m in Sources */, 1867E5A92BB2706F008FC5FB /* HidManager.m in Sources */, @@ -645,6 +645,7 @@ CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = M33X987285; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GENERATE_INFOPLIST_FILE = YES; HEADER_SEARCH_PATHS = ( @@ -668,7 +669,7 @@ ); MACOSX_DEPLOYMENT_TARGET = 11.0; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk.mix; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk; PRODUCT_NAME = AiMouseSdk; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_OBJC_BRIDGING_HEADER = "MouseSdk/MouseSdk-Bridging-Header.h"; @@ -693,6 +694,7 @@ CODE_SIGN_STYLE = Automatic; COMBINE_HIDPI_IMAGES = YES; CURRENT_PROJECT_VERSION = 1; + DEVELOPMENT_TEAM = M33X987285; FRAMEWORK_SEARCH_PATHS = "$(inherited)"; GENERATE_INFOPLIST_FILE = YES; HEADER_SEARCH_PATHS = ( @@ -717,7 +719,7 @@ ); MACOSX_DEPLOYMENT_TARGET = 11.0; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk.mix; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk; PRODUCT_NAME = AiMouseSdk; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_OBJC_BRIDGING_HEADER = "MouseSdk/MouseSdk-Bridging-Header.h"; diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 437539d..a4fbdf8 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/AiHidManager.h b/MouseSdk/MouseSdk/AiHidManager.h new file mode 100644 index 0000000..7683c2c --- /dev/null +++ b/MouseSdk/MouseSdk/AiHidManager.h @@ -0,0 +1,19 @@ +// +// HidManager.h +// MouseSdk +// +// Created by incar on 2024/3/26. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface AiHidManager : NSObject +@property (nonatomic,strong)void (^connectStateCallBack)(BOOL isConnnect);/// 设备是否连上 + ++(AiHidManager *)shared; + +@end + +NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/AiHidManager.m b/MouseSdk/MouseSdk/AiHidManager.m new file mode 100644 index 0000000..2fcf486 --- /dev/null +++ b/MouseSdk/MouseSdk/AiHidManager.m @@ -0,0 +1,154 @@ +// +// HidManager.m +// MouseSdk +// +// Created by incar on 2024/3/26. +// + +#import "AiHidManager.h" +#include +#include + + + +//供应商id +//const long vendorID = 0x6675; +//const long productID = 0x8601; + +@interface AiHidManager () + +@property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理 +@property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道 +@property (nonatomic,strong) NSMutableData *recordData;//录音的数据 +@end + +@implementation AiHidManager ++(AiHidManager *)shared{ + static AiHidManager *manager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + manager = [[AiHidManager alloc]init]; + }); + return manager; +} + +- (instancetype)init{ + self = [super init]; + if (self) { + [self initHid]; + [self open]; + } + return self; +} + +- (void)initHid{ + +} + +- (void)open{ + self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); + IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); + if (ret != kIOReturnSuccess) { + NSLog(@"打开设备失败!"); + }else{ + NSLog(@"打开设备成功!"); + } + //供应商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]]; + IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict); + //设置匹配到设备回调 + IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL); + //设置移除设备回调 + IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL); +} + +- (void)close{ + IOReturn ret = IOHIDManagerClose(self.hidManager, 0); + if (ret != kIOReturnSuccess) { + NSLog(@"关闭设备失败!"); + }else{ + NSLog(@"关闭设备成功!"); + } +} + +#pragma mark - Hid CallBack +/// 匹配到设备回调 +static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { + [[AiHidManager shared] showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]]; +// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ + //设备信息 + [AiHidManager shared].deviceInfoCha = inIOHIDDeviceRef; + //注册消息回调 + unsigned char InputBuffer[128]; + IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); + IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, kIOHIDOptionsTypeNone); + if (ret != kIOReturnSuccess) { + NSLog(@"打开设备信息失败!"); + }else{ + NSLog(@"打开设备信息成功!"); + } + CFRetain(inIOHIDDeviceRef); + IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); +// } +// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ +// //ai +// [AiHidManager shared].aiCha = inIOHIDDeviceRef; +// //注册消息回调 +// unsigned char InputBuffer[128]; +// IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); +// IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0); +// if (ret != kIOReturnSuccess) { +// NSLog(@"打开AI失败!"); +// }else{ +// NSLog(@"打开AI成功!"); +// } +// CFRetain(inIOHIDDeviceRef); +// IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); +// } +} + +/// 设备移除回调 +static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { + [[AiHidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]]; +// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); +// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); +// +// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ +// [AiHidManager shared].deviceInfoCha = nil; +//// [AiHidManager shared].aiCha = nil; +// [[AiMouse shared] didDisconnect]; +// } +} + +static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){ +// [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[AiMouseTools dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]]; + +// NSInteger len = get_data_len(report, 64); + NSData *data = [[NSData alloc] initWithBytes:report length:64]; + [[AiHidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理后 %@",data]]; + +// uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey)); +// uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey)); +// +// if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ +// [[AiMouse shared] sendDeviceDataToSdk:data]; +// +// } +// else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ +// [[AiMouse shared] sendAiDataToSdk:data]; +// } +} + + +- (void)showLog:(NSString *)log{ + NSLog(@"%@",log); +} + +@end diff --git a/MouseSdk/MouseSdk/Ble/BleManager.m b/MouseSdk/MouseSdk/Ble/BleManager.m index ed718ee..be15414 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.m +++ b/MouseSdk/MouseSdk/Ble/BleManager.m @@ -170,8 +170,10 @@ - (void)stopScan{ - [self showLog:[NSString stringWithFormat:@"ble:停止扫描"]]; - [self.centralManager stopScan]; + if (self.centralManager.isScanning) { + [self showLog:[NSString stringWithFormat:@"ble:停止扫描"]]; + [self.centralManager stopScan]; + } } /// 连接设备 @@ -183,14 +185,12 @@ -(void)writeToDevice:(NSData *)data{ if (self.crrentPeripheral ==nil) { - [self showLog:@"peripheral 为空"]; return; } if (self.deviceInfoCha ==nil) { - [self showLog:@"characteristic 为空"]; return; } - + [self showLog:[NSString stringWithFormat:@"向蓝牙设备发送数据 %@",data]]; [self.crrentPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithResponse]; } @@ -211,7 +211,8 @@ // [self performSelector:@selector(autoConnectDevice) withObject:nil afterDelay:10]; //} - (void)showLog:(NSString *)log{ - if(self.logCallBack)self.logCallBack(log); + NSString *text = [NSString stringWithFormat:@"[BleManager] %@",log]; + if(self.logCallBack) self.logCallBack(log); } //将录音数据保存 diff --git a/MouseSdk/MouseSdk/Ble/DeviceManger.m b/MouseSdk/MouseSdk/Ble/DeviceManger.m index b7ac399..1249629 100644 --- a/MouseSdk/MouseSdk/Ble/DeviceManger.m +++ b/MouseSdk/MouseSdk/Ble/DeviceManger.m @@ -57,6 +57,11 @@ if (connected) { [[TlwMouse shared] switchConnect:AiMouseConnectType_hid isConnected:connected]; } + else{ + if ([TlwMouse shared].connectType == AiMouseConnectType_hid && [TlwMouse shared].connected){ + [[TlwMouse shared] switchConnect:AiMouseConnectType_hid isConnected:false]; + } + } if(ws.authCallBack && connected)ws.authCallBack(true); if (ws.aiMouseConnectStateDidChangeCallBack) { @@ -73,9 +78,6 @@ self.hid.logCallBack = ^(NSString * _Nonnull log) { [ws showLog:log]; - if (ws.aiMouseAddLogCallBack) { - ws.aiMouseAddLogCallBack(log); - } }; } @@ -84,6 +86,7 @@ __weak typeof(self) ws = self; self.ble.connectStateCallBack = ^(BOOL connected) { + [[TlwMouse shared] switchConnect:AiMouseConnectType_bluetooth isConnected:connected]; if(ws.authCallBack && connected)ws.authCallBack(true); @@ -100,9 +103,6 @@ self.ble.logCallBack = ^(NSString * _Nonnull log) { [ws showLog:log]; - if (ws.aiMouseAddLogCallBack) { - ws.aiMouseAddLogCallBack(log); - } }; } @@ -125,7 +125,7 @@ // [self showLog:@"定时器。。。"]; if([TlwMouse shared].connected){ [self.ble stopScan]; - [self.hid close]; +// [self.hid close]; } else{ [self.ble startScanWithName:mouseName]; @@ -136,14 +136,22 @@ ///获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation{ [[TlwMouse shared] 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.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; - info.CPI_list = @"1,2,3,4"; info.version = [NSString stringWithFormat:@"%d",deviceInfo.version]; +// info.CPI_list = CPI_list;// 1-4,800 1200 1800 2400 +// info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; + info.CPI_list = @"1,2,3,4";// 1-4,800 1200 1800 2400 + info.CPI = [NSString stringWithFormat:@"%d",deviceInfo.DPI]; info.brand = @"NNMouse"; info.sn = @"MSCL1BKZ12105000042371"; info.mac = @"C0310000A583"; + + [self showLog:info.description]; completation(info,nil); }]; } @@ -231,7 +239,13 @@ /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 /// - Parameter data: 数据 -(void)tlwMouseSendDeviceInfoData:(NSData *)data{ - [self.ble writeToDevice:data]; + if ([TlwMouse shared].connectType == AiMouseConnectType_bluetooth) { + [self.ble writeToDevice:data]; + } + else if ([TlwMouse shared].connectType == AiMouseConnectType_hid){ + [self.hid writeToDevice:data]; + } + if (self.aiMouseSendDeviceInfoDataCallBack) { // 将NSData转换为Base64编码的字符串 NSString *base64String = [data base64EncodedStringWithOptions:0]; @@ -291,14 +305,14 @@ } if(cmd == 0x31 || cmd == 0x21 || cmd == 0x25 ||cmd == 0x27){ - [self showLog:@"(BleManager) aiMouseSendCmdData:开始录音"]; + [self showLog:@"开始录音"]; self.recordData = [[NSMutableData alloc] init]; if (self.begainRecoredCallBack) { self.begainRecoredCallBack(); } } else if(cmd == 0x32 || cmd == 0x22 || cmd == 0x26 ||cmd == 0x28){ - [self showLog:@"(BleManager) aiMouseSendCmdData:结束录音"]; + [self showLog:@"结束录音"]; [self finishRecored:self.recordData]; if (self.finishRecoredCallBack) { // 将NSData转换为Base64编码的字符串 @@ -327,10 +341,10 @@ // 连接状态改变 - (void)tlwMouseConnectStateDidChange:(BOOL)connected{ - [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseConnectStateDidChange - 设备连接状态返回: %hhd",connected]]; + [self showLog:[NSString stringWithFormat:@"设备连接状态返回: %hhd",connected]]; if (self.aiMouseConnectStateDidChangeCallBack) { - self.aiMouseConnectStateDidChangeCallBack(self.getCurrentConnectType,connected); + self.aiMouseConnectStateDidChangeCallBack(self.getCurrentConnectType.intValue,connected); } if (connected == true) { self.authCallBack(true); @@ -338,7 +352,8 @@ } - (void)showLog:(NSString *)log{ - NSLog(@"%@",log); + NSLog(@"[AiMouseSdk] %@",log); + [self aiMouseAddLog:log]; } //将录音数据保存 - (void)finishRecored:(NSData *)data{ diff --git a/MouseSdk/MouseSdk/Ble/HidManager.m b/MouseSdk/MouseSdk/Ble/HidManager.m index 09572e1..05a19f5 100644 --- a/MouseSdk/MouseSdk/Ble/HidManager.m +++ b/MouseSdk/MouseSdk/Ble/HidManager.m @@ -35,9 +35,10 @@ #pragma mark - Hid CallBack /// 匹配到设备回调 -static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { +//static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { +void hidAppearDevice(void *inContext, IOReturn inResult, void *inSender, IOHIDDeviceRef inIOHIDDeviceRef){ [HidManager shared].deviceInfoCha = inIOHIDDeviceRef; - [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice:回调连接成功 ")]; + [[HidManager shared] showLog:([NSString stringWithFormat:@"发现设备 %@",inIOHIDDeviceRef])]; //注册消息回调 unsigned char InputBuffer[64]; @@ -45,9 +46,9 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH IOReturn result = IOHIDDeviceOpen([HidManager shared].deviceInfoCha, kIOHIDOptionsTypeNone); if (result != kIOReturnSuccess) { - NSLog([NSString stringWithFormat:@"打开设备失败! %d",result]); + [[HidManager shared] showLog:[NSString stringWithFormat:@"打开设备失败! %d",result]]; }else{ - NSLog(@"打开设备成功!"); + [[HidManager shared] showLog:@"打开设备成功!"]; [HidManager shared].connectStateCallBack(true) ; } CFRetain([HidManager shared].deviceInfoCha); @@ -55,20 +56,16 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH } /// 设备移除回调 -static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { - - [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidDisAppearDevice:拔出设备 %@ ",inIOHIDDeviceRef]]; - //取消执行超时操作 - [[HidManager shared] showLog:(@"(HidManager)hidDisAppearDevice:回调连接失败 ")]; +void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { + [[HidManager shared] showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]]; [HidManager shared].deviceInfoCha = nil; - [HidManager shared].connectStateCallBack(false) ; } - -static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){ + void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDReportType type, uint32_t reportID, uint8_t *report,CFIndex reportLength){ +// [[HidManager shared] showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[[HidManager shared] dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]]; if (report[0] == 0x01 || report[0] == 0x02) { //鼠标键盘活动事件 return; @@ -76,7 +73,7 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe NSInteger len = get_data_len(report, reportLength); NSData *data = [[NSData alloc] initWithBytes:report length:len]; - [[HidManager shared] showLog:@"(HidManager)hidReceiveData:device数据"]; +// [[HidManager shared] showLog:@"hidReceiveData:device数据"]; [HidManager shared].receiveInfoDataCallBack(data); } @@ -90,15 +87,16 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe #pragma mark - public - (void)open{ + [self close]; //初始化 self.deviceInfoCha = nil; self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); if (ret != kIOReturnSuccess) { - [self showLog:(@"(HidManager)open:打开hid失败!")]; + [self showLog:(@"打开hid失败!")]; }else{ - [self showLog:(@"(HidManager)open:打开hid成功!")]; + [self showLog:(@"打开hid成功!")]; } //设置匹配设备 ////供应商id @@ -114,45 +112,46 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe [dict setValue:[NSNumber numberWithLong:vendorID] forKey:[NSString stringWithCString:kIOHIDVendorIDKey encoding:NSUTF8StringEncoding]]; IOHIDManagerSetDeviceMatching(self.hidManager, (__bridge CFMutableDictionaryRef)dict); //设置匹配到设备回调 - IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, &hidAppearDevice, NULL); + IOHIDManagerRegisterDeviceMatchingCallback(self.hidManager, hidAppearDevice, NULL); //设置移除设备回调 IOHIDManagerRegisterDeviceRemovalCallback(self.hidManager, &hidDisAppearDevice, NULL); } - (void)close{ if (self.hidManager == nil) { - [self showLog:@"(HidManager)close:设备已关闭"]; return; } - [self showLog:@"(HidManager)close:关闭设备"]; + [self showLog:@"关闭hid"]; IOReturn ret = IOHIDManagerClose(self.hidManager, 0); self.hidManager = nil; if (ret != kIOReturnSuccess) { - [self showLog:(@"(HidManager)close:关闭设备失败!")]; + [self showLog:(@"关闭hid失败!")]; }else{ - [self showLog:(@"(HidManager)close:关闭设备成功!")]; + [self showLog:(@"关闭hid成功!")]; } self.deviceInfoCha = nil; } /// 发送数据给设备 -(void)writeToDevice:(NSData *)data{ if(self.deviceInfoCha == nil){ - [self showLog:@"(HidManager)sendData: 请先连上设备"]; + [self showLog:@"sendData: 请先连上设备"]; return; } + [self showLog:[NSString stringWithFormat:@"向usb设备发送数据 %@",data]]; IOReturn ret = IOHIDDeviceSetReport(self.deviceInfoCha, kIOHIDReportTypeOutput, 0, data.bytes, data.length); if (ret != kIOReturnSuccess) { - [self showLog:[NSString stringWithFormat:@"(HidManager)sendData: 发送失败! code = %d",ret]]; + [self showLog:[NSString stringWithFormat:@"发送失败! code = %d",ret]]; }else{ - [self showLog:@"(HidManager)sendData: 发送成功!"]; + [self showLog:@"发送成功!"]; } } - (void)showLog:(NSString *)log{ - NSLog(@"%@",log); + NSString *text = [NSString stringWithFormat:@"[HidManager] %@",log]; + if(self.logCallBack) self.logCallBack(text); } /// 实际到数据长度 @@ -163,4 +162,13 @@ static inline int get_data_len(uint8_t *buf, u_long len) { return url_len; } +- (NSString *)dataToHexString:(NSData *)data { + NSUInteger length = data.length; + NSMutableString *result = [NSMutableString stringWithCapacity:length * 2]; + const unsigned char *byte = data.bytes; + for (int i = 0; i < length; i++, byte++) { + [result appendFormat:@"%02X", *byte]; + } + return result; +} @end diff --git a/MouseSdk/MouseSdk/FrkVC.m b/MouseSdk/MouseSdk/FrkVC.m index 8acb8ea..2fa7e02 100644 --- a/MouseSdk/MouseSdk/FrkVC.m +++ b/MouseSdk/MouseSdk/FrkVC.m @@ -123,7 +123,6 @@ [self presentViewControllerAsModalWindow:vc]; } -/// 获取设备信息 - (void)geviceInfoAction{ [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { if (deviceInfo) { @@ -141,43 +140,30 @@ } }]; } -- (void)showAuthResult:(BOOL)isSuccess{ - dispatch_async(dispatch_get_main_queue(), ^{ - self.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败"; - if(isSuccess){ - [self geviceInfoAction]; - } - }); -} -- (void)connectStateDidChange:(AiMouseConnectType)type connectState:(BOOL)isConnected{ - dispatch_async(dispatch_get_main_queue(), ^{ - if(isConnected){ - self.typeLabel.stringValue = type == AiMouseConnectType_bluetooth ?@"连接模式:蓝牙":@"连接模式:hid"; - self.connectLabel.stringValue = isConnected?@"连接状态:已连接":@"连接状态:未连接"; - [self geviceInfoAction]; - } - else{ - self.typeLabel.stringValue = @"连接模式:--"; - self.connectLabel.stringValue = @"连接状态:未连接"; - } - }); -} - (void)setupDeviceManger{ DeviceManger *manager = [DeviceManger shared]; __weak typeof(self) ws = self; manager.authCallBack = ^(BOOL isSuccess) { - [ws showAuthResult:isSuccess]; + dispatch_async(dispatch_get_main_queue(), ^{ + ws.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败"; + if(isSuccess){ + [ws geviceInfoAction]; + } + }); }; -// manager.connectStateCallBack = ^(AiMouseConnectType type,BOOL isConnected) { -// [ws connectStateDidChange:type connectState:isConnected]; -// }; - -// [manager begainMonitorAiMouseConnectStateDidChange:@"VMouse" callBack:^(int connectType, BOOL connected) { -// [ws connectStateDidChange:connectType connectState:connected]; -// }]; [manager begainMonitorAiMouseConnectStateDidChange:@"NMMouse" callBack:^(int connectType, BOOL connected) { - [ws connectStateDidChange:connectType connectState:connected]; + dispatch_async(dispatch_get_main_queue(), ^{ + if(connected){ + ws.typeLabel.stringValue = connectType == 0 ?@"连接模式:蓝牙":@"连接模式:hid"; + ws.connectLabel.stringValue = connected?@"连接状态:已连接":@"连接状态:未连接"; + [ws geviceInfoAction]; + } + else{ + ws.typeLabel.stringValue = @"连接模式:--"; + ws.connectLabel.stringValue = @"连接状态:未连接"; + } + }); }]; } diff --git a/MouseSdk/MouseSdk/MouseSdkManager.h b/MouseSdk/MouseSdk/MouseSdkManager.h deleted file mode 100644 index e984e2e..0000000 --- a/MouseSdk/MouseSdk/MouseSdkManager.h +++ /dev/null @@ -1,24 +0,0 @@ -// -// MouseSdkManager.h -// MouseSdk -// -// Created by incar on 2024/8/21. -// - -#import - -typedef NS_ENUM(NSInteger,MouseSdkType) { - TlwMouseLogLevel_frk = 1,//富瑞坤 - TlwMouseLogLevel_tlw = 2,//泰凌微 -}; - -NS_ASSUME_NONNULL_BEGIN - - -@interface MouseSdkManager : NSObject -@property (nonatomic,assign)MouseSdkType type;//默认是富瑞坤 - -+(MouseSdkManager *)shared; -@end - -NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/MouseSdkManager.m b/MouseSdk/MouseSdk/MouseSdkManager.m deleted file mode 100644 index ec8fda1..0000000 --- a/MouseSdk/MouseSdk/MouseSdkManager.m +++ /dev/null @@ -1,27 +0,0 @@ -// -// MouseSdkManager.m -// MouseSdk -// -// Created by incar on 2024/8/21. -// - -#import "MouseSdkManager.h" - -@implementation MouseSdkManager -+(MouseSdkManager *)shared{ - static MouseSdkManager *manager = nil; - static dispatch_once_t oncePredicate; - dispatch_once(&oncePredicate, ^{ - manager = [[MouseSdkManager alloc]init]; - }); - return manager; -} - -- (instancetype)init{ - self = [super init]; - if (self) { - self.type = TlwMouseLogLevel_frk; - } - return self; -} -@end diff --git a/MouseSdk/MouseSdk/Tlw/TlwHidManager.m b/MouseSdk/MouseSdk/Tlw/TlwHidManager.m index e7a916f..3beeafb 100644 --- a/MouseSdk/MouseSdk/Tlw/TlwHidManager.m +++ b/MouseSdk/MouseSdk/Tlw/TlwHidManager.m @@ -111,6 +111,7 @@ static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender, } 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) { //鼠标键盘活动事件 return; diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index ca849f7..3d39ab4 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -5,64 +5,284 @@ // Created by incar on 2024/2/18. // +//#import "ViewController.h" +//#import "FrkVC.h" +//#import "TlwVC.h" +//#import "ScanVC.h" +//#import "TlwHidManager.h" +//#import "HidManager.h" +//@interface ViewController () +//@end +// +//@implementation ViewController +// +//- (void)viewDidLoad { +// [super viewDidLoad]; +// +// // Do any additional setup after loading the view. +// [self setupUI]; +//} +// +//- (void)setRepresentedObject:(id)representedObject { +// [super setRepresentedObject:representedObject]; +// +// // Update the view, if already loaded. +//} +// +//- (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 = @"富瑞坤"; +// [btn setTarget:self]; +// [btn setAction:@selector(frkAction)]; +// btn.state = 1; +// [self.view addSubview:btn]; +// } +// { +// NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 200, 100, 45)]; +// btn.bordered = true; +// btn.title = @"泰凌微"; +// [btn setTarget:self]; +// [btn setAction:@selector(tlwAction)]; +// btn.state = 1; +// [self.view addSubview:btn]; +// } +//} +// +//#pragma mark - action +//- (void)frkAction{ +// [HidManager shared]; +// FrkVC *vc = [[FrkVC alloc] initWithNibName:nil bundle:nil]; +// [self presentViewControllerAsModalWindow:vc]; +//} +//- (void)tlwAction{ +//// [TlwHidManager shared]; +//// return; +// TlwVC *vc = [[TlwVC alloc] initWithNibName:nil bundle:nil]; +// [self presentViewControllerAsModalWindow:vc]; +// +//} +//@end +// +// FrkVC.m +// MouseSdk +// +// Created by incar on 2024/8/21. +// + #import "ViewController.h" -#import "FrkVC.h" -#import "TlwVC.h" #import "ScanVC.h" -#import "TlwHidManager.h" -#import "HidManager.h" +#import "DeviceManger.h" +#import "AiHidManager.h" @interface ViewController () +@property(nonatomic,strong)NSTextField *typeLabel;///当前模式状态 +@property(nonatomic,strong)NSTextField *connectLabel;///连接状态 +@property(nonatomic,strong)NSTextField *authLabel;///鉴权状态 +@property(nonatomic,strong)NSTextField *deviceInfoLabel;///设备信息 @end @implementation ViewController +-(instancetype)init{ + if (self = [super init]) { +// [[DeviceManger shared] start]; + } + return self; +} + +-(void)dealloc{ +// [[DeviceManger shared] stop]; +} + - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. +// [[DeviceManger shared] start]; [self setupUI]; + [self setupDeviceManger]; } -- (void)setRepresentedObject:(id)representedObject { - [super setRepresentedObject:representedObject]; - - // Update the view, if already loaded. +- (void)loadView{ + self.view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; } - (void)setupUI{ + CALayer *viewLayer = [CALayer layer]; + NSView *backgroundView= [[NSView alloc]init]; + [backgroundView setWantsLayer:YES]; + [backgroundView setLayer:viewLayer]; + backgroundView.layer.backgroundColor = [NSColor greenColor].CGColor; + [backgroundView setNeedsDisplay:YES]; + [self.view addSubview:backgroundView]; + self.title = @"AI SDK"; { NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 80, 100, 45)]; btn.bordered = true; - btn.title = @"富瑞坤"; + btn.title = @"蓝牙扫描"; + [btn setTarget:self]; + [btn setAction:@selector(scanAction)]; + btn.state = 1; + [self.view addSubview:btn]; + } + { + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 180, 100, 45)]; + btn.bordered = true; + btn.title = @"获取设备信息"; [btn setTarget:self]; - [btn setAction:@selector(frkAction)]; + [btn setAction:@selector(geviceInfoAction)]; btn.state = 1; [self.view addSubview:btn]; } { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 200, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 280, 100, 45)]; btn.bordered = true; - btn.title = @"泰凌微"; + btn.title = @"更多指令"; [btn setTarget:self]; - [btn setAction:@selector(tlwAction)]; + [btn setAction:@selector(moreAction:)]; btn.state = 1; [self.view addSubview:btn]; } + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 80, 300, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"连接模式:--"; + label.backgroundColor = NSColor.clearColor; + self.typeLabel = label; + [self.view addSubview:label]; + } + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 120, 300, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"连接状态:未连接"; + label.backgroundColor = NSColor.clearColor; + self.connectLabel = label; + [self.view addSubview:label]; + } + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 160, 300, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"鉴权状态:unknow"; + label.backgroundColor = NSColor.clearColor; + self.authLabel = label; + [self.view addSubview:label]; + } + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 260, 300, 100)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @""; + label.backgroundColor = NSColor.clearColor; + self.deviceInfoLabel = label; + [self.view addSubview:label]; + } } #pragma mark - action -- (void)frkAction{ - [HidManager shared]; +- (void)scanAction{ +// [[HidManager shared] open]; // return; - FrkVC *vc = [[FrkVC alloc] initWithNibName:nil bundle:nil]; + ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil]; [self presentViewControllerAsModalWindow:vc]; } -- (void)tlwAction{ -// [TlwHidManager shared]; -// return; - TlwVC *vc = [[TlwVC alloc] initWithNibName:nil bundle:nil]; - [self presentViewControllerAsModalWindow:vc]; - + +- (void)geviceInfoAction{ + [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { + if (deviceInfo) { + NSString *info = deviceInfo.description; + dispatch_async(dispatch_get_main_queue(), ^{ + self.deviceInfoLabel.stringValue = info; + }); + } + else{ + NSString *info = [NSString stringWithFormat:@"error == %@",error.description]; + NSLog(@"%@",info); + dispatch_async(dispatch_get_main_queue(), ^{ + self.deviceInfoLabel.stringValue = info; + }); + } + }]; +} + +- (void)setupDeviceManger{ +// [AiHidManager shared]; + DeviceManger *manager = [DeviceManger shared]; + __weak typeof(self) ws = self; + manager.authCallBack = ^(BOOL isSuccess) { + dispatch_async(dispatch_get_main_queue(), ^{ + ws.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败"; + if(isSuccess){ + [ws geviceInfoAction]; + } + }); + }; + [manager begainMonitorAiMouseConnectStateDidChange:@"NMMouse" callBack:^(int connectType, BOOL connected) { + dispatch_async(dispatch_get_main_queue(), ^{ + if(connected){ + ws.typeLabel.stringValue = connectType == 0 ?@"连接模式:蓝牙":@"连接模式:hid"; + ws.connectLabel.stringValue = connected?@"连接状态:已连接":@"连接状态:未连接"; + [ws geviceInfoAction]; + } + else{ + ws.typeLabel.stringValue = @"连接模式:--"; + ws.connectLabel.stringValue = @"连接状态:未连接"; + } + }); + }]; +} + +- (void)moreAction:(NSButton *)sender { + NSMenu *popupMenu = [[NSMenu alloc] initWithTitle:@"Popup Menu"]; + NSMenuItem *item1 = [[NSMenuItem alloc] initWithTitle:@"语音开启" action:@selector(voiceStart:) keyEquivalent:@"1"]; + NSMenuItem *item2 = [[NSMenuItem alloc] initWithTitle:@"语音停止" action:@selector(voiceStop:) keyEquivalent:@"2"]; + NSMenuItem *item3 = [[NSMenuItem alloc] initWithTitle:@"设置DPI" action:@selector(setDPI:) keyEquivalent:@"3"]; + NSMenuItem *item4 = [[NSMenuItem alloc] initWithTitle:@"鼠标状态查询" action:@selector(searchState:) keyEquivalent:@"3"]; + + [popupMenu addItem:item1]; + [popupMenu addItem:item2]; + [popupMenu addItem:item3]; + [popupMenu addItem:item4]; + + NSPoint buttonLocation = [sender convertPoint:NSZeroPoint toView:self.view]; + [popupMenu popUpMenuPositioningItem:nil atLocation:buttonLocation inView:self.view]; +} + +- (void)voiceStart:(NSMenuItem*)sender { + [[DeviceManger shared] openRecord:^(BOOL isSuccess, NSError * _Nonnull error) { + + }]; +} +- (void)voiceStop:(NSMenuItem*)sender { + [[DeviceManger shared] closeRecord:^(BOOL isSuccess, NSError * _Nonnull error) { + + }]; +} +- (void)setDPI:(NSMenuItem*)sender { + [[DeviceManger shared] setCPI:2 completation:^(BOOL isSuccess, NSError * _Nonnull error) { + + }]; +} +- (void)searchState:(NSMenuItem*)sender { + [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { + if (deviceInfo) { + NSString *info = deviceInfo.description; + dispatch_async(dispatch_get_main_queue(), ^{ + self.deviceInfoLabel.stringValue = info; + }); + } + else{ + NSString *info = [NSString stringWithFormat:@"error == %@",error.description]; + NSLog(@"%@",info); + dispatch_async(dispatch_get_main_queue(), ^{ + self.deviceInfoLabel.stringValue = info; + }); + } + }]; } @end