From: liangzhenying <867174124@qq.com> Date: Fri, 23 Aug 2024 08:12:31 +0000 (+0800) Subject: 删掉多余的代码 X-Git-Tag: 千寻适配d9~44 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=fa29f89c04609aff2d08e960040a2c97eb84d501;p=mirrors%2FMouseSdk-MacOS.git 删掉多余的代码 --- diff --git a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj index 8eddb30..3bf05b9 100644 --- a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj @@ -3,7 +3,7 @@ archiveVersion = 1; classes = { }; - objectVersion = 56; + objectVersion = 55; objects = { /* Begin PBXBuildFile section */ @@ -26,6 +26,7 @@ 18BF1DF02B81AB8000B8D90F /* MouseSdkUITestsLaunchTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1DEF2B81AB8000B8D90F /* MouseSdkUITestsLaunchTests.m */; }; 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 */; }; @@ -76,7 +77,7 @@ 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 = ""; }; - 18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MouseSdk.app; sourceTree = BUILT_PRODUCTS_DIR; }; + 18BF1DCA2B81AB7C00B8D90F /* AiMouseSdk.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = AiMouseSdk.app; sourceTree = BUILT_PRODUCTS_DIR; }; 18BF1DCD2B81AB7C00B8D90F /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; 18BF1DCE2B81AB7C00B8D90F /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; 18BF1DD02B81AB7C00B8D90F /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; @@ -97,6 +98,8 @@ 18BF1E752B832D2E00B8D90F /* ScanVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanVC.m; sourceTree = ""; }; 18C31D6F2BBAA6CC0089D656 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist; path = Info.plist; sourceTree = ""; }; 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 = ""; }; @@ -175,7 +178,7 @@ 18BF1DCB2B81AB7C00B8D90F /* Products */ = { isa = PBXGroup; children = ( - 18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */, + 18BF1DCA2B81AB7C00B8D90F /* AiMouseSdk.app */, 18BF1DDF2B81AB8000B8D90F /* MouseSdkTests.xctest */, 18BF1DE92B81AB8000B8D90F /* MouseSdkUITests.xctest */, ); @@ -255,6 +258,8 @@ 18BF1E212B83066200B8D90F /* BleManager.m */, 1867E5A72BB2706F008FC5FB /* HidManager.h */, 1867E5A82BB2706F008FC5FB /* HidManager.m */, + 18CC59942C787AAB0009FCC8 /* DeviceManager2.h */, + 18CC59952C787AAB0009FCC8 /* DeviceManager2.m */, ); path = Ble; sourceTree = ""; @@ -304,7 +309,7 @@ ); name = MouseSdk; productName = MouseSdk; - productReference = 18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */; + productReference = 18BF1DCA2B81AB7C00B8D90F /* AiMouseSdk.app */; productType = "com.apple.product-type.application"; }; 18BF1DDE2B81AB8000B8D90F /* MouseSdkTests */ = { @@ -367,7 +372,7 @@ }; }; buildConfigurationList = 18BF1DC52B81AB7C00B8D90F /* Build configuration list for PBXProject "MouseSdk" */; - compatibilityVersion = "Xcode 14.0"; + compatibilityVersion = "Xcode 13.0"; developmentRegion = en; hasScannedForEncodings = 0; knownRegions = ( @@ -451,6 +456,7 @@ 18F328D52B98569D00CABA2E /* Config.m in Sources */, 18EF7B182C760A5B00AC5223 /* TlwHidManager.m in Sources */, 18EF7B0D2C76079E00AC5223 /* TlwVC.m in Sources */, + 18CC59962C787AAB0009FCC8 /* DeviceManager2.m in Sources */, 182B548D2BB7EC9B009F53B6 /* DeviceManger.m in Sources */, 18EF7B102C7607C100AC5223 /* FrkVC.m in Sources */, 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */, @@ -656,8 +662,8 @@ ); MACOSX_DEPLOYMENT_TARGET = 11.0; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk; - PRODUCT_NAME = "$(TARGET_NAME)"; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk.mix; + PRODUCT_NAME = AiMouseSdk; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_OBJC_BRIDGING_HEADER = "MouseSdk/MouseSdk-Bridging-Header.h"; SWIFT_OPTIMIZATION_LEVEL = "-Onone"; @@ -705,8 +711,8 @@ ); MACOSX_DEPLOYMENT_TARGET = 11.0; MARKETING_VERSION = 1.0; - PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk; - PRODUCT_NAME = "$(TARGET_NAME)"; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.MouseSdk.mix; + PRODUCT_NAME = AiMouseSdk; SWIFT_EMIT_LOC_STRINGS = YES; SWIFT_OBJC_BRIDGING_HEADER = "MouseSdk/MouseSdk-Bridging-Header.h"; SWIFT_VERSION = 5.0; diff --git a/MouseSdk/MouseSdk.xcodeproj/xcshareddata/xcschemes/MouseSdk.xcscheme b/MouseSdk/MouseSdk.xcodeproj/xcshareddata/xcschemes/MouseSdk.xcscheme index 109fc63..7bf9e69 100644 --- a/MouseSdk/MouseSdk.xcodeproj/xcshareddata/xcschemes/MouseSdk.xcscheme +++ b/MouseSdk/MouseSdk.xcodeproj/xcshareddata/xcschemes/MouseSdk.xcscheme @@ -15,7 +15,7 @@ @@ -68,7 +68,7 @@ @@ -85,7 +85,7 @@ diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index c3bec2a..777154a 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 b71c66a..83f7622 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.h +++ b/MouseSdk/MouseSdk/Ble/BleManager.h @@ -9,30 +9,50 @@ #import #import "AiMouseSdk.h" -#define kTlwDidConnectBleNotification @"kTlwDidConnectBleNotification" -#define kTlwDidDisconnectBleNotification @"kTlwDidDisconnectBleNotification" +@interface BleManager : NSObject +@property (nonatomic,copy)void (^discoverDevice)(CBPeripheral *peripheral) ;/// 扫描到设备 +@property (nonatomic,copy)void (^didConnectDevice)(CBPeripheral *peripheral);/// 连接成功 +@property (nonatomic,copy)void (^didDisConnectDevice)(CBPeripheral *peripheral);/// 断开连接 +/// +/// +@property (nonatomic,copy)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 +@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(BOOL connected);/// 设备连接状态回调 +@property (nonatomic, copy)void (^aiMouseSendDeviceInfoDataCallBack)(NSData *data);/// 设备信息数据回调 +@property (nonatomic, copy)void (^aiMouseSendCmdDataCallBack)(NSString *data);/// 鼠标按键指令回调 +@property (nonatomic, copy)void (^begainRecoredCallBack)(void);/// 录音开始回调 +@property (nonatomic, copy)void (^recordingCallBack)(NSData *data);/// 录音进行中回调 +@property (nonatomic, copy)void (^finishRecoredCallBack)(NSData *data);/// 录音结束回调 + +@property (nonatomic, copy)void (^aiMouseAddLogCallBack)(NSString *log);//输出日志 +/// 单利 ++(BleManager *)shared; - -@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);/// 鉴权回调 - /// 扫描 - (void)startScanDevice; - /// 停止扫描 - (void)stopScanDevice; - /// 连接设备 /// - Parameter peripheral: 蓝牙设备 - (void)connectPeripheral:(CBPeripheral *)peripheral; +///当前是否处于蓝牙连接状态 +- (BOOL)isDidConnectBleDevice; + +///直连蓝牙设备,该函数已经处理了startScanDevice、connectPeripheral和stopScanDevice的调用 +/// - Parameter bleName:蓝牙设备名称 +- (void)connectBleDeviceWithName:(NSString *)bleName; +//断开设备链接 +- (void)disconnectBleDevice:(void(^)(BOOL isSuccess))result; + /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; +/// 获取信号强度 +- (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 303523a..8e74ed6 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.m +++ b/MouseSdk/MouseSdk/Ble/BleManager.m @@ -6,8 +6,6 @@ // #import "BleManager.h" -#import "AiMouseSdk.h" -#import "Config.h" // 服务id #define kServiceUUID @"FFF1" @@ -16,26 +14,92 @@ //设备信息特征 #define kDeviceInfoUUID @"0783B63E-8535-B5A0-7140-A304D2495CB6" -@interface BleManager () +@interface BleManager () +{ +// dispatch_block_t _block; + //是否是主动断开蓝牙连接的 + bool _isActiveDisconnect; + dispatch_block_t _disconnectBlock; +} @property(nonatomic, strong) CBCentralManager *centralManager;///蓝牙管理 @property(nonatomic, strong) CBPeripheral *connectPeripheral;//连接的设备 @property(nonatomic, strong) CBCharacteristic *aiCha;//ai特征 @property(nonatomic, strong) CBCharacteristic *deviceInfoCha;//设备信息特征 @property (nonatomic,strong) NSMutableData *recordData;//录音的数据 + +@property (nonatomic, copy)void (^activeDisconnectCallBack)(BOOL isSuccess);//主动断开连接的回调 + +@property (nonatomic, copy) NSString *targetBleName; @end @implementation BleManager ++(BleManager *)shared{ + static BleManager *manager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + manager = [[BleManager alloc]init]; + }); + return manager; +} + - (instancetype)init{ self = [super init]; if (self) { - self.centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; + self.centralManager = [[CBCentralManager alloc]init]; + self.centralManager.delegate = self; + _isActiveDisconnect = NO; + +// //链接超时 +// _block = dispatch_block_create(0, ^{ +// [self showLog:(@"(BleManager)执行超时操作 ")]; +// //连接超时走这里 +// if (self.connectPeripheral == nil || self.deviceInfoCha == nil) { +// if (self.connectPeripheral != nil) { +// //立即取消与外围设备的连接 +// [self.centralManager cancelPeripheralConnection:self.connectPeripheral]; +// self.connectPeripheral = nil; +// } +// [self stopScanDevice]; +// [self showLog:(@"(BleManager)执行超时操作成功,回调连接失败回调 ")]; +// if(self.didDisConnectDevice){ +// self.didDisConnectDevice(nil); +// } +// } +// }); + + //断开连接超时 + _disconnectBlock = dispatch_block_create(0, ^{ + [self showLog:(@"(BleManager)断开连接执行超时操作 ")]; + //连接超时走这里 + if (self.connectPeripheral != nil && self.connectPeripheral.state != CBPeripheralStateDisconnected) { + if (self.activeDisconnectCallBack) { + self.activeDisconnectCallBack(NO); + } + } + else { + if (self.activeDisconnectCallBack) { + self.activeDisconnectCallBack(YES); + } + } + }); } return self; } +- (BOOL)isDidConnectBleDevice { + if (self.connectPeripheral == nil) { + return NO; + } + //当前处于蓝牙链接状态 + if (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting) { + return YES; + } + return NO; +} + #pragma mark - CBCentralManagerDelegate - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central { - [self showLog:[NSString stringWithFormat:@"蓝牙状态 = %@",@(central.state)]]; + [self showLog:[NSString stringWithFormat:@"centralManagerDidUpdateState:蓝牙状态 = %@",@(central.state)]]; switch (central.state) { case CBManagerStateUnknown: @@ -59,89 +123,178 @@ } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ -// [self showLog:[NSString stringWithFormat:@"扫描到设备 = %@, data = %@",peripheral,advertisementData]]; +// [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverPeripheral:扫描到设备 = %@, data = %@",peripheral,advertisementData]]; if(self.discoverDevice){ self.discoverDevice(peripheral); } + + //直连 + if (self.targetBleName != nil && self.targetBleName.length > 0 && [self.targetBleName isEqualToString:peripheral.name]) { + [self connectPeripheral:peripheral]; + } + } - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ - [self showLog:[NSString stringWithFormat:@"连接成功 name = %@",peripheral.name]]; - if(self.didConnectDevice){ - self.didConnectDevice(peripheral); - } + _isActiveDisconnect = NO; + [self showLog:[NSString stringWithFormat:@"(BleManager) didConnectPeripheral:连接成功 name = %@",peripheral.name]]; + //要求在发现特征处返回连接成功 +// if(self.didConnectDevice){ +// self.didConnectDevice(peripheral); +// } peripheral.delegate = self; [peripheral discoverServices:nil]; + self.connectPeripheral = peripheral; + + //直连成功后直接停止扫描 + if (self.targetBleName != nil && self.targetBleName.length > 0 && [self.targetBleName isEqualToString:peripheral.name]) { + [self stopScanDevice]; + } - [[NSNotificationCenter defaultCenter] postNotificationName:kTlwDidConnectBleNotification object:nil]; } -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ - [self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]]; - [[NSNotificationCenter defaultCenter] postNotificationName:kTlwDidDisconnectBleNotification object:nil]; - if(self.didDisConnectDevice){ - self.didDisConnectDevice(peripheral); + [self showLog:[NSString stringWithFormat:@"(BleManager) didDisconnectPeripheral:断开连接。。。%@",error.description]]; +// dispatch_block_cancel(_block); // 取消定时的执行 + if (_isActiveDisconnect) { + dispatch_block_cancel(_disconnectBlock);// 取消定时的执行 + + if (self.activeDisconnectCallBack) { + self.activeDisconnectCallBack(YES); + } + if (self.connectPeripheral != nil) { + self.connectPeripheral = nil; + } + } + else{ + //返回设备断开连接 +// [self aiMouseConnectStateDidChange:false]; + [[AiMouse shared] didDisconnect]; + + if(self.didDisConnectDevice){ + self.didDisConnectDevice(peripheral); + } + self.connectPeripheral = nil; + [self autoConnectDevice]; } -// [self aiMouseConnectStateDidChange:false]; - [[AiMouse shared] didDisconnect]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ - [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]]; + [self showLog:[NSString stringWithFormat:@"(BleManager) didFailToConnectPeripheral:连接失败。。。%@",error.description]]; +// dispatch_block_cancel(_block); // 取消定时的执行 + if(self.didDisConnectDevice){ + self.didDisConnectDevice(peripheral); + } + + self.connectPeripheral = nil; } #pragma mark - CBPeripheralDelegate //发现服务 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { + [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverServices:发现服务"]]; for (CBService *service in peripheral.services){ [peripheral discoverCharacteristics:nil forService:service]; } } + // 发现特征 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { + [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverCharacteristicsForService:发现特征"]]; for (CBCharacteristic *characteristic in service.characteristics){ + + [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverCharacteristicsForService:service.characteristicUUID %@",characteristic.UUID.UUIDString]]; + if ([characteristic.UUID.UUIDString isEqualToString:kAiUUID]) { + [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverCharacteristicsForService:AI特征"]]; //AI特征 self.aiCha = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } else if ([characteristic.UUID.UUIDString isEqualToString:kDeviceInfoUUID]) { + [self showLog:[NSString stringWithFormat:@"(BleManager) didDiscoverCharacteristicsForService:设备信息特征"]]; //设备信息特征 self.deviceInfoCha = characteristic; [peripheral setNotifyValue:YES forCharacteristic:characteristic]; - //切换代理 - [AiMouse shared].delegate = self; - //切换当前连接类型 - [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth]; - +// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 +// [self startAuthentication]; + + //按照前端要求,他们自己调用鉴权接口,此处只先设置代理和连接类型 + { //设置代理 + [AiMouse shared].delegate = self; + //切换当前连接类型 + [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth]; + } + + [self showLog:(@"(BleManager)didDiscoverCharacteristicsForService:回调连接成功 ")]; +// dispatch_block_cancel(_block); // 取消定时的执行 + //此时才返回连接成功 + if(self.didConnectDevice){ + self.didConnectDevice(peripheral); + } } } } //收到蓝牙数据 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { NSData *data = characteristic.value; + [self showLog:[NSString stringWithFormat:@"(BleManager) didUpdateValueForCharacteristic:[receive] %@",data]]; + [self showLog:[NSString stringWithFormat:@"(BleManager) didUpdateValueForCharacteristic:characteristicUUID %@",characteristic.UUID.UUIDString]]; + if([characteristic.UUID.UUIDString isEqualToString:kAiUUID]){ //AI数据 + [self showLog:[NSString stringWithFormat:@"(BleManager) didUpdateValueForCharacteristic:发送AI数据sendAiDataToSdk"]]; [[AiMouse shared] sendAiDataToSdk:data]; } else if([characteristic.UUID.UUIDString isEqualToString:kDeviceInfoUUID]){ //设备信息数据 + [self showLog:[NSString stringWithFormat:@"(BleManager) didUpdateValueForCharacteristic:发送设备信息数据sendDeviceDataToSdk"]]; [[AiMouse shared] sendDeviceDataToSdk:data]; } } #pragma mark - AiDelegate + +/// 设备连接状态 +/// - Parameter connected: 是否连接 +- (void)aiMouseConnectStateDidChange:(BOOL)connected { + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseConnectStateDidChange - 设备连接状态返回: %hhd",connected]]; + + if (self.aiMouseConnectStateDidChangeCallBack) { + self.aiMouseConnectStateDidChangeCallBack(connected); + } + //没有连接 + if (!connected) { + self.connectPeripheral = nil; + self.deviceInfoCha = nil; + self.aiCha = nil; + } +} + /// 设备鉴权结果返回 /// - Parameter authState: 鉴权状态 --(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]]; +- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseAuthenticationResult - 设备鉴权结果返回: %ld",(long)authState]]; if(self.authCallBack){ self.authCallBack(authState == AiMouseAuthState_Success); } } + /// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 /// - Parameter data: 数据 -(void)aiMouseSendDeviceInfoData:(NSData *)data{ -// [self showLog:[NSString stringWithFormat:@"[send] %@",data]]; + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseSendDeviceInfoData:[send] %@",data]]; + if (self.connectPeripheral ==nil) { + [self showLog:@"peripheral 为空"]; + return; + } + if (self.deviceInfoCha ==nil) { + [self showLog:@"characteristic 为空"]; + return; + } [self.connectPeripheral writeValue:data forCharacteristic:self.deviceInfoCha type:CBCharacteristicWriteWithoutResponse]; + + if (self.aiMouseSendDeviceInfoDataCallBack) { + self.aiMouseSendDeviceInfoDataCallBack(data); + } } /// 鼠标按键指令回调 @@ -153,62 +306,158 @@ - (void)aiMouseSendCmdData:(NSData *)data{ uint8 cmd; [data getBytes:&cmd length:1]; - [self showLog:[NSString stringWithFormat:@"收到指令 = %X",cmd]]; + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseSendCmdData:收到指令 = %X",cmd]]; + + if (self.aiMouseSendCmdDataCallBack) { + self.aiMouseSendCmdDataCallBack([NSString stringWithFormat:@"%X",cmd]); + } if(cmd == 0x31 || cmd == 0x21 || cmd == 0x25 ||cmd == 0x27){ - [self showLog:@"开始录音"]; + [self showLog:@"(BleManager) aiMouseSendCmdData:开始录音"]; self.recordData = [[NSMutableData alloc] init]; + if (self.begainRecoredCallBack) { + self.begainRecoredCallBack(); + } } else if(cmd == 0x32 || cmd == 0x22 || cmd == 0x26 ||cmd == 0x28){ - [self showLog:@"结束录音"]; + [self showLog:@"(BleManager) aiMouseSendCmdData:结束录音"]; [self finishRecored:self.recordData]; + if (self.finishRecoredCallBack) { + self.finishRecoredCallBack(self.recordData); + } } } /// 录音回调 /// - Parameter data: pcm格式语音数据 - (void)aiMouseSendRecordData:(NSData *)data{ + NSString *base64String = [data base64EncodedStringWithOptions:0]; + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseSendRecordData:[appendData] %@\n%@",data,base64String]]; [self.recordData appendData:data]; + if (self.recordingCallBack) { + self.recordingCallBack(data); + } } -/// 输出日志 -- (void)aiMouseAddLog:(NSString *)log { -// [self showLog:log]; + +- (void)aiMouseAddLog:(NSString *)log { + if (self.aiMouseAddLogCallBack) { + self.aiMouseAddLogCallBack(log); + } } -// 连接状态改变 -- (void)aiMouseConnectStateDidChange:(BOOL)connected{ - if (connected) { - [[NSNotificationCenter defaultCenter] postNotificationName:kTlwDidConnectBleNotification object:nil]; +#pragma mark - public +- (void)connectBleDeviceWithName:(NSString *)bleName { + //如果是已连接或者正在连接,不需要在链接了 + if (self.connectPeripheral != nil ) { + if ([self.connectPeripheral.name isEqualToString:bleName]) { + if (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting) { + return; + } + } + else { + [self.centralManager cancelPeripheralConnection:self.connectPeripheral]; + } } - else{ - [[NSNotificationCenter defaultCenter] postNotificationName:kTlwDidDisconnectBleNotification object:nil]; + + [self showLog:[NSString stringWithFormat:@"(BleManager) connectBleDeviceWithName:%@",bleName]]; +// [self stopScanDevice]; + self.targetBleName = bleName; + [self startScanDevice]; + +// 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]; } -#pragma mark - public + /// 连接设备 - (void)startScanDevice{ + [self showLog:[NSString stringWithFormat:@"(BleManager) startScanDevice:开始扫描"]]; [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}]; } /// 扫描设备 - (void)stopScanDevice{ + [self showLog:[NSString stringWithFormat:@"(BleManager) stopScanDevice:停止扫描"]]; [self.centralManager stopScan]; } /// 连接设备 - (void)connectPeripheral:(CBPeripheral *)peripheral{ + //peripheral设备已经链接 + if (self.connectPeripheral == peripheral && (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting)) { + return; + } + + + [self showLog:[NSString stringWithFormat:@"(BleManager) connectPeripheral:连接设备"]]; self.connectPeripheral = peripheral; [self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : [NSNumber numberWithBool:YES]}]; } /// 获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo * _Nonnull, NSError * _Nonnull))completation{ + [self showLog:[NSString stringWithFormat:@"(BleManager) getDeviceInfo:获取设备信息"]]; [[AiMouse shared] getDeviceInfo:completation]; } +/// 获取信号强度 +- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete { + [self showLog:[NSString stringWithFormat:@"(BleManager) getRSSI:获取信号强度"]]; + [[AiMouse shared] getRSSI:complete]; +} + +///// 鉴权 +//-(void)startAuthentication{ +// //设置代理 +// [AiMouse shared].delegate = self; +// //切换当前连接类型 +// [[AiMouse shared] switchConnect:AiMouseConnectType_bluetooth]; +// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 +// [self showLog:@"(BleManager) startAuthentication 开始鉴权"]; +// //鉴权 +// [[AiMouse shared] startAuthentication:^(AiMouseAuthState state) { +// [self showLog:[NSString stringWithFormat:@"(BleManager) startAuthentication 鉴权结果 = %@",@(state)]]; +// if(self.authCallBack){ +// self.authCallBack(state == AiMouseAuthState_Success); +// } +// }]; +//} + #pragma mark - other //自动连接,获取已配对的蓝牙,取第一个连接 - (void)autoConnectDevice{ +// [self showLog:[NSString stringWithFormat:@"(BleManager) autoConnectDevice:开始自动重连设备"]]; //如果是已连接或者正在连接,则不再重连 if (self.connectPeripheral && (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting)) { return; @@ -216,9 +465,10 @@ NSArray *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; if (devices.count > 0) { CBPeripheral *peripheral = devices[0]; - [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",peripheral.name]]; + [self showLog:[NSString stringWithFormat:@"(BleManager) autoConnectDevice:匹配过的设备%@",peripheral.name]]; [self connectPeripheral:peripheral]; } + // 10s自动重连 [self performSelector:@selector(autoConnectDevice) withObject:nil afterDelay:10]; } @@ -228,25 +478,15 @@ //将录音数据保存 - (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]]; - } -} - -- (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; +// 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:@"finishRecored:录音保存失败 %@",err.description]]; +// } +// else{ +// [self showLog:[NSString stringWithFormat:@"finishRecored:录音保存成功 path = %@",fullPath]]; +// } } @end diff --git a/MouseSdk/MouseSdk/Ble/DeviceManager2.h b/MouseSdk/MouseSdk/Ble/DeviceManager2.h new file mode 100644 index 0000000..cee9bb6 --- /dev/null +++ b/MouseSdk/MouseSdk/Ble/DeviceManager2.h @@ -0,0 +1,127 @@ +//// +//// DeviceManger.h +//// MouseSdk +//// +//// Created by incar on 2024/3/30. +//// +// +//#import +//#import "AiMouseSdk.h" +// +//NS_ASSUME_NONNULL_BEGIN +// +/////用于connectStateCallBack的connectState +//#define connectState_Success @"success" +//#define connectState_Fail @"fail" +// +//@interface DeviceManger : NSObject +// +//@property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型 +//#pragma mark ------- 鼠标的回调 +//@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 +//@property (nonatomic, copy)void (^aiMouseSendDeviceInfoDataCallBack)(NSString *data);/// 设备信息数据回调 +//@property (nonatomic, copy)void (^aiMouseSendCmdDataCallBack)(NSString *data);/// 鼠标按键指令回调 +/////录音相关 +//@property (nonatomic, copy)void (^begainRecoredCallBack)(void);/// 录音开始回调 +//@property (nonatomic, copy)void (^recordingCallBack)(NSString *data);/// 录音进行中回调 +//@property (nonatomic, copy)void (^finishRecoredCallBack)(NSString *data);/// 录音结束回调 +/////其他 +//@property (nonatomic, copy)void (^aiMouseAddLogCallBack)(NSString *log);///输出日志 +// +// +//#pragma mark ------- 接口 +/////单例 +//+(DeviceManger *)shared; +// +/////当前设置的连接类型:@"0"表示蓝牙;@"1"表示USB; +//- (NSString *)getCurrentConnectType; +// +/////开始监听鼠标的链接状态 +//- (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; +// +///// 获取信号强度 +//- (void)getRSSI:(void (^)(NSString *value,NSError *error))completation; +// +/////设置CPI值 +/////value: AiMouseDeviceInfo 中可以设置的值CPI_list +//- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 设置品牌名称 +///// - Parameters: +///// - value: 品牌名称 +///// - finished: 设置结果回调 +//- (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///////开始鉴权 +////-(void)startAuthentication; +// +/////// 开始鉴权 +////- (void)startAuthentication:(void (^)(BOOL isSuccess))completation; +// +///// 设置录音模式 +///// - Parameters: +///// - value: 0:常规模式,按按键打开关闭录音 1:命令模式,发命令打开关闭录音 +///// - finished: 设置结果回调 +//- (void)setRecordMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 设置长按滚轮弹出的网页地址 +///// - Parameters: +///// - url: 最大长度:49字节 +///// - finished: 设置结果回调 +//- (void)setUrl:(NSString *)url completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 设置长按滚轮弹出网页开关 +///// - Parameters: +///// - state: 0关闭,1打开 +///// - finished: 设置结果回调 +//- (void)setUrlState:(int)state completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 设置鼠标连接模式 +///// - Parameters: +///// - mode: 0:2.4G模式,1:蓝牙1模式,2:蓝牙2模式 +///// - finished: 设置结果回调 +//- (void)setMouseMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 获取录音模式 +///// - Parameter complete: mode 0:常规模式,按按键打开关闭录音 1:命令模式,发命令打开关闭录音 。error为nil才代表获取成功 +//- (void)getRecordMode:(void (^)(int mode,NSError *error))complete; +// +///// 获取长按滚轮弹出的网页地址 +///// - Parameter complete: url地址 。error为nil才代表获取成功 +//- (void)getUrl:(void (^)(NSString *url,NSError *error))complete; +// +///// 获取长按滚轮弹出的网页地址开关状态 +///// - Parameter complete: state 0关闭,1打开。 error为nil才代表获取成功 +//- (void)getUrlState:(void (^)(int state,NSError *error))complete; +// +///// 获取鼠标连接模式 +///// - Parameter complete: mode 0:2.4G模式,1:蓝牙1模式,2:蓝牙2模式 。error为nil才代表获取成功 +//- (void)getMouseModel:(void (^)(int mode,NSError *error))complete; +// +///// 打开录音 +///// - finished: 执行结果回调 +//- (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished; +// +///// 关闭录音 +///// - finished: 执行结果回调 +//- (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished; +// +//@end +// +//NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/Ble/DeviceManager2.m b/MouseSdk/MouseSdk/Ble/DeviceManager2.m new file mode 100644 index 0000000..e49a962 --- /dev/null +++ b/MouseSdk/MouseSdk/Ble/DeviceManager2.m @@ -0,0 +1,328 @@ +//// +//// 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; +// +////connectType 返回为0代表蓝牙,为1代表usb +//@property (nonatomic, copy)void (^aiMouseConnectStateDidChangeCallBack)(int connectType, BOOL connected);/// 设备连接状态回调 +// +//@end +// +//@implementation DeviceManger +//+(DeviceManger *)shared{ +// static DeviceManger *manager = nil; +// static dispatch_once_t oncePredicate; +// dispatch_once(&oncePredicate, ^{ +// +// [[AiMouse shared] setLogLevel:(AiMouseLogLevel_info)]; +// manager = [[DeviceManger alloc]init]; +// +// }); +// return manager; +//} +// +//- (instancetype)init{ +// self = [super init]; +// if (self) { +// [self setupBle]; +// [self setupHid]; +// +// //先默认设置蓝牙管理对象为鼠标 +// [[AiMouse shared] setDelegate:self.ble]; +// } +// return self; +//} +// +//-(AiMouseConnectType)connectType{ +// return [AiMouse shared].connectType; +//} +// +// +//- (NSString *)getCurrentConnectType { +// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +// return @"0"; +// } +// else { +// return @"1"; +// } +//} +// +////- (void)switchConnect:(AiMouseConnectType)type{ +//// [[AiMouse shared] switchConnect:type]; +//// +//// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +//// [self setupBle]; +//// [AiMouse shared].delegate = self.ble; +//// } +//// else{ +//// [self setupHid]; +//// [AiMouse 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{ +// self.hid = [HidManager shared]; +// __weak typeof(self) ws = self; +// +// self.hid.authCallBack = ^(BOOL isSuccess) { +// if(ws.authCallBack)ws.authCallBack(isSuccess); +// }; +// self.hid.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) { +// if (ws.aiMouseConnectStateDidChangeCallBack) { +// ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_hid,connected); +// } +// //连接失败时,重新初始化USB监听 +// if (!connected) { +// [ws.hid close]; +// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ +// [ws.hid open]; +// }); +// } +// }; +// +// self.hid.aiMouseSendCmdDataCallBack = ^(NSString * _Nonnull data) { +// if(ws.aiMouseSendCmdDataCallBack){ +// ws.aiMouseSendCmdDataCallBack(data); +// } +// }; +// +// self.hid.aiMouseSendDeviceInfoDataCallBack = ^(NSData * _Nonnull data) { +// if (ws.aiMouseSendDeviceInfoDataCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.aiMouseSendDeviceInfoDataCallBack(base64String); +// } +// }; +// self.hid.begainRecoredCallBack = ^{ +// if (ws.begainRecoredCallBack) { +// ws.begainRecoredCallBack(); +// } +// }; +// +// self.hid.recordingCallBack = ^(NSData *data) { +// if (ws.recordingCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.recordingCallBack(base64String); +// } +// }; +// +// self.hid.finishRecoredCallBack = ^(NSData * _Nonnull data) { +// if (ws.finishRecoredCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.finishRecoredCallBack(base64String); +// } +// }; +// +// self.hid.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) { +// if (ws.aiMouseAddLogCallBack) { +// ws.aiMouseAddLogCallBack(log); +// } +// }; +//} +// +//- (void)setupBle{ +// self.ble = [BleManager shared]; +// __weak typeof(self) ws = self; +// +// self.ble.authCallBack = ^(BOOL isSuccess) { +// if(ws.authCallBack)ws.authCallBack(isSuccess); +// }; +// +// self.ble.aiMouseConnectStateDidChangeCallBack = ^(BOOL connected) { +// if (ws.aiMouseConnectStateDidChangeCallBack) { +// ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_bluetooth,connected); +// } +// +// //蓝牙连接失败,重新初始化USB监听 +// if (!connected) { +// [ws.hid close]; +// dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ +// [ws.hid open]; +// }); +// } +// }; +// +// self.ble.aiMouseSendCmdDataCallBack = ^(NSString * _Nonnull data) { +// if(ws.aiMouseSendCmdDataCallBack){ +// ws.aiMouseSendCmdDataCallBack(data); +// } +// }; +// +// self.ble.aiMouseSendDeviceInfoDataCallBack = ^(NSData * _Nonnull data) { +// if (ws.aiMouseSendDeviceInfoDataCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.aiMouseSendDeviceInfoDataCallBack(base64String); +// } +// }; +// +// self.ble.begainRecoredCallBack = ^{ +// if (ws.begainRecoredCallBack) { +// ws.begainRecoredCallBack(); +// } +// }; +// +// self.ble.recordingCallBack = ^(NSData *data) { +// if (ws.recordingCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.recordingCallBack(base64String); +// } +// }; +// +// self.ble.finishRecoredCallBack = ^(NSData * _Nonnull data) { +// if (ws.finishRecoredCallBack) { +// // 将NSData转换为Base64编码的字符串 +// NSString *base64String = [data base64EncodedStringWithOptions:0]; +// ws.finishRecoredCallBack(base64String); +// } +// }; +// +// self.ble.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) { +// if (ws.aiMouseAddLogCallBack) { +// ws.aiMouseAddLogCallBack(log); +// } +// }; +//} +// +// +//#pragma mark ------ 设备相关接口 +/////获取设备信息 +//- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation{ +//// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +//// [self.ble getDeviceInfo:completation]; +//// } +//// else{ +//// [self.hid getDeviceInfo:completation]; +//// } +// [[AiMouse shared] getDeviceInfo:completation]; +//} +// +///// 获取信号强度 +//- (void)getRSSI:(void (^)(NSString *value,NSError *error))completation{ +//// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +//// [self.ble getRSSI:completation]; +//// } +//// else{ +//// [self.hid getRSSI:completation]; +//// } +// [[AiMouse shared] getRSSI:completation]; +//} +// +///// 设置CPI +//- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] setCPI:value completation:finished]; +//} +// +///// 设置品牌名称 +///// - Parameters: +///// - value: 品牌名称 +///// - finished: 设置结果回调 +//- (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished { +// [[AiMouse shared] setBrand:brand completation:finished]; +//} +// +///////设备鉴权 +////-(void)startAuthentication { +//// if ([AiMouse 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{ +// [[AiMouse shared] setRecordMode:mode completation:finished]; +//} +// +///// 设置长按滚轮弹出的网页地址 +//- (void)setUrl:(NSString *)url completation:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] setUrl:url completation:finished]; +//} +// +///// 设置长按滚轮弹出网页开关 +//- (void)setUrlState:(int)state completation:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] setUrlState:state completation:finished]; +//} +// +///// 设置鼠标连接模式 +//- (void)setMouseMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] setMouseMode:mode completation:finished]; +//} +// +///// 获取录音模式 +//- (void)getRecordMode:(void (^)(int mode,NSError *error))complete{ +// [[AiMouse shared] getRecordMode:complete]; +//} +// +///// 获取长按滚轮弹出的网页地址 +//- (void)getUrl:(void (^)(NSString *url,NSError *error))complete{ +// [[AiMouse shared] getUrl:complete]; +//} +// +///// 获取长按滚轮弹出的网页地址开关状态 +//- (void)getUrlState:(void (^)(int state,NSError *error))complete{ +// [[AiMouse shared] getUrlState:complete]; +//} +// +///// 获取鼠标连接模式 +//- (void)getMouseModel:(void (^)(int mode,NSError *error))complete{ +// [[AiMouse shared] getMouseModel:complete]; +//} +// +///// 打开录音 +//- (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] openRecord:finished]; +//} +// +///// 关闭录音 +//- (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ +// [[AiMouse shared] closeRecord:finished]; +//} +//@end diff --git a/MouseSdk/MouseSdk/Ble/DeviceManger.h b/MouseSdk/MouseSdk/Ble/DeviceManger.h index 108b310..9cf626d 100644 --- a/MouseSdk/MouseSdk/Ble/DeviceManger.h +++ b/MouseSdk/MouseSdk/Ble/DeviceManger.h @@ -7,32 +7,121 @@ #import #import "AiMouseSdk.h" -#import "HidManager.h" -#import "BleManager.h" +NS_ASSUME_NONNULL_BEGIN + +///用于connectStateCallBack的connectState +#define connectState_Success @"success" +#define connectState_Fail @"fail" @interface DeviceManger : NSObject -@property (nonatomic,strong) BleManager *ble; -@property (nonatomic,strong) HidManager *hid; @property (nonatomic, readonly) AiMouseConnectType connectType;//连接类型 +#pragma mark ------- 鼠标的回调 @property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 -@property (nonatomic,strong)void (^connectStateCallBack)(AiMouseConnectType connectType,BOOL isConnect);///是否连接成功 +@property (nonatomic, copy)void (^aiMouseSendDeviceInfoDataCallBack)(NSString *data);/// 设备信息数据回调 +@property (nonatomic, copy)void (^aiMouseSendCmdDataCallBack)(NSString *data);/// 鼠标按键指令回调 +///录音相关 +@property (nonatomic, copy)void (^begainRecoredCallBack)(void);/// 录音开始回调 +@property (nonatomic, copy)void (^recordingCallBack)(NSString *data);/// 录音进行中回调 +@property (nonatomic, copy)void (^finishRecoredCallBack)(NSString *data);/// 录音结束回调 +///其他 +@property (nonatomic, copy)void (^aiMouseAddLogCallBack)(NSString *log);///输出日志 + +#pragma mark ------- 接口 +///单例 +(DeviceManger *)shared; -//开始,使用该sdk需要调用该方法 -- (void)start; +///当前设置的连接类型:@"0"表示蓝牙;@"1"表示USB; +- (NSString *)getCurrentConnectType; -//停止,停止使用sdk需要调用该方法 -- (void)stop; +///开始监听鼠标的链接状态 +- (void)begainMonitorAiMouseConnectStateDidChange:(NSString *)mouseName callBack:(void(^)(int connectType, BOOL connected))callBack; -/// 切换连接类型 -/// - Parameter type: 连接类型 -- (void)switchConnect:(AiMouseConnectType)type; +//#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; +/// 获取信号强度 +- (void)getRSSI:(void (^)(NSString *value,NSError *error))completation; + +///设置CPI值 +///value: AiMouseDeviceInfo 中可以设置的值CPI_list +- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 设置品牌名称 +/// - Parameters: +/// - value: 品牌名称 +/// - finished: 设置结果回调 +- (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/////开始鉴权 +//-(void)startAuthentication; + +///// 开始鉴权 +//- (void)startAuthentication:(void (^)(BOOL isSuccess))completation; + +/// 设置录音模式 +/// - Parameters: +/// - value: 0:常规模式,按按键打开关闭录音 1:命令模式,发命令打开关闭录音 +/// - finished: 设置结果回调 +- (void)setRecordMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 设置长按滚轮弹出的网页地址 +/// - Parameters: +/// - url: 最大长度:49字节 +/// - finished: 设置结果回调 +- (void)setUrl:(NSString *)url completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 设置长按滚轮弹出网页开关 +/// - Parameters: +/// - state: 0关闭,1打开 +/// - finished: 设置结果回调 +- (void)setUrlState:(int)state completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 设置鼠标连接模式 +/// - Parameters: +/// - mode: 0:2.4G模式,1:蓝牙1模式,2:蓝牙2模式 +/// - finished: 设置结果回调 +- (void)setMouseMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 获取录音模式 +/// - Parameter complete: mode 0:常规模式,按按键打开关闭录音 1:命令模式,发命令打开关闭录音 。error为nil才代表获取成功 +- (void)getRecordMode:(void (^)(int mode,NSError *error))complete; + +/// 获取长按滚轮弹出的网页地址 +/// - Parameter complete: url地址 。error为nil才代表获取成功 +- (void)getUrl:(void (^)(NSString *url,NSError *error))complete; + +/// 获取长按滚轮弹出的网页地址开关状态 +/// - Parameter complete: state 0关闭,1打开。 error为nil才代表获取成功 +- (void)getUrlState:(void (^)(int state,NSError *error))complete; + +/// 获取鼠标连接模式 +/// - Parameter complete: mode 0:2.4G模式,1:蓝牙1模式,2:蓝牙2模式 。error为nil才代表获取成功 +- (void)getMouseModel:(void (^)(int mode,NSError *error))complete; + +/// 打开录音 +/// - finished: 执行结果回调 +- (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished; + +/// 关闭录音 +/// - finished: 执行结果回调 +- (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished; + @end +NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/Ble/DeviceManger.m b/MouseSdk/MouseSdk/Ble/DeviceManger.m index 09d8f74..a0f5504 100644 --- a/MouseSdk/MouseSdk/Ble/DeviceManger.m +++ b/MouseSdk/MouseSdk/Ble/DeviceManger.m @@ -6,10 +6,17 @@ // #import "DeviceManger.h" - +#import "HidManager.h" +#import "BleManager.h" @interface DeviceManger() +@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 @@ -17,27 +24,22 @@ static DeviceManger *manager = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ + + [[AiMouse shared] setLogLevel:(AiMouseLogLevel_info)]; manager = [[DeviceManger alloc]init]; + }); return manager; } -//开始,使用该sdk需要调用该方法 -- (void)start{ - [self setupBle]; - [self setupHid]; -} - -//停止,停止使用sdk需要调用该方法 -- (void)stop{ - self.ble = nil; - self.hid = nil; -} - - (instancetype)init{ self = [super init]; if (self) { - [[AiMouse shared] setLogLevel:AiMouseLogLevel_info]; + [self setupBle]; + [self setupHid]; + + //先默认设置蓝牙管理对象为鼠标 + [[AiMouse shared] setDelegate:self.ble]; } return self; } @@ -46,66 +48,301 @@ return [AiMouse shared].connectType; } -- (void)switchConnect:(AiMouseConnectType)type{ - [[AiMouse shared] switchConnect:type]; - + +- (NSString *)getCurrentConnectType { if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { - [self setupBle]; + return @"0"; } - else{ - [self setupHid]; + else { + return @"1"; } } +//- (void)switchConnect:(AiMouseConnectType)type{ +// [[AiMouse shared] switchConnect:type]; +// +// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +// [self setupBle]; +// [AiMouse shared].delegate = self.ble; +// } +// else{ +// [self setupHid]; +// [AiMouse 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{ - self.hid = [[HidManager alloc] init]; + 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); + //改为由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) { + ws.aiMouseConnectStateDidChangeCallBack(AiMouseConnectType_hid,connected); + } + //连接失败时,重新初始化USB监听 + if (!connected) { + [ws.hid close]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [ws.hid open]; + }); + } + }; + + self.hid.aiMouseSendCmdDataCallBack = ^(NSString * _Nonnull data) { + if(ws.aiMouseSendCmdDataCallBack){ + ws.aiMouseSendCmdDataCallBack(data); + } + }; + + self.hid.aiMouseSendDeviceInfoDataCallBack = ^(NSData * _Nonnull data) { + if (ws.aiMouseSendDeviceInfoDataCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.aiMouseSendDeviceInfoDataCallBack(base64String); + } + }; + self.hid.begainRecoredCallBack = ^{ + if (ws.begainRecoredCallBack) { + ws.begainRecoredCallBack(); + } + }; + + self.hid.recordingCallBack = ^(NSData *data) { + if (ws.recordingCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.recordingCallBack(base64String); + } + }; + + self.hid.finishRecoredCallBack = ^(NSData * _Nonnull data) { + if (ws.finishRecoredCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.finishRecoredCallBack(base64String); + } + }; + + self.hid.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) { + if (ws.aiMouseAddLogCallBack) { + ws.aiMouseAddLogCallBack(log); + } }; } - (void)setupBle{ - self.ble = [[BleManager alloc] init]; + 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); + + //改为由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); + } + + //蓝牙连接失败,重新初始化USB监听 + if (!connected) { + [ws.hid close]; + dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ + [ws.hid open]; + }); + } + }; + + self.ble.aiMouseSendCmdDataCallBack = ^(NSString * _Nonnull data) { + if(ws.aiMouseSendCmdDataCallBack){ + ws.aiMouseSendCmdDataCallBack(data); + } + }; + + self.ble.aiMouseSendDeviceInfoDataCallBack = ^(NSData * _Nonnull data) { + if (ws.aiMouseSendDeviceInfoDataCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.aiMouseSendDeviceInfoDataCallBack(base64String); + } }; - self.ble.didDisConnectDevice = ^(CBPeripheral *peripheral) { - if(ws.connectStateCallBack)ws.connectStateCallBack(AiMouseConnectType_bluetooth,false); + + self.ble.begainRecoredCallBack = ^{ + if (ws.begainRecoredCallBack) { + ws.begainRecoredCallBack(); + } + }; + + self.ble.recordingCallBack = ^(NSData *data) { + if (ws.recordingCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.recordingCallBack(base64String); + } + }; + + self.ble.finishRecoredCallBack = ^(NSData * _Nonnull data) { + if (ws.finishRecoredCallBack) { + // 将NSData转换为Base64编码的字符串 + NSString *base64String = [data base64EncodedStringWithOptions:0]; + ws.finishRecoredCallBack(base64String); + } + }; + + self.ble.aiMouseAddLogCallBack = ^(NSString * _Nonnull log) { + if (ws.aiMouseAddLogCallBack) { + ws.aiMouseAddLogCallBack(log); + } }; - [self setupBleNotification]; } + +#pragma mark ------ 设备相关接口 +///获取设备信息 - (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation{ - if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { - [self.ble getDeviceInfo:completation]; - } - else{ - [self.hid getDeviceInfo:completation]; - } +// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +// [self.ble getDeviceInfo:completation]; +// } +// else{ +// [self.hid getDeviceInfo:completation]; +// } + [[AiMouse shared] getDeviceInfo:completation]; } +/// 获取信号强度 +- (void)getRSSI:(void (^)(NSString *value,NSError *error))completation{ +// if ([AiMouse shared].connectType == AiMouseConnectType_bluetooth) { +// [self.ble getRSSI:completation]; +// } +// else{ +// [self.hid getRSSI:completation]; +// } + [[AiMouse shared] getRSSI:completation]; +} -#pragma mark - notication -- (void)setupBleNotification{ - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidConnect:) name:kTlwDidConnectBleNotification object:nil]; - [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(bleDidDisconnect:) name:kTlwDidDisconnectBleNotification object:nil]; +/// 设置CPI +- (void)setCPI:(NSInteger)value completation:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] setCPI:value completation:finished]; } -- (void)bleDidConnect:(NSNotification *)notification{ - if(self.connectStateCallBack)self.connectStateCallBack(AiMouseConnectType_bluetooth,true); +/// 设置品牌名称 +/// - Parameters: +/// - value: 品牌名称 +/// - finished: 设置结果回调 +- (void)setBrand:(NSString *)brand completation:(void (^)(BOOL isSuccess,NSError *error))finished { + [[AiMouse shared] setBrand:brand completation:finished]; } -- (void)bleDidDisconnect:(NSNotification *)notification{ - if(self.connectStateCallBack)self.connectStateCallBack(AiMouseConnectType_bluetooth,false); - [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; +/////设备鉴权 +//-(void)startAuthentication { +// if ([AiMouse 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{ + [[AiMouse shared] setRecordMode:mode completation:finished]; +} + +/// 设置长按滚轮弹出的网页地址 +- (void)setUrl:(NSString *)url completation:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] setUrl:url completation:finished]; +} + +/// 设置长按滚轮弹出网页开关 +- (void)setUrlState:(int)state completation:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] setUrlState:state completation:finished]; +} + +/// 设置鼠标连接模式 +- (void)setMouseMode:(int)mode completation:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] setMouseMode:mode completation:finished]; +} + +/// 获取录音模式 +- (void)getRecordMode:(void (^)(int mode,NSError *error))complete{ + [[AiMouse shared] getRecordMode:complete]; } +/// 获取长按滚轮弹出的网页地址 +- (void)getUrl:(void (^)(NSString *url,NSError *error))complete{ + [[AiMouse shared] getUrl:complete]; +} + +/// 获取长按滚轮弹出的网页地址开关状态 +- (void)getUrlState:(void (^)(int state,NSError *error))complete{ + [[AiMouse shared] getUrlState:complete]; +} + +/// 获取鼠标连接模式 +- (void)getMouseModel:(void (^)(int mode,NSError *error))complete{ + [[AiMouse shared] getMouseModel:complete]; +} + +/// 打开录音 +- (void)openRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] openRecord:finished]; +} + +/// 关闭录音 +- (void)closeRecord:(void (^)(BOOL isSuccess,NSError *error))finished{ + [[AiMouse shared] closeRecord:finished]; +} @end diff --git a/MouseSdk/MouseSdk/Ble/HidManager.h b/MouseSdk/MouseSdk/Ble/HidManager.h index 94b62f5..823ddfa 100644 --- a/MouseSdk/MouseSdk/Ble/HidManager.h +++ b/MouseSdk/MouseSdk/Ble/HidManager.h @@ -10,15 +10,34 @@ NS_ASSUME_NONNULL_BEGIN -@interface HidManager : NSObject -@property (nonatomic,strong)void (^connectStateCallBack)(BOOL isConnnect);/// 设备是否连上 -@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 +@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);/// 设备信息数据回调 +@property (nonatomic, copy)void (^aiMouseSendCmdDataCallBack)(NSString *data);/// 鼠标按键指令回调 +@property (nonatomic, copy)void (^begainRecoredCallBack)(void);/// 录音开始回调 +@property (nonatomic, copy)void (^recordingCallBack)(NSData *data);/// 录音进行中回调 +@property (nonatomic, copy)void (^finishRecoredCallBack)(NSData *data);/// 录音结束回调 -/// 获取设备信息 -- (void)getDeviceInfo:(void (^)(AiMouseDeviceInfo *deviceInfo,NSError *error))completation; +@property (nonatomic, copy)void (^aiMouseAddLogCallBack)(NSString *log);//输出日志 + ++(HidManager *)shared; -- (void)showLog:(NSString *)log; +- (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 8e78d35..32de6e7 100644 --- a/MouseSdk/MouseSdk/Ble/HidManager.m +++ b/MouseSdk/MouseSdk/Ble/HidManager.m @@ -8,16 +8,18 @@ #import "HidManager.h" #include #include -#import "Config.h" -#import "DeviceManger.h" + +#import "BleManager.h" //供应商id const long vendorID = 0x6675; //产品id const long productID = 0x8601; -@interface HidManager () - +@interface HidManager () +{ + dispatch_block_t _block; +} @property(nonatomic, assign) IOHIDManagerRef hidManager;///hid管理 @property(nonatomic, assign) IOHIDDeviceRef aiCha;//ai通道 @property(nonatomic, assign) IOHIDDeviceRef deviceInfoCha;//设备信息通道 @@ -25,28 +27,57 @@ const long productID = 0x8601; @end @implementation HidManager ++(HidManager *)shared{ + static HidManager *manager = nil; + static dispatch_once_t oncePredicate; + dispatch_once(&oncePredicate, ^{ + manager = [[HidManager alloc]init]; + [manager open]; + }); + return manager; +} - (instancetype)init{ self = [super init]; if (self) { - [self initHid]; - [self open]; + _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; } -- (void)initHid{ - +///开始尝试连接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); } + - (void)open{ + //初始化 + self.deviceInfoCha = nil; + self.aiCha = nil; + + [self showLog:@"(HidManager)open:打开设备"]; self.hidManager = IOHIDManagerCreate(kCFAllocatorDefault, kIOHIDOptionsTypeNone); - IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); + IOHIDManagerScheduleWithRunLoop(self.hidManager, CFRunLoopGetMain(), kCFRunLoopDefaultMode); IOReturn ret = IOHIDManagerOpen(self.hidManager, 0L); if (ret != kIOReturnSuccess) { - NSLog(@"打开hid失败!"); + [self showLog:(@"(HidManager)open:打开设备失败!")]; }else{ - NSLog(@"打开hid成功!"); + [self showLog:(@"(HidManager)open:打开设备成功!")]; } //设置匹配设备 NSMutableDictionary* dict= [NSMutableDictionary dictionary]; @@ -60,99 +91,160 @@ const long productID = 0x8601; } - (void)close{ + if (self.hidManager == nil) { + [self showLog:@"(HidManager)close:设备已关闭"]; + return; + } + [self showLog:@"(HidManager)close:关闭设备"]; IOReturn ret = IOHIDManagerClose(self.hidManager, 0); + self.hidManager = nil; + if (ret != kIOReturnSuccess) { - NSLog(@"关闭设备失败!"); + [self showLog:(@"(HidManager)close:关闭设备失败!")]; }else{ - NSLog(@"关闭设备成功!"); + [self showLog:(@"(HidManager)close:关闭设备成功!")]; } + + self.deviceInfoCha = nil; + self.aiCha = nil; } #pragma mark - Hid CallBack /// 匹配到设备回调 static void hidAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { - [[DeviceManger shared].hid showLog:[NSString stringWithFormat:@"发现设备 %@\n ",inIOHIDDeviceRef]]; + if ([[BleManager shared] isDidConnectBleDevice]) { + [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice:当前处于蓝牙连接状态,对监听到有USB 插入 的事件不做处理")]; + return; + } +// uint32_t vid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDVendorIDKey)); +// uint32_t pid = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDProductIDKey)); uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); - if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ +// [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidAppearDevice : vid = %u, pid = %u, PrimaryUsagePage = %u, PrimaryUsage = %u,",vid,pid,PrimaryUsagePage,PrimaryUsage]]; +// [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidAppearDevice : 发现设备 %@\n ",inIOHIDDeviceRef]]; + + NSString *tmpPrimaryUsagePage = [NSString stringWithFormat:@"%u",PrimaryUsagePage]; + NSString *tmpPrimaryUsage = [NSString stringWithFormat:@"%u",PrimaryUsage]; + if([tmpPrimaryUsagePage isEqualToString:@"1"] && [tmpPrimaryUsage isEqualToString:@"0"]){ +// [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice :设备信息 - 开始鉴权")]; //设备信息 - [DeviceManger shared].hid.deviceInfoCha = inIOHIDDeviceRef; - [[DeviceManger shared].hid connectDevice]; - //注册消息回调 - unsigned char InputBuffer[128]; - IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); - IOReturn ret = IOHIDDeviceOpen(inIOHIDDeviceRef, 0); - if (ret != kIOReturnSuccess) { - NSLog(@"打开设备信息失败!"); - }else{ - NSLog(@"打开设备信息成功!"); + [HidManager shared].deviceInfoCha = inIOHIDDeviceRef; +// //开始鉴权 +// [[HidManager shared] startAuthentication]; + + //按照前端要求,他们自己调用鉴权接口,此处只先设置代理和连接类型 + { //设置代理 + [AiMouse shared].delegate = [HidManager shared]; + //切换当前连接类型 + [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; + } + + [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice:回调连接成功 ")]; + [[HidManager shared] cancelBlock]; + //连上设备 + if ([HidManager shared].connectStateCallBack != nil) { + [HidManager shared].connectStateCallBack(true); } - CFRetain(inIOHIDDeviceRef); - IOHIDDeviceScheduleWithRunLoop(inIOHIDDeviceRef, CFRunLoopGetCurrent(), kCFRunLoopDefaultMode); } - else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ + else if([tmpPrimaryUsagePage isEqualToString:@"65283"] && [tmpPrimaryUsage isEqualToString:@"0"]){ + [[HidManager shared] showLog:(@"(HidManager)hidAppearDevice :发现ai")]; //ai - [DeviceManger shared].hid.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); + [HidManager shared].aiCha = inIOHIDDeviceRef; } + //注册消息回调 + unsigned char InputBuffer[128]; + IOHIDDeviceRegisterInputReportCallback(inIOHIDDeviceRef, InputBuffer, sizeof(InputBuffer), hidReceiveData, NULL); } /// 设备移除回调 static void hidDisAppearDevice(void *inContext,IOReturn inResult,void *inSender,IOHIDDeviceRef inIOHIDDeviceRef) { - [[DeviceManger shared].hid showLog:[NSString stringWithFormat:@"拔出设备 %@ ",inIOHIDDeviceRef]]; + if ([[BleManager shared] isDidConnectBleDevice]) { + [[HidManager shared] showLog:(@"(HidManager)hidDisAppearDevice:当前处于蓝牙连接状态,对监听到有USB 拔出 的事件不做处理")]; + return; + } + + [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)hidDisAppearDevice:拔出设备 %@ ",inIOHIDDeviceRef]]; uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsagePageKey)); uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(inIOHIDDeviceRef, CFSTR(kIOHIDPrimaryUsageKey)); - if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ - [DeviceManger shared].hid.deviceInfoCha = nil; - [DeviceManger shared].hid.aiCha = nil; + NSString *tmpPrimaryUsagePage = [NSString stringWithFormat:@"%u",PrimaryUsagePage]; + NSString *tmpPrimaryUsage = [NSString stringWithFormat:@"%u",PrimaryUsage]; + + if([tmpPrimaryUsagePage isEqualToString:@"1"] && [tmpPrimaryUsage isEqualToString:@"0"]){ + //取消执行超时操作 + [[HidManager shared] showLog:(@"(HidManager)hidDisAppearDevice:回调连接失败 ")]; + [[HidManager shared] cancelBlock]; + if ([HidManager shared].connectStateCallBack != nil) { + [HidManager shared].connectStateCallBack(false); + } + + [HidManager shared].deviceInfoCha = nil; + [HidManager shared].aiCha = nil; + +// [[HidManager shared] aiMouseConnectStateDidChange:false]; [[AiMouse 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){ -// [[DeviceManger shared].hid showLog:[NSString stringWithFormat:@"[receive] 处理前 %@",[AiMouseTools dataToHexString:[[NSData alloc] initWithBytes:report length:reportLength]]]]; + NSInteger len = get_data_len(report, reportLength); NSData *data = [[NSData alloc] initWithBytes:report length:len]; -// [[DeviceManger shared].hid showLog:[NSString stringWithFormat:@"[receive] 处理后 %@",[AiMouseTools dataToHexString:data]]]; uint32_t PrimaryUsagePage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsagePageKey)); uint32_t PrimaryUsage = IOHIDDevGetUInt32Property(sender, CFSTR(kIOHIDPrimaryUsageKey)); if(PrimaryUsagePage == 1 && PrimaryUsage == 0){ + [[HidManager shared] showLog:@"(HidManager)hidReceiveData:device数据"]; [[AiMouse shared] sendDeviceDataToSdk:data]; - } else if(PrimaryUsagePage == 65283 && PrimaryUsage == 0){ + [[HidManager shared] showLog:@"(HidManager)hidReceiveData:AI数据"]; [[AiMouse shared] sendAiDataToSdk:data]; } } #pragma mark - AiDelegate -/// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 -/// - Parameter data: 数据 --(void)aiMouseSendDeviceInfoData:(NSData *)data{ - [self sendData:data toDevice:self.deviceInfoCha]; + +/// 设备连接状态 +/// - Parameter connected: 是否连接 +- (void)aiMouseConnectStateDidChange:(BOOL)connected { + [self showLog:[NSString stringWithFormat:@"(BleManager) aiMouseConnectStateDidChange - 设备连接状态返回: %hhd",connected]]; + + if (self.aiMouseConnectStateDidChangeCallBack) { + self.aiMouseConnectStateDidChangeCallBack(connected); + } + //没有连接 + if (!connected) { + [HidManager shared].deviceInfoCha = nil; + [HidManager shared].aiCha = nil; + } } /// 设备鉴权结果返回 /// - Parameter authState: 鉴权状态 --(void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ - [self showLog:[NSString stringWithFormat:@"鉴权结果 = %@",@(authState)]]; +- (void)aiMouseAuthenticationResult:(AiMouseAuthState)authState{ + [self showLog:[NSString stringWithFormat:@"(HidManager) aiMouseAuthenticationResult - 设备鉴权结果返回: %ld",(long)authState]]; + if(self.authCallBack){ self.authCallBack(authState == AiMouseAuthState_Success); } } + +/// 设备信息数据回调,需要将此数据通过设备信息特征转发给蓝牙设备 +/// - Parameter data: 数据 +-(void)aiMouseSendDeviceInfoData:(NSData *)data{ + [self showLog:[NSString stringWithFormat:@"(HidManager)aiMouseSendDeviceInfoData:[send] %@",data]]; + [self sendData:data toDevice:self.deviceInfoCha]; + if (self.aiMouseSendDeviceInfoDataCallBack) { + self.aiMouseSendDeviceInfoDataCallBack(data); + } +} + /// 鼠标按键指令回调 /// - Parameter data: 数据。 /// 前 第一次单击31(开始录音), 第二次单击32(结束录音) , 按下21(开始录音)抬起22(结束录音) @@ -162,75 +254,108 @@ static void hidReceiveData(void* context, IOReturn result, void* sender, IOHIDRe - (void)aiMouseSendCmdData:(NSData *)data{ uint8 cmd; [data getBytes:&cmd length:1]; - [self showLog:[NSString stringWithFormat:@"收到指令 = %X",cmd]]; + [self showLog:[NSString stringWithFormat:@"(HidManager)aiMouseSendCmdData:收到指令 = %X",cmd]]; + + if (self.aiMouseSendCmdDataCallBack) { + self.aiMouseSendCmdDataCallBack([NSString stringWithFormat:@"%X",cmd]); + } + if(cmd == 0x31 || cmd == 0x21 || cmd == 0x25 ||cmd == 0x27){ - [self showLog:@"开始录音"]; + [self showLog:@"(HidManager)aiMouseSendCmdData:开始录音"]; self.recordData = [[NSMutableData alloc] init]; + + if (self.begainRecoredCallBack) { + self.begainRecoredCallBack(); + } } else if(cmd == 0x32 || cmd == 0x22 || cmd == 0x26 ||cmd == 0x28){ - [self showLog:@"结束录音"]; + [self showLog:@"(HidManager)aiMouseSendCmdData:结束录音"]; [self finishRecored:self.recordData]; + + if (self.finishRecoredCallBack) { + self.finishRecoredCallBack(self.recordData); + } } } /// 录音回调 /// - Parameter data: pcm格式语音数据 - (void)aiMouseSendRecordData:(NSData *)data{ + [self showLog:[NSString stringWithFormat:@"(HidManager)aiMouseSendRecordData: %@",data]]; [self.recordData appendData:data]; + if (self.recordingCallBack) { + self.recordingCallBack(data); + } } -/// 输出日志 + - (void)aiMouseAddLog:(NSString *)log { -// [self showLog:log]; + if (self.aiMouseAddLogCallBack) { + self.aiMouseAddLogCallBack(log); + } } --(void)hidDataError{ +/// hid数据异常,需要重新开启监听usb设备 +- (void)hidDataError { + //收到数据异常,需要关闭后重新打开 [self close]; [self open]; } -// 连接状态改变 -- (void)aiMouseConnectStateDidChange:(BOOL)connected{ - [DeviceManger shared].hid.connectStateCallBack(connected); -} #pragma mark - public --(void)connectDevice{ - //设置代理 - [AiMouse shared].delegate = self; - //切换当前连接类型 - [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; -} +//-(void)startAuthentication{ +// //设置代理 +// [AiMouse shared].delegate = self; +// //切换当前连接类型 +// [[AiMouse shared] switchConnect:AiMouseConnectType_hid]; +// //找到鉴权特征后,一定要调用鉴权接口开始鉴权。只有鉴权通过了,sdk相关功能才可以使用 +// [self showLog:@"(HidManager)开始鉴权"]; +// [[AiMouse 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: 获取设备信息"]; [[AiMouse shared] getDeviceInfo:completation]; } +/// 获取信号强度 +- (void)getRSSI:(void (^)(NSString *value,NSError *error))complete { + [self showLog:[NSString stringWithFormat:@"(HidManager) getRSSI:获取信号强度"]]; + [[AiMouse shared] getRSSI:complete]; +} + /// 发送数据给设备 -(void)sendData:(NSData *)data toDevice:(IOHIDDeviceRef)device{ if(device == nil){ - [self showLog:@"请先连上设备"]; + [self showLog:@"(HidManager)sendData: 请先连上设备"]; return; } IOReturn ret = IOHIDDeviceSetReport(device, kIOHIDReportTypeOutput, 0, data.bytes, data.length); if (ret != kIOReturnSuccess) { - [self showLog:[NSString stringWithFormat:@"数据发送失败! code = %d",ret]]; + [self showLog:[NSString stringWithFormat:@"(HidManager)sendData: 发送失败! code = %d",ret]]; }else{ - [self showLog:@"数据发送成功!"]; + [self showLog:@"(HidManager)sendData: 发送成功!"]; } } //将录音数据保存 - (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]]; - } +// 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]]; +// } } + - (void)showLog:(NSString *)log{ NSLog(@"%@",log); } @@ -251,10 +376,11 @@ static uint32_t IOHIDDevGetUInt32Property(IOHIDDeviceRef inIOHIDDeviceRef, CFStr if (tCFTypeRef != NULL && (CFNumberGetTypeID() == CFGetTypeID(tCFTypeRef)) ) { Boolean result = CFNumberGetValue((CFNumberRef) tCFTypeRef, kCFNumberSInt32Type, &outValue); if (!result) { - [[DeviceManger shared].hid showLog:[NSString stringWithFormat:@"get %@ failed.", inKey]]; + [[HidManager shared] showLog:[NSString stringWithFormat:@"(HidManager)IOHIDDevGetUInt32Property:get %@ failed.", inKey]]; } } } return outValue; } + @end diff --git a/MouseSdk/MouseSdk/FrkVC.m b/MouseSdk/MouseSdk/FrkVC.m index 449d613..47ee21c 100644 --- a/MouseSdk/MouseSdk/FrkVC.m +++ b/MouseSdk/MouseSdk/FrkVC.m @@ -26,14 +26,14 @@ } -(void)dealloc{ - [[DeviceManger shared] stop]; +// [[DeviceManger shared] stop]; } - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. - [[DeviceManger shared] start]; +// [[DeviceManger shared] start]; [self setupUI]; [self setupDeviceManger]; } @@ -160,9 +160,11 @@ manager.authCallBack = ^(BOOL isSuccess) { [ws showAuthResult:isSuccess]; }; - manager.connectStateCallBack = ^(AiMouseConnectType type,BOOL isConnected) { - [ws connectStateDidChange:type connectState:isConnected]; - }; +// manager.connectStateCallBack = ^(AiMouseConnectType type,BOOL isConnected) { +// [ws connectStateDidChange:type connectState:isConnected]; +// }; + + NSLog(@"当前连接类型 = %ld",[manager getCurrentConnectType]); } @end diff --git a/MouseSdk/MouseSdk/ScanVC.m b/MouseSdk/MouseSdk/ScanVC.m index d63b398..783160b 100644 --- a/MouseSdk/MouseSdk/ScanVC.m +++ b/MouseSdk/MouseSdk/ScanVC.m @@ -8,6 +8,7 @@ #import "ScanVC.h" #import "ScanCell.h" #import "DeviceManger.h" +#import "BleManager.h" @interface ScanVC () @property (nonatomic,strong) NSTableView *tableView; @property (nonatomic,strong) NSMutableArray *scanArray; @@ -67,7 +68,7 @@ } - (void)setupBleManger{ - BleManager *manager = [DeviceManger shared].ble; + BleManager *manager = [BleManager shared]; __weak typeof(self) weakSelf= self; __weak typeof(BleManager) *weakManager = manager; manager.discoverDevice = ^(CBPeripheral *peripheral){ @@ -87,11 +88,11 @@ } - (void)tapDevice:(CBPeripheral *)peripheral{ - [[DeviceManger shared].ble connectPeripheral:peripheral]; + [[BleManager shared] connectPeripheral:peripheral]; } - (void)connectSuccess{ - [[DeviceManger shared].ble stopScanDevice]; + [[BleManager shared] stopScanDevice]; dispatch_async(dispatch_get_main_queue(), ^{ NSAlert *alert = [NSAlert new]; [alert addButtonWithTitle:@"确定"]; diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 3a0fec8..4b89781 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -10,7 +10,7 @@ #import "TlwVC.h" #import "ScanVC.h" #import "TlwHidManager.h" - +#import "HidManager.h" @interface ViewController () @end @@ -53,12 +53,14 @@ #pragma mark - action - (void)frkAction{ +// [HidManager shared]; +// return; FrkVC *vc = [[FrkVC alloc] initWithNibName:nil bundle:nil]; [self presentViewControllerAsModalWindow:vc]; } - (void)tlwAction{ - [TlwHidManager shared]; - return; +// [TlwHidManager shared]; +// return; TlwVC *vc = [[TlwVC alloc] initWithNibName:nil bundle:nil]; [self presentViewControllerAsModalWindow:vc];