From: liangzhenying <867174124@qq.com> Date: Sat, 30 Mar 2024 07:57:53 +0000 (+0800) Subject: 自动识别连接模式 X-Git-Tag: 阿里鉴权~6 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=7cbcc522f00cd7712895a89853a14981edbef43a;p=mirrors%2FMouseSdk-MacOS.git 自动识别连接模式 --- diff --git a/MouseSdk/AiMouseSdk/AiMouse.h b/MouseSdk/AiMouseSdk/AiMouse.h index 9f3f9a2..a5e4b9b 100644 --- a/MouseSdk/AiMouseSdk/AiMouse.h +++ b/MouseSdk/AiMouseSdk/AiMouse.h @@ -10,7 +10,7 @@ typedef NS_ENUM(NSInteger,AiMouseConnectType) { AiMouseConnectType_bluetooth = 0,///蓝牙 - AiMouseConnectType_usb,///usb + AiMouseConnectType_hid,///hid }; typedef NS_ENUM(NSInteger,AiMouseAuthState) { diff --git a/MouseSdk/AiMouseSdk/AiMouse.m b/MouseSdk/AiMouseSdk/AiMouse.m index 897d041..25892f5 100644 --- a/MouseSdk/AiMouseSdk/AiMouse.m +++ b/MouseSdk/AiMouseSdk/AiMouse.m @@ -106,7 +106,7 @@ _connectType = type; [self showLog:type == AiMouseConnectType_bluetooth?@"sdk切换到蓝牙模式":@"sdk切换到usb模式"]; - if (type == AiMouseConnectType_usb) { + if (type == AiMouseConnectType_hid) { [self.recordManager checkPermissions]; } } @@ -123,12 +123,11 @@ } - (void)sendAiDataToSdk:(NSData *)data{ - if(self.connectType == AiMouseConnectType_usb){ + if(self.connectType == AiMouseConnectType_hid){ // 0AFFFF12BC32 if(data.length == 6){ NSData *subData = [data subdataWithRange:NSMakeRange(5, 1)]; [self sendCmdData:subData]; - uint8 cmd; [subData getBytes:&cmd length:1]; // [self showLog:[NSString stringWithFormat:@"cmd = %X",cmdInt]]; @@ -144,16 +143,14 @@ } } else if (self.connectType == AiMouseConnectType_bluetooth){ - NSString *str = [[AiMouseTools dataToHexString:data] lowercaseString]; - NSString *cmd = [str substringWithRange:NSMakeRange(0, 12)]; - // 0A65004E003200010101C0C10000A5A5 if(data.length == 16){ + //0A65004E003200010101C0C10000A5A5 NSData *cmd = [data subdataWithRange:NSMakeRange(5, 1)]; - // NSString *cmdStr = [[AiMouseTools dataToHexString:cmd] lowercaseString]; - // NSLog(@"cmd = %@",cmdStr); [self sendCmdData:cmd]; + return; } - else if([cmd hasPrefix:@"9c31"]){ + NSString *cmd = [[AiMouseTools dataToHexString:data] lowercaseString]; + if([cmd hasPrefix:@"9c31"]){ //声音 [self.audioManager receiveData:data]; } diff --git a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj index ebeee42..2dc25fc 100644 --- a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj @@ -9,6 +9,7 @@ /* Begin PBXBuildFile section */ 182B54802BB6A0D7009F53B6 /* AiRecordManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 182B547E2BB6A0D7009F53B6 /* AiRecordManager.m */; }; 182B54812BB6A0D7009F53B6 /* AiRecordManager.h in Headers */ = {isa = PBXBuildFile; fileRef = 182B547F2BB6A0D7009F53B6 /* AiRecordManager.h */; }; + 182B548D2BB7EC9B009F53B6 /* DeviceManger.m in Sources */ = {isa = PBXBuildFile; fileRef = 182B548C2BB7EC9B009F53B6 /* DeviceManger.m */; }; 1867E5A92BB2706F008FC5FB /* HidManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 1867E5A82BB2706F008FC5FB /* HidManager.m */; }; 1877F3E32BB4049600D3220A /* AiMouseConfig.h in Headers */ = {isa = PBXBuildFile; fileRef = 1877F3E12BB4049600D3220A /* AiMouseConfig.h */; }; 1877F3E42BB4049600D3220A /* AiMouseConfig.m in Sources */ = {isa = PBXBuildFile; fileRef = 1877F3E22BB4049600D3220A /* AiMouseConfig.m */; }; @@ -239,6 +240,8 @@ 0311DF3AD67ED792549B96BD /* libPods-MouseSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-MouseSdk.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 182B547E2BB6A0D7009F53B6 /* AiRecordManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = AiRecordManager.m; sourceTree = ""; }; 182B547F2BB6A0D7009F53B6 /* AiRecordManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = AiRecordManager.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 = ""; }; 1867E5A72BB2706F008FC5FB /* HidManager.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = HidManager.h; sourceTree = ""; }; 1867E5A82BB2706F008FC5FB /* HidManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = HidManager.m; sourceTree = ""; }; 1877F3E12BB4049600D3220A /* AiMouseConfig.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiMouseConfig.h; sourceTree = ""; }; @@ -512,6 +515,8 @@ 18BF1E212B83066200B8D90F /* BleManager.m */, 1867E5A72BB2706F008FC5FB /* HidManager.h */, 1867E5A82BB2706F008FC5FB /* HidManager.m */, + 182B548B2BB7EC9B009F53B6 /* DeviceManger.h */, + 182B548C2BB7EC9B009F53B6 /* DeviceManger.m */, 18BF1DCD2B81AB7C00B8D90F /* AppDelegate.h */, 18BF1DCE2B81AB7C00B8D90F /* AppDelegate.m */, 18BF1DD02B81AB7C00B8D90F /* ViewController.h */, @@ -1178,6 +1183,7 @@ 18BF1DD22B81AB7C00B8D90F /* ViewController.m in Sources */, 18BF1DD92B81AB7F00B8D90F /* main.m in Sources */, 18F328D52B98569D00CABA2E /* Config.m in Sources */, + 182B548D2BB7EC9B009F53B6 /* DeviceManger.m in Sources */, 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */, 18BF1E222B83066200B8D90F /* BleManager.m in Sources */, 18BF1DCF2B81AB7C00B8D90F /* AppDelegate.m in Sources */, diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 2454474..fc4e3f5 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 6486004..2d6da70 100644 --- a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist +++ b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/xcdebugger/Breakpoints_v2.xcbkptlist @@ -35,49 +35,33 @@ + + + + - - - - - - diff --git a/MouseSdk/MouseSdk/BleManager.h b/MouseSdk/MouseSdk/BleManager.h index d98b352..122b086 100644 --- a/MouseSdk/MouseSdk/BleManager.h +++ b/MouseSdk/MouseSdk/BleManager.h @@ -17,8 +17,9 @@ @interface BleManager : NSObject @property (nonatomic,strong)void (^discoverDevice)(CBPeripheral *peripheral) ;/// 扫描到设备 @property (nonatomic,strong)void (^didConnectDevice)(CBPeripheral *peripheral);/// 连接成功 +@property (nonatomic,strong)void (^didDisConnectDevice)(CBPeripheral *peripheral);/// 断开连接 @property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 - +/// 单利 +(BleManager *)shared; /// 扫描 diff --git a/MouseSdk/MouseSdk/BleManager.m b/MouseSdk/MouseSdk/BleManager.m index 9d08625..fbc625f 100644 --- a/MouseSdk/MouseSdk/BleManager.m +++ b/MouseSdk/MouseSdk/BleManager.m @@ -41,7 +41,6 @@ self = [super init]; if (self) { self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; - } return self; } @@ -91,7 +90,9 @@ -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ [self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]]; [[NSNotificationCenter defaultCenter] postNotificationName:kDidDisconnectBleNotification object:nil]; - + if(self.didDisConnectDevice){ + self.didDisConnectDevice(peripheral); + } [self autoConnectDevice]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ diff --git a/MouseSdk/MouseSdk/DeviceManger.h b/MouseSdk/MouseSdk/DeviceManger.h new file mode 100644 index 0000000..234f4ab --- /dev/null +++ b/MouseSdk/MouseSdk/DeviceManger.h @@ -0,0 +1,29 @@ +// +// DeviceManger.h +// MouseSdk +// +// Created by incar on 2024/3/30. +// + +#import +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface DeviceManger : NSObject +@property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型 +@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 +@property (nonatomic,strong)void (^connectStateCallBack)(AiMouseConnectType connectType,BOOL isConnect);///是否连接成功 + ++(DeviceManger *)shared; + +/// 切换连接类型 +/// - Parameter type: 连接类型 +- (void)switchConnect:(AiMouseConnectType)type; + +/// 获取设备信息 +- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; + +@end + +NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/DeviceManger.m b/MouseSdk/MouseSdk/DeviceManger.m new file mode 100644 index 0000000..4a2a3af --- /dev/null +++ b/MouseSdk/MouseSdk/DeviceManger.m @@ -0,0 +1,102 @@ +// +// DeviceManger.m +// MouseSdk +// +// Created by incar on 2024/3/30. +// + +#import "DeviceManger.h" +#import "HidManager.h" +#import "BleManager.h" + +@interface DeviceManger() + +@property (nonatomic,strong) BleManager *ble; +@property (nonatomic,strong) HidManager *hid; +@end + +@implementation DeviceManger ++(DeviceManger *)shared{ + static DeviceManger *manager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + manager = [[DeviceManger alloc]init]; + }); + return manager; +} + +- (instancetype)init{ + self = [super init]; + if (self) { + [self setupBle]; + [self setupHid]; + } + return self; +} + +-(AiMouseConnectType)connectType{ + return [AiMouse shared].connectType; +} + +- (void)switchConnect:(AiMouseConnectType)type{ + [[AiMouse shared] switchConnect:type]; + + if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { + [self setupBle]; + } + else{ + [self setupHid]; + } +} + +- (void)setupHid{ + self.ble = [BleManager shared]; + __weak typeof(self) ws = self; + self.ble.authCallBack = ^(BOOL isSuccess) { + if(ws.authCallBack)ws.authCallBack(isSuccess); + }; + self.ble.didConnectDevice = ^(CBPeripheral *peripheral) { + if(ws.connectStateCallBack)ws.connectStateCallBack(AiMouseConnectType_bluetooth,true); + }; + self.ble.didDisConnectDevice = ^(CBPeripheral *peripheral) { + if(ws.connectStateCallBack)ws.connectStateCallBack(AiMouseConnectType_bluetooth,false); + }; +} + +- (void)setupBle{ + self.hid = [HidManager shared]; + __weak typeof(self) ws = self; + self.hid.authCallBack = ^(BOOL isSuccess) { + if(ws.authCallBack)ws.authCallBack(isSuccess); + }; + self.hid.connectStateCallBack = ^(BOOL isConnnect) { + if(ws.connectStateCallBack)ws.connectStateCallBack(AiMouseConnectType_hid,isConnnect); + }; + [self setupBleNotification]; +} + +- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation{ + if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { + [self.ble getDeviceInfo:completation]; + } + else{ + [self.hid getDeviceInfo:completation]; + } +} + + +#pragma mark - notication +- (void)setupBleNotification{ + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidConnect:) name:kDidConnectBleNotification object:nil]; + [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidDisconnect:) name:kDidDisconnectBleNotification object:nil]; +} + +- (void)bleDidConnect:(NSNotification *)notification{ + if(self.connectStateCallBack)self.connectStateCallBack(AiMouseConnectType_bluetooth,true); +} + +- (void)bleDidDisconnect:(NSNotification *)notification{ + if(self.connectStateCallBack)self.connectStateCallBack(AiMouseConnectType_bluetooth,false); + [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; +} +@end diff --git a/MouseSdk/MouseSdk/HidManager.h b/MouseSdk/MouseSdk/HidManager.h index 826614b..8c32827 100644 --- a/MouseSdk/MouseSdk/HidManager.h +++ b/MouseSdk/MouseSdk/HidManager.h @@ -11,9 +11,9 @@ NS_ASSUME_NONNULL_BEGIN @interface HidManager : NSObject -@property (nonatomic,strong)void (^connectStateDidChange)(BOOL isConnnect);/// 设备是否连上 +@property (nonatomic,strong)void (^connectStateCallBack)(BOOL isConnnect);/// 设备是否连上 @property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 -///单利 + +(HidManager *)shared; /// 获取设备信息 diff --git a/MouseSdk/MouseSdk/HidManager.m b/MouseSdk/MouseSdk/HidManager.m index c15857a..60ea2c1 100644 --- a/MouseSdk/MouseSdk/HidManager.m +++ b/MouseSdk/MouseSdk/HidManager.m @@ -14,7 +14,6 @@ @interface HidManager () -//IOHIDDeviceRef deviceRef; @property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理 @property(nonatomic, assign) IOHIDDeviceRef authCha;//鉴权特征 @property(nonatomic, assign) IOHIDDeviceRef aiCha;//ai特征 @@ -36,11 +35,16 @@ self = [super init]; if (self) { [self initHid]; + [self open]; } return self; } - (void)initHid{ + +} + +- (void)open{ self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); @@ -61,6 +65,15 @@ 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) { uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey)); @@ -86,16 +99,15 @@ static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOH unsigned char InputBuffer[128]; IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); - // CFRelease(ref); } static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { NSLog(@"拔出设备 %@ ",inIOHIDDeviceRef); - uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inSender, CFSTR(kIOHIDPrimaryUsagePageKey)); - uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inSender, CFSTR(kIOHIDPrimaryUsageKey)); + uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); + uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ - [HidManager shared].connectStateDidChange(false); + [HidManager shared].connectStateCallBack(false); } } @@ -177,10 +189,10 @@ static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStr #pragma mark - public -(void)startAuthentication{ - [HidManager shared].connectStateDidChange(true); + [HidManager shared].connectStateCallBack(true); [AiMouse shared].delegate = self; //切换当前连接类型 - [[AiMouse shared] switchConnect:AiMouseConnectType_usb]; + [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 [self showLog:@"开始鉴权"]; [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) { @@ -223,7 +235,6 @@ static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStr static inline int get_data_len(uint8_t *buf, u_long len) { if (len <= 0 ) return 0; - int url_len = 0; while (buf[url_len] != 0x00) url_len++; return url_len; diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 6bf7f72..60a34cb 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -7,12 +7,11 @@ #import "ViewController.h" #import "ScanVC.h" -#import "BleManager.h" -#import "HidManager.h" +#import "DeviceManger.h" @interface ViewController () -@property(nonatomic,strong)NSTextField *bleLabel;///蓝牙连接状态 -@property(nonatomic,strong)NSTextField *usbLabel;///usb连接状态 +@property(nonatomic,strong)NSTextField *typeLabel;///当前模式状态 +@property(nonatomic,strong)NSTextField *connectLabel;///连接状态 @property(nonatomic,strong)NSTextField *authLabel;///鉴权状态 @property(nonatomic,strong)NSTextField *deviceInfoLabel;///设备信息 @end @@ -24,9 +23,7 @@ // Do any additional setup after loading the view. [self setupUI]; - [self setupNotification]; - [self setupBleManger]; - [self setupHidManger]; + [self setupDeviceManger]; } - (void)setRepresentedObject:(id)representedObject { @@ -37,7 +34,6 @@ - (void)setupUI{ self.title = @"AI SDK"; - { NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 100, 100, 45)]; btn.bordered = true; @@ -50,18 +46,9 @@ { NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 200, 100, 45)]; btn.bordered = true; - btn.title = @"蓝牙获取设备信息"; + btn.title = @"获取设备信息"; [btn setTarget:self]; - [btn setAction:@selector(bleGetDeviceInfoAction)]; - btn.state = 1; - [self.view addSubview:btn]; - } - { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(50, self.view.bounds.size.height - 260, 100, 45)]; - btn.bordered = true; - btn.title = @"2.4g获取设备信息"; - [btn setTarget:self]; - [btn setAction:@selector(hidGetDeviceInfoAction)]; + [btn setAction:@selector(geviceInfoAction)]; btn.state = 1; [self.view addSubview:btn]; } @@ -69,18 +56,18 @@ NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(180, self.view.bounds.size.height - 80, 300, 35)]; label.editable = NO; label.bordered = NO; - label.stringValue = @"连蓝牙接状态:未连接"; + label.stringValue = @"连接模式:--"; label.backgroundColor = NSColor.clearColor; - self.bleLabel = label; + 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 = @"2.4G连接状态:未连接"; + label.stringValue = @"连接状态:未连接"; label.backgroundColor = NSColor.clearColor; - self.usbLabel = label; + self.connectLabel = label; [self.view addSubview:label]; } { @@ -103,21 +90,15 @@ } } -#pragma mark - ble -- (void)setupBleManger{ - BleManager *manager = [BleManager shared]; - __weak typeof(self) ws = self; - manager.authCallBack = ^(BOOL isSuccess) { - [ws showAuthResult:isSuccess]; - }; -} +#pragma mark - action - (void)scanAction{ ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil]; [self presentViewControllerAsModalWindow:vc]; } -- (void)bleGetDeviceInfoAction{ - [[BleManager shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { +/// 获取设备信息 +- (void)geviceInfoAction{ + [[DeviceManger shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { if (deviceInfo) { NSString *info = [NSString stringWithFormat:@"battery_power = %@,CPI = %@,CPI_list = %@,version = %@,brand = %@,sn = %@,mac = %@",deviceInfo.battery_power,deviceInfo.CPI,deviceInfo.CPI_list,deviceInfo.version,deviceInfo.brand,deviceInfo.sn,deviceInfo.mac]; NSLog(@"%@",info); @@ -140,49 +121,27 @@ self.authLabel.stringValue = isSuccess?@"鉴权状态:鉴权成功":@"鉴权状态:鉴权失败"; }); } -#pragma mark - hid -- (void)setupHidManger{ - HidManager *manager = [HidManager shared]; - __weak typeof(self) ws = self; - manager.authCallBack = ^(BOOL isSuccess) { - [ws showAuthResult:isSuccess]; - }; - manager.connectStateDidChange = ^(BOOL isConnnect) { - ws.usbLabel.stringValue = isConnnect? @"2.4g连接状态:已连接":@"2.4g连接状态:未连接"; - }; -} -- (void)hidGetDeviceInfoAction{ - [[HidManager shared] getDeviceInfo:^(AiMouseDeviceInfo *deviceInfo, NSError *error) { - if (deviceInfo) { - NSString *info = [NSString stringWithFormat:@"battery_power = %@,CPI = %@,CPI_list = %@,version = %@,brand = %@,sn = %@,mac = %@",deviceInfo.battery_power,deviceInfo.CPI,deviceInfo.CPI_list,deviceInfo.version,deviceInfo.brand,deviceInfo.sn,deviceInfo.mac]; - NSLog(@"%@",info); - - dispatch_async(dispatch_get_main_queue(), ^{ - self.deviceInfoLabel.stringValue = info; - }); +- (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?@"连接状态:已连接":@"连接状态:未连接"; } else{ - NSString *info = [NSString stringWithFormat:@"error == %@",error.description]; - NSLog(@"%@",info); - dispatch_async(dispatch_get_main_queue(), ^{ - self.deviceInfoLabel.stringValue = info; - }); + self.typeLabel.stringValue = @"连接模式:--"; + self.connectLabel.stringValue = @"连接状态:未连接"; } - }]; -} - -#pragma mark - notication -- (void)setupNotification{ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidConnect:) name:kDidConnectBleNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidDisconnect:) name:kDidDisconnectBleNotification object:nil]; -} - -- (void)bleDidConnect:(NSNotification *)notification{ - self.bleLabel.stringValue = @"蓝牙连接状态:已连接"; + }); } -- (void)bleDidDisconnect:(NSNotification *)notification{ - self.bleLabel.stringValue = @"蓝牙连接状态:未连接"; +- (void)setupDeviceManger{ + DeviceManger *manager = [DeviceManger shared]; + __weak typeof(self) ws = self; + manager.authCallBack = ^(BOOL isSuccess) { + [ws showAuthResult:isSuccess]; + }; + manager.connectStateCallBack = ^(AiMouseConnectType type,BOOL isConnected) { + [ws connectStateDidChange:type connectState:isConnected]; + }; } - @end