From: liangzhenying <867174124@qq.com> Date: Mon, 18 Mar 2024 01:50:09 +0000 (+0800) Subject: demo同步修改断线重连 X-Git-Tag: 阿里鉴权~16 X-Git-Url: http://git.snicker.com.cn/?a=commitdiff_plain;h=453c451e72fc1879399739b8b6491a044f573d52;p=mirrors%2FMouseSdk-MacOS.git demo同步修改断线重连 --- diff --git "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" index bcf2b42..7286543 100644 --- "a/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" +++ "b/Document/AiMouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" @@ -48,6 +48,7 @@ ```objective-c +/// 鉴权状态回调 /// - Parameter isSuccess:是否鉴权成功 - (void)aiMouseAuthCallBack:(BOOL)isSuccess; @@ -56,7 +57,7 @@ - (void)aiMouseSendAuthData:(NSData *)data; /// 鼠标按键指令回调 -/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知 +/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义 - (void)aiMouseSendCmdData:(NSData *)data; /// 录音回调 @@ -74,7 +75,7 @@ //ai特征 #define kAiUUID @"0783B03E-8535-B5A0-7140-A304D2194CB8" //鉴权特征 - #define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7" + #define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7" ``` @@ -147,10 +148,10 @@ [self showLog:[NSString stringWithFormat:@"[send] %@",[AiTools dataToHexString:data]]]; [self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse]; } - /// 指令回调 - /// - Parameter data: 数据 + /// 鼠标按键指令回调 + /// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义 - (void)aiMouseSendCmdData:(NSData *)data{ - NSString *cmd = [AiTools dataToHexString:data]; + NSString *cmd = [[AiTools dataToHexString:data] lowercaseString]; if([cmd hasSuffix:@"31"]){ [self showLog:@"录音键开始录音"]; self.recordData = [[NSMutableData alloc] init]; @@ -167,20 +168,22 @@ [self showLog:@"AI键结束录音"]; [self finishRecored:self.recordData]; } + else{ + [self showLog:@"未定义指令!!!!!!!!"]; + } } /// 录音回调 /// - Parameter data: pcm格式语音数据 - (void)aiMouseSendRecordData:(NSData *)data{ [self.recordData appendData:data]; } - ``` - + ## 数据交互流程 -鉴权特征数据走鉴权接口,AI特征数据走AI接口 +鉴权特征数据走鉴权接口,AI特征数据走AI接口![数据交互流程](/Users/apple/Desktop/code/MouseSdk-iOS/Document/数据交互流程.png) + -![数据交互流程](/Users/apple/Desktop/数据交互流程.png) diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 9bae047..fe108e5 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/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.pbxproj b/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.pbxproj index 821fa67..bbae49d 100644 --- a/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.pbxproj +++ b/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.pbxproj @@ -15,14 +15,14 @@ 18AE9D502B9AFE7C00743515 /* MouseSdkDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D4F2B9AFE7C00743515 /* MouseSdkDemoTests.m */; }; 18AE9D5A2B9AFE7C00743515 /* MouseSdkDemoUITests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D592B9AFE7C00743515 /* MouseSdkDemoUITests.m */; }; 18AE9D5C2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D5B2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m */; }; - 18AE9D6E2B9B017C00743515 /* AiMouseSdk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */; }; - 18AE9D6F2B9B017C00743515 /* AiMouseSdk.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; - 18AE9D712B9B017E00743515 /* AliIotConnectKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */; }; - 18AE9D722B9B017E00743515 /* AliIotConnectKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 18AE9D7B2B9B02BC00743515 /* BleManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D732B9B02BB00743515 /* BleManager.m */; }; 18AE9D7C2B9B02BC00743515 /* ScanVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D762B9B02BC00743515 /* ScanVC.m */; }; 18AE9D7D2B9B02BC00743515 /* ScanCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D772B9B02BC00743515 /* ScanCell.m */; }; 18AE9D7E2B9B02BC00743515 /* Config.m in Sources */ = {isa = PBXBuildFile; fileRef = 18AE9D7A2B9B02BC00743515 /* Config.m */; }; + 18BF8F2F2BA7D45B008F6933 /* AiMouseSdk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */; }; + 18BF8F302BA7D45B008F6933 /* AiMouseSdk.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; + 18BF8F312BA7D45D008F6933 /* AliIotConnectKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */; }; + 18BF8F322BA7D45D008F6933 /* AliIotConnectKit.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; /* End PBXBuildFile section */ /* Begin PBXContainerItemProxy section */ @@ -49,8 +49,8 @@ dstPath = ""; dstSubfolderSpec = 10; files = ( - 18AE9D722B9B017E00743515 /* AliIotConnectKit.framework in Embed Frameworks */, - 18AE9D6F2B9B017C00743515 /* AiMouseSdk.framework in Embed Frameworks */, + 18BF8F322BA7D45D008F6933 /* AliIotConnectKit.framework in Embed Frameworks */, + 18BF8F302BA7D45B008F6933 /* AiMouseSdk.framework in Embed Frameworks */, ); name = "Embed Frameworks"; runOnlyForDeploymentPostprocessing = 0; @@ -72,8 +72,6 @@ 18AE9D552B9AFE7C00743515 /* MouseSdkDemoUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = MouseSdkDemoUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; 18AE9D592B9AFE7C00743515 /* MouseSdkDemoUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkDemoUITests.m; sourceTree = ""; }; 18AE9D5B2B9AFE7C00743515 /* MouseSdkDemoUITestsLaunchTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = MouseSdkDemoUITestsLaunchTests.m; sourceTree = ""; }; - 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AliIotConnectKit.framework; sourceTree = ""; }; - 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AiMouseSdk.framework; sourceTree = ""; }; 18AE9D732B9B02BB00743515 /* BleManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BleManager.m; sourceTree = ""; }; 18AE9D742B9B02BB00743515 /* Config.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Config.h; sourceTree = ""; }; 18AE9D752B9B02BC00743515 /* ScanCell.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScanCell.h; sourceTree = ""; }; @@ -82,6 +80,8 @@ 18AE9D782B9B02BC00743515 /* BleManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BleManager.h; sourceTree = ""; }; 18AE9D792B9B02BC00743515 /* ScanVC.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ScanVC.h; sourceTree = ""; }; 18AE9D7A2B9B02BC00743515 /* Config.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Config.m; sourceTree = ""; }; + 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AiMouseSdk.framework; sourceTree = ""; }; + 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = AliIotConnectKit.framework; sourceTree = ""; }; /* End PBXFileReference section */ /* Begin PBXFrameworksBuildPhase section */ @@ -89,8 +89,8 @@ isa = PBXFrameworksBuildPhase; buildActionMask = 2147483647; files = ( - 18AE9D712B9B017E00743515 /* AliIotConnectKit.framework in Frameworks */, - 18AE9D6E2B9B017C00743515 /* AiMouseSdk.framework in Frameworks */, + 18BF8F312BA7D45D008F6933 /* AliIotConnectKit.framework in Frameworks */, + 18BF8F2F2BA7D45B008F6933 /* AiMouseSdk.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -175,8 +175,8 @@ 18AE9D692B9B008400743515 /* framework */ = { isa = PBXGroup; children = ( - 18AE9D6A2B9B008400743515 /* AliIotConnectKit.framework */, - 18AE9D6B2B9B008400743515 /* AiMouseSdk.framework */, + 18BF8F2B2BA7D456008F6933 /* AiMouseSdk.framework */, + 18BF8F2C2BA7D456008F6933 /* AliIotConnectKit.framework */, ); path = framework; sourceTree = ""; diff --git a/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 1a24a66..1ab8209 100644 Binary files a/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate and b/MouseSdkDemo/MouseSdkDemo.xcodeproj/project.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate differ diff --git a/MouseSdkDemo/MouseSdkDemo/BleManager.h b/MouseSdkDemo/MouseSdkDemo/BleManager.h index f7b6b75..6e3749e 100644 --- a/MouseSdkDemo/MouseSdkDemo/BleManager.h +++ b/MouseSdkDemo/MouseSdkDemo/BleManager.h @@ -7,22 +7,31 @@ #import #import + +#define kDidConnectBleNotification @"kDidConnectBleNotification" +#define kDidDisconnectBleNotification @"kDidDisconnectBleNotification" + NS_ASSUME_NONNULL_BEGIN @interface BleManager : NSObject -@property (nonatomic,strong)void (^discoverDevice)(CBPeripheral *peripheral,NSDictionary *advertisementData) ; -@property (nonatomic,strong)void (^didConnectDevice)(CBPeripheral *peripheral); -@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess); +@property (nonatomic,strong)void (^discoverDevice)(CBPeripheral *peripheral) ;/// 扫描到设备 +@property (nonatomic,strong)void (^didConnectDevice)(CBPeripheral *peripheral);/// 连接成功 +@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess);/// 鉴权回调 -+(id)shared; ++(BleManager *)shared; +/// 扫描 - (void)startScanDevice; +/// 停止扫描 - (void)stopScanDevice; +/// 连接设备 +/// - Parameter peripheral: 蓝牙设备 - (void)connectPeripheral:(CBPeripheral *)peripheral; -- (void)startAuth; +/// 开始鉴权 +- (void)startAuthentication; @end NS_ASSUME_NONNULL_END diff --git a/MouseSdkDemo/MouseSdkDemo/BleManager.m b/MouseSdkDemo/MouseSdkDemo/BleManager.m index 5e06da0..e33a4f4 100644 --- a/MouseSdkDemo/MouseSdkDemo/BleManager.m +++ b/MouseSdkDemo/MouseSdkDemo/BleManager.m @@ -9,26 +9,24 @@ #import #import "Config.h" - +// 服务id +#define kServiceUUID @"FFF1" //ai特征 #define kAiUUID @"0783B03E-8535-B5A0-7140-A304D2194CB8" //鉴权特征 -#define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7" -//#define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7" - - +//#define kAuthUUID @"0783B73E-8535-B5A0-7140-A304D2194CB7" +#define kAuthUUID @"0783B03E-8535-B5A0-7140-A304D2194CB7" @interface BleManager () -@property (nonatomic,strong) NSMutableArray *scanArray; -@property(nonatomic, strong) CBCentralManager *centralManager; -@property(nonatomic, strong) CBPeripheral *connectPeripheral; -@property(nonatomic, strong) CBCharacteristic *authCha; -@property(nonatomic, strong) CBCharacteristic *aiCha; -@property (nonatomic,strong)NSMutableData *recordData; +@property(nonatomic, strong) CBCentralManager *centralManager;///蓝牙管理 +@property(nonatomic, strong) CBPeripheral *connectPeripheral;//连接的设备 +@property(nonatomic, strong) CBCharacteristic *authCha;//鉴权特征 +@property(nonatomic, strong) CBCharacteristic *aiCha;//ai特征 +@property (nonatomic,strong) NSMutableData *recordData;//录音的数据 @end @implementation BleManager -+(id)shared{ ++(BleManager *)shared{ static BleManager *manager = nil; static dispatch_once_t oncePredicate; dispatch_once(&oncePredicate, ^{ @@ -37,13 +35,6 @@ return manager; } --(NSMutableArray *)scanArray{ - if (_scanArray == nil) { - _scanArray = [[NSMutableArray alloc] init]; - } - return _scanArray; -} - - (instancetype)init{ self = [super init]; if (self) { @@ -55,8 +46,8 @@ #pragma mark - CBCentralManagerDelegate - (void)centralManagerDidUpdateState:(nonnull CBCentralManager *)central { + [self showLog:[NSString stringWithFormat:@"设备状态 = %@",@(central.state)]]; switch (central.state) { - case CBManagerStateUnknown: break; @@ -73,47 +64,46 @@ break; case CBManagerStatePoweredOn: -// [self startScanDevice]; + [self autoConnectDevice]; break; } } - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ - if ([self.scanArray containsObject:peripheral]){ - return; - } - if(peripheral.name == nil){ - return; - } - [self showLog:peripheral.name]; - [self.scanArray insertObject:peripheral atIndex:0]; +// [self showLog:[NSString stringWithFormat:@"扫描到设备 = %@, data = %@",peripheral,advertisementData]]; if(self.discoverDevice){ - self.discoverDevice(peripheral, advertisementData); + self.discoverDevice(peripheral); } } - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral{ + [self showLog:[NSString stringWithFormat:@"连接成功 name = %@",peripheral.name]]; if(self.didConnectDevice){ self.didConnectDevice(peripheral); } - - self.connectPeripheral = peripheral; peripheral.delegate = self; [peripheral discoverServices:nil]; + + [[NSNotificationCenter defaultCenter] postNotificationName:kDidConnectBleNotification object:nil]; } -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ [self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]]; + [[NSNotificationCenter defaultCenter] postNotificationName:kDidDisconnectBleNotification object:nil]; + + [self autoConnectDevice]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]]; } #pragma mark - CBPeripheralDelegate +//发现服务 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { for (CBService *service in peripheral.services){ [peripheral discoverCharacteristics:nil forService:service]; } } +// 发现特征 - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { for (CBCharacteristic *characteristic in service.characteristics){ if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID]) { @@ -126,7 +116,7 @@ } } } - +//收到蓝牙数据 - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { NSData *data = characteristic.value; [self showLog:[NSString stringWithFormat:@"[receive] %@",[AiTools dataToHexString:data]]]; @@ -156,7 +146,7 @@ [self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse]; } /// 鼠标按键指令回调 -/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知 +/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义 - (void)aiMouseSendCmdData:(NSData *)data{ NSString *cmd = [[AiTools dataToHexString:data] lowercaseString]; if([cmd hasSuffix:@"31"]){ @@ -175,6 +165,9 @@ [self showLog:@"AI键结束录音"]; [self finishRecored:self.recordData]; } + else{ + [self showLog:@"未定义指令!!!!!!!!"]; + } } /// 录音回调 /// - Parameter data: pcm格式语音数据 @@ -185,7 +178,6 @@ #pragma mark - public /// 连接设备 - (void)startScanDevice{ - [self.scanArray removeAllObjects]; [self.centralManager scanForPeripheralsWithServices:nil options:@{CBCentralManagerScanOptionAllowDuplicatesKey : [NSNumber numberWithBool:NO]}]; } @@ -196,20 +188,36 @@ /// 连接设备 - (void)connectPeripheral:(CBPeripheral *)peripheral{ + self.connectPeripheral = peripheral; [self.centralManager connectPeripheral:peripheral options:@{CBConnectPeripheralOptionNotifyOnConnectionKey : [NSNumber numberWithBool:YES]}]; } /// 开始鉴权 -- (void)startAuth{ +- (void)startAuthentication{ [[AiMouse shared] startAuthentication]; } #pragma mark - other +//自动连接,获取已配对的蓝牙,取第一个连接 +- (void)autoConnectDevice{ + //如果是已连接或者正在连接,则不再重连 + if (self.connectPeripheral && (self.connectPeripheral.state == CBPeripheralStateConnected || self.connectPeripheral.state == CBPeripheralStateConnecting)) { + return; + } + NSArray *devices = [self.centralManager retrieveConnectedPeripheralsWithServices:@[[CBUUID UUIDWithString:kServiceUUID]]]; + for (CBPeripheral *peripheral in devices) { + [self showLog:[NSString stringWithFormat:@"匹配过的设备%@",peripheral.name]]; + [self connectPeripheral:peripheral]; + break; + } + // 10s自动重连 + [self performSelector:@selector(autoConnectDevice) withObject:nil afterDelay:10]; +} - (void)showLog:(NSString *)log{ NSLog(@"%@",log); } -//拿到录音数据,可以上传调用AI接口处理 +//将录音数据保存 - (void)finishRecored:(NSData *)data{ NSString *doc = NSTemporaryDirectory(); NSString *fullPath = [NSString stringWithFormat:@"%@1.pcm",doc]; diff --git a/MouseSdkDemo/MouseSdkDemo/ScanCell.h b/MouseSdkDemo/MouseSdkDemo/ScanCell.h index bb8a265..6bf06ee 100644 --- a/MouseSdkDemo/MouseSdkDemo/ScanCell.h +++ b/MouseSdkDemo/MouseSdkDemo/ScanCell.h @@ -11,7 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @interface ScanCell : NSTableCellView @property(nonatomic,strong)CBPeripheral *peripheral; -@property(nonatomic,strong)void (^connecCallBack)(CBPeripheral *peripheral); +@property(nonatomic,strong)void (^connecBtnActionCallBack)(CBPeripheral *peripheral); @end NS_ASSUME_NONNULL_END diff --git a/MouseSdkDemo/MouseSdkDemo/ScanCell.m b/MouseSdkDemo/MouseSdkDemo/ScanCell.m index a0bf16f..dae4419 100644 --- a/MouseSdkDemo/MouseSdkDemo/ScanCell.m +++ b/MouseSdkDemo/MouseSdkDemo/ScanCell.m @@ -42,8 +42,6 @@ btn.state = 1; [self addSubview:btn]; self.connectBtn = btn; - - } -(void)setPeripheral:(CBPeripheral *)peripheral{ @@ -56,8 +54,8 @@ } - (void)connetAction{ - if (self.connecCallBack) { - self.connecCallBack(self.peripheral); + if (self.connecBtnActionCallBack) { + self.connecBtnActionCallBack(self.peripheral); } } @end diff --git a/MouseSdkDemo/MouseSdkDemo/ScanVC.m b/MouseSdkDemo/MouseSdkDemo/ScanVC.m index 22a1d85..4ba2d18 100644 --- a/MouseSdkDemo/MouseSdkDemo/ScanVC.m +++ b/MouseSdkDemo/MouseSdkDemo/ScanVC.m @@ -24,15 +24,14 @@ - (void)viewDidLoad { [super viewDidLoad]; // Do view setup here. - self.title = @"扫描设备"; - [self tableViewSetting]; - [self setupBle]; + [self setupUI]; + [self setupBleManger]; } - (void)loadView{ self.view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; } -- (void)tableViewSetting{ +- (void)setupUI{ CALayer *viewLayer = [CALayer layer]; NSView *backgroundView= [[NSView alloc]init]; [backgroundView setWantsLayer:YES]; @@ -41,11 +40,13 @@ [backgroundView setNeedsDisplay:YES]; [self.view addSubview:backgroundView]; + //实现tableview的滚动效果 NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:self.view.bounds]; scrollView.backgroundColor = [NSColor redColor]; - [scrollView setDrawsBackground:NO]; - [scrollView setHasVerticalScroller:YES]; - scrollView.autohidesScrollers = YES; + [scrollView setDrawsBackground:NO];//不画背景(背景默认画成白色) + [scrollView setHasVerticalScroller:YES];//有垂直滚动条 + //[_tableContainer setHasHorizontalScroller:YES]; //有水平滚动条 + scrollView.autohidesScrollers = YES;//自动隐藏滚动条(滚动的时候出现) [self.view addSubview:scrollView]; self.tableView = [[NSTableView alloc] initWithFrame:scrollView.bounds]; @@ -55,17 +56,20 @@ column1.title = @"设备名称"; [column1 setWidth:self.tableView.bounds.size.width]; [self.tableView addTableColumn:column1]; - + +// self.tableView.focusRingType = NSFocusRingTypeNone;//tableview获得焦点时的风格 +// self.tableView.selectionHighlightStyle = NSTableViewSelectionHighlightStyleRegular;//行高亮的风格 +// self.tableView.backgroundColor = [NSColor greenColor]; +// self.tableView.usesAlternatingRowBackgroundColors = YES; //背景颜色的交替,一行白色,一行灰色。设置后,原来设置的 backgroundColor 就无效了。 self.tableView.delegate = self; self.tableView.dataSource = self; self.tableView.gridStyleMask = NSTableViewSolidHorizontalGridLineMask; } -- (void)setupBle{ +- (void)setupBleManger{ BleManager *manager = [BleManager shared]; __weak typeof(self) weakSelf= self; - __weak typeof(BleManager) *weakManager = manager; - manager.discoverDevice = ^(CBPeripheral *peripheral,NSDictionary *advertisementData){ + manager.discoverDevice = ^(CBPeripheral *peripheral){ if ([weakSelf.scanArray containsObject:peripheral]){ return; } @@ -74,33 +78,51 @@ } [weakSelf.scanArray addObject:peripheral]; [weakSelf.tableView reloadData]; -// if([peripheral.name isEqualToString:@"iMouse"]){ -// [weakManager connectPeripheral:peripheral]; -// [weakManager stopScanDevice]; -// } }; manager.didConnectDevice = ^(CBPeripheral * _Nonnull peripheral) { - NSLog(@"连接成功"); - [weakManager stopScanDevice]; - [weakSelf dismissViewController:weakSelf]; + [weakSelf connectSuccess]; }; [manager startScanDevice]; } -- (void)connectDevice:(CBPeripheral *)peripheral{ +- (void)tapDevice:(CBPeripheral *)peripheral{ [[BleManager shared] connectPeripheral:peripheral]; } + +- (void)connectSuccess{ + [[BleManager shared] stopScanDevice]; + dispatch_async(dispatch_get_main_queue(), ^{ + NSAlert *alert = [NSAlert new]; + [alert addButtonWithTitle:@"确定"]; + [alert setMessageText:@"连接成功"]; + [alert setAlertStyle:NSAlertStyleInformational]; + [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) { + [self dismissViewController:self]; + }]; + }); +} #pragma mark - NSTableViewDelegate,NSTableViewDataSource -(NSInteger)numberOfRowsInTableView:(NSTableView *)tableView{ return self.scanArray.count; } -(NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row{ +// NSString *columnID = tableColumn.identifier; +// NSString *strIdt = @"NSTableCellView"; +// NSTableCellView *cell = [tableView makeViewWithIdentifier:strIdt owner:self]; +// if (!cell) { +// cell = [[NSTableCellView alloc]init]; +// cell.identifier = strIdt; +// } +// cell.wantsLayer = YES; +// cell.textField.stringValue = [NSString stringWithFormat:@"cell %ld",(long)row]; +// +// return cell; ScanCell *cell = [[ScanCell alloc] initWithFrame:CGRectMake(0, 0, self.tableView.bounds.size.width, 44)]; cell.peripheral = self.scanArray[row]; __weak typeof(self) weakSelf = self; - cell.connecCallBack = ^(CBPeripheral * _Nonnull peripheral) { - [weakSelf connectDevice:peripheral]; + cell.connecBtnActionCallBack = ^(CBPeripheral * _Nonnull peripheral) { + [weakSelf tapDevice:peripheral]; }; return cell; } diff --git a/MouseSdkDemo/MouseSdkDemo/ViewController.m b/MouseSdkDemo/MouseSdkDemo/ViewController.m index 6fa4e18..8dd850e 100644 --- a/MouseSdkDemo/MouseSdkDemo/ViewController.m +++ b/MouseSdkDemo/MouseSdkDemo/ViewController.m @@ -11,19 +11,32 @@ @interface ViewController () +@property(nonatomic,strong)NSTextField *connectLabel; +@property(nonatomic,strong)NSTextField *autoLabel; @end @implementation ViewController - - - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. + [self setupUI]; + [self setupNotification]; + [self setupBleManger]; +} + +- (void)setRepresentedObject:(id)representedObject { + [super setRepresentedObject:representedObject]; + + // Update the view, if already loaded. +} + +- (void)setupUI{ self.title = @"AI SDK"; + { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 100, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 120, 100, 45)]; btn.bordered = true; btn.title = @"扫描"; [btn setTarget:self]; @@ -32,7 +45,7 @@ [self.view addSubview:btn]; } { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 150, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 200, 100, 45)]; btn.bordered = true; btn.title = @"鉴权"; [btn setTarget:self]; @@ -40,20 +53,24 @@ btn.state = 1; [self.view addSubview:btn]; } - [self setupBleManger]; -} - - -- (void)setRepresentedObject:(id)representedObject { - [super setRepresentedObject:representedObject]; - - // Update the view, if already loaded. -} - -- (void)scanAction{ - ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil]; - [self presentViewControllerAsModalWindow:vc]; -// [[BleManager shared] startScanDevice]; + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 120, 200, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"连接状态:未连接"; + label.backgroundColor = NSColor.clearColor; + self.connectLabel = label; + [self.view addSubview:label]; + } + { + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(230, self.view.bounds.size.height - 200, 200, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"鉴权状态:--"; + label.backgroundColor = NSColor.clearColor; + self.autoLabel = label; + [self.view addSubview:label]; + } } - (void)setupBleManger{ BleManager *manager = [BleManager shared]; @@ -63,18 +80,31 @@ }; } +- (void)scanAction{ + ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil]; + [self presentViewControllerAsModalWindow:vc]; +} + - (void)checkAuth{ - [[BleManager shared] startAuth]; + [[BleManager shared] startAuthentication]; } - (void)showAuthResult:(BOOL)isSuccess{ dispatch_async(dispatch_get_main_queue(), ^{ - NSAlert *alert = [NSAlert new]; - [alert addButtonWithTitle:@"确定"]; - [alert setMessageText:isSuccess?@"鉴权成功":@"鉴权失败"]; - [alert setAlertStyle:NSAlertStyleInformational]; - [alert beginSheetModalForWindow:[self.view window] completionHandler:^(NSModalResponse returnCode) { - }]; + self.autoLabel.stringValue = isSuccess?@"鉴权结果:鉴权成功":@"鉴权结果:鉴权失败"; }); } + +#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.connectLabel.stringValue = @"连接状态:已连接"; +} +- (void)bleDidDisconnect:(NSNotification *)notification{ + self.connectLabel.stringValue = @"连接状态:未连接"; +} @end diff --git a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/AiMouseSdk b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/AiMouseSdk index 3160925..bee6223 100755 Binary files a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/AiMouseSdk and b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/AiMouseSdk differ diff --git a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/Headers/AiMouse.h b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/Headers/AiMouse.h index 0ae61f6..3ba2ace 100644 --- a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/Headers/AiMouse.h +++ b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/Headers/AiMouse.h @@ -20,7 +20,7 @@ NS_ASSUME_NONNULL_BEGIN - (void)aiMouseSendAuthData:(NSData *)data; /// 鼠标按键指令回调 -/// - Parameter data: 数据。AI键0x42单击,0x25按下,0x26抬起 ; 录音键0x51单击,0x32按下。0x31(未知),其他未知 +/// - Parameter data: 数据。AI键0x42 单击 0x25 按下(开始录音) 0x26抬起(结束录音); 语音键0x31开始录音,0x32结束录音;OCR 按键 单击 0x41;其他未定义 - (void)aiMouseSendCmdData:(NSData *)data; /// 录音回调 diff --git a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/_CodeSignature/CodeResources b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/_CodeSignature/CodeResources index 1800b44..5096994 100644 --- a/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/_CodeSignature/CodeResources +++ b/MouseSdkDemo/framework/AiMouseSdk.framework/Versions/A/_CodeSignature/CodeResources @@ -15,7 +15,7 @@ hash2 - JyvxVxrmWssBwECs5Ix0vn7n5HUulWxcOT7uWBUWoI0= + fZL1gyEtDIrBtKXvRHJ+p9DtmNnDDuR+hRWmsxYGAuI= Headers/AiMouseSdk.h diff --git a/MouseSdkDemo/framework/AliIotConnectKit.framework/Versions/A/AliIotConnectKit b/MouseSdkDemo/framework/AliIotConnectKit.framework/Versions/A/AliIotConnectKit index c71ae30..37ce0d5 100755 Binary files a/MouseSdkDemo/framework/AliIotConnectKit.framework/Versions/A/AliIotConnectKit and b/MouseSdkDemo/framework/AliIotConnectKit.framework/Versions/A/AliIotConnectKit differ