From: liangzhenying <867174124@qq.com> Date: Fri, 1 Mar 2024 12:01:17 +0000 (+0800) Subject: 设备鉴权调试 X-Git-Tag: 阿里鉴权~32 X-Git-Url: http://git.snicker.com.cn/sitemap.xml?a=commitdiff_plain;h=2f3afd3221d57dbbc834184489f00937e2f3a368;p=mirrors%2FMouseSdk-MacOS.git 设备鉴权调试 --- diff --git a/Document/1.pcm b/Document/1.pcm index b7357d6..bce1216 100644 Binary files a/Document/1.pcm and b/Document/1.pcm differ diff --git a/Document/2.pcm b/Document/2.pcm new file mode 100644 index 0000000..85cb61b Binary files /dev/null and b/Document/2.pcm differ diff --git a/MouseSdk/AiSdk/AiIot.h b/MouseSdk/AiSdk/AiIot.h index b8c68c7..a6c1333 100644 --- a/MouseSdk/AiSdk/AiIot.h +++ b/MouseSdk/AiSdk/AiIot.h @@ -11,10 +11,9 @@ NS_ASSUME_NONNULL_BEGIN @protocol AiIotDelegate -/// 配置SDK,每次连接上蓝牙设备后调用 -//- (void)configSDK; - - +/// 鉴权状态回调 +/// - Parameter isSuccess:是否鉴权成功 +- (void)aiIotAuthStatus:(BOOL)isSuccess; /// 发送给蓝牙设备 /// - Parameter data: 传输的数据 @@ -25,10 +24,9 @@ NS_ASSUME_NONNULL_BEGIN @interface AiIot : NSObject @property (nonatomic, weak) id delegate; +/// 单利 +(AiIot *)shared; -- (void)startLogin; - /// 将数据传输给sdk处理 /// - Parameter data: 传输的数据 - (void)sendToSdk:(NSData *)data; diff --git a/MouseSdk/AiSdk/AiIot.m b/MouseSdk/AiSdk/AiIot.m index 800278f..9b2361e 100644 --- a/MouseSdk/AiSdk/AiIot.m +++ b/MouseSdk/AiSdk/AiIot.m @@ -11,7 +11,7 @@ #import "AiIotTools.h" @interface AiIot() -@property (nonatomic, strong) AiIotConnectManager *loginManager; +@property (nonatomic, strong) AiIotConnectManager *authManager; @property (nonatomic, strong) AiIotAudioManager *audioManager; @end @@ -25,15 +25,18 @@ return manager; } --(AiIotConnectManager *)loginManager{ - if (!_loginManager) { - _loginManager = [[AiIotConnectManager alloc] init]; +-(AiIotConnectManager *)authManager{ + if (!_authManager) { + _authManager = [[AiIotConnectManager alloc] init]; __weak typeof(self) ws = self; - _loginManager.outputData = ^(NSData * _Nonnull data) { + _authManager.outputData = ^(NSData * _Nonnull data) { [ws willSendToBle:data]; }; + _authManager.authCallBack = ^(BOOL isSuccess) { + + }; } - return _loginManager; + return _authManager; } -(AiIotAudioManager *)audioManager{ if (!_audioManager) { @@ -50,7 +53,7 @@ } - (void)startLogin{ - [self.loginManager login]; + [self.authManager login]; } - (void)sendToSdk:(NSData *)data{ @@ -58,6 +61,9 @@ if([str hasPrefix:@"0affff64"] || [str hasPrefix:@"9c31"]){ //声音 [self.audioManager receiveData:data]; + } + else if([str isEqual:@"000401"]){ + } else{ [self showLog:@"未实现指令"]; @@ -65,7 +71,7 @@ // uint8_t cmdType = 0x00; // switch (cmdType) { // case 0x00: -// [self.loginManager receiveData:data]; +// [self.authManager receiveData:data]; // break; // case 0x01: // [self.audioManager receiveData:data]; @@ -84,4 +90,10 @@ - (void)showLog:(NSString *)log{ NSLog(@"%@",log); } + +- (void)sendTestLP{ + NSString *cmd = @"7B22736964223A22464753222C22766572223A312C2264617461223A227B5C227369647665725C223A312C5C226D73675F747970655C223A5C224647535F4D53475F545950455F53544152545F4647535F5245515C227D227D"; + NSData *data = [AiIotTools hexStringToData:cmd]; + [self willSendToBle:data]; +} @end diff --git a/MouseSdk/AiSdk/AiIotAudioManager.m b/MouseSdk/AiSdk/AiIotAudioManager.m index f18e6ca..663bf1c 100644 --- a/MouseSdk/AiSdk/AiIotAudioManager.m +++ b/MouseSdk/AiSdk/AiIotAudioManager.m @@ -26,9 +26,9 @@ self.recvData = nil; } else if([str hasPrefix:@"9c31"] && data.length >= 32){ - NSData *sub = [data subdataWithRange:NSMakeRange(2, data.length - 2)]; +// NSData *sub = [data subdataWithRange:NSMakeRange(3, data.length - 3)]; // [self showLog:[NSString stringWithFormat:@"截取的数据 %@",sub]]; - [self.recvData appendData:sub]; + [self.recvData appendData:data]; } else{ [self showLog:@"未实现指令"]; @@ -38,7 +38,7 @@ - (void)finishRecevice:(NSData *)data{ NSString *doc = NSHomeDirectory(); - NSString *fullPath = [NSString stringWithFormat:@"%@/Documents/1.pcm",doc]; + NSString *fullPath = [NSString stringWithFormat:@"%@/Documents/2.pcm",doc]; [self showLog:[NSString stringWithFormat:@"%@",fullPath]]; NSError *err; [data writeToFile:fullPath options:NSDataWritingAtomic error:&err]; diff --git a/MouseSdk/AiSdk/AiIotConnectManager.h b/MouseSdk/AiSdk/AiIotConnectManager.h index 06b7675..37c0928 100644 --- a/MouseSdk/AiSdk/AiIotConnectManager.h +++ b/MouseSdk/AiSdk/AiIotConnectManager.h @@ -10,7 +10,9 @@ NS_ASSUME_NONNULL_BEGIN @interface AiIotConnectManager : NSObject +@property (nonatomic,strong)void (^outputLog)(NSString *log); @property (nonatomic,strong)void (^outputData)(NSData *data); +@property (nonatomic,strong)void (^authCallBack)(BOOL isSuccess); - (void)login; diff --git a/MouseSdk/AiSdk/AiIotConnectManager.m b/MouseSdk/AiSdk/AiIotConnectManager.m index 15fe2bc..5aca020 100644 --- a/MouseSdk/AiSdk/AiIotConnectManager.m +++ b/MouseSdk/AiSdk/AiIotConnectManager.m @@ -41,20 +41,21 @@ #pragma mark - IotStateDelatege_C - (void)onIotConnectFailed { -// if (_stateBlock) _stateBlock(AliIotSDKBizbusState_IOT_CONNECT_FAILED); + [self showLog:@"LP连接失败。。。"]; + if(self.authCallBack)self.authCallBack(false); } -- (void)onIotConnected { -// if (_stateBlock) _stateBlock(AliIotSDKBizbusState_IOT_CONNECTED); - [self showLog:@"LP连接成功。。。"]; +- (void)onIotConnecting { + [self showLog:@"LP连接中。。。"]; } -- (void)onIotConnecting { -// if (_stateBlock) _stateBlock(AliIotSDKBizbusState_IOT_CONNECTING); +- (void)onIotConnected { + [self showLog:@"LP连接成功。。。"]; + if(self.authCallBack)self.authCallBack(true); } - (void)onIotDisconeted { -// if (_stateBlock) _stateBlock(AliIotSDKBizbusState_IOT_DISCONNECTED); + [self showLog:@"LP断开连接。。。"]; } - (void)onLogWithLog:(NSString *)log { @@ -83,7 +84,7 @@ } - (void)showLog:(NSString *)log { -// if (self.debugLogBlock) self.debugLogBlock(log); + if (self.outputLog) self.outputLog(log); } - (void)login{ diff --git a/MouseSdk/AiSdk/opusCodec.h b/MouseSdk/AiSdk/opusCodec.h new file mode 100644 index 0000000..41109e4 --- /dev/null +++ b/MouseSdk/AiSdk/opusCodec.h @@ -0,0 +1,14 @@ +// +// +//#import +//@interface opusCodec : NSObject +// +//-(void)opusInit; +// +//-(NSData*)encodePCMData:(NSData*)data; +// +//-(NSData*)decodeOpusData:(NSData*)data; +// +//-(void)destroy; +// +//@end diff --git a/MouseSdk/AiSdk/opusCodec.m b/MouseSdk/AiSdk/opusCodec.m new file mode 100644 index 0000000..0186bbd --- /dev/null +++ b/MouseSdk/AiSdk/opusCodec.m @@ -0,0 +1,149 @@ +// +// +//#import "opusCodec.h" +//// +////#import "opus.h" +// +//#define kDefaultSampleRate 8000 +// +//#define WB_FRAME_SIZE 320 +// +//@implementation opusCodec +// +//{ +// +//OpusEncoder *enc; +// +//OpusDecoder *dec; +// +//unsigned char opus_data_encoder[40]; +// +//} +// +//-(void)opusInit +// +//{ +// +//int error; +// +//enc = opus_encoder_create(kDefaultSampleRate, 1, OPUS_APPLICATION_VOIP, &error);//(采样率,声道数,,) +// +//dec = opus_decoder_create(kDefaultSampleRate, 1, &error); +// +//opus_encoder_ctl(enc, OPUS_SET_BITRATE(kDefaultSampleRate));//比特率 +// +//opus_encoder_ctl(enc, OPUS_SET_BANDWIDTH(OPUS_AUTO));//OPUS_BANDWIDTH_NARROWBAND 宽带窄带 +// +//opus_encoder_ctl(enc, OPUS_SET_VBR(0)); +// +//opus_encoder_ctl(enc, OPUS_SET_VBR_CONSTRAINT(1)); +// +//opus_encoder_ctl(enc, OPUS_SET_COMPLEXITY(8));//录制质量 1-10 +// +//opus_encoder_ctl(enc, OPUS_SET_PACKET_LOSS_PERC(0)); +// +//opus_encoder_ctl(enc, OPUS_SET_SIGNAL(OPUS_SIGNAL_VOICE));//信号 +// +//} +// +//- (NSData *)encode:(short *)pcmBuffer length:(NSInteger)lengthOfShorts +// +//{ +// +//// NSLog(@"--->>lengthOfShorts = %ld size -= %lu",(long)lengthOfShorts,sizeof(short)); +// +//int frame_size = (int)lengthOfShorts / sizeof(short);//WB_FRAME_SIZE; +// +//short input_frame[frame_size]; +// +//opus_int32 max_data_bytes = 2 * WB_FRAME_SIZE ;//随便设大,此时为原始PCM大小 +// +//memcpy(input_frame, pcmBuffer, lengthOfShorts );//frame_size * sizeof(short) +// +//int encodeBack = opus_encode(enc, input_frame, frame_size, opus_data_encoder, max_data_bytes); +// +//// NSLog(@"encodeBack===%d",encodeBack); +// +//if (encodeBack > 0) +// +//{ +// +//NSData *decodedData = [NSData dataWithBytes:opus_data_encoder length:encodeBack]; +// +//return decodedData; +// +//} +// +//else +// +//{ +// +//return nil; +// +//} +// +//} +// +////int decode(unsigned char* in_data, int len, short* out_data, int* out_len) { +// +//-(int)decode:(unsigned char *)encodedBytes length:(int)lengthOfBytes output:(short *)decoded +// +//{ +// +//int frame_size = WB_FRAME_SIZE; +// +//unsigned char cbits[frame_size*2]; +// +//memcpy(cbits, encodedBytes, lengthOfBytes); +// +//int pcm_num = opus_decode(dec, cbits, lengthOfBytes, decoded, frame_size, 0); +// +//// NSLog(@"解压后长度=%d",pcm_num); +// +//return frame_size; +// +//} +// +//-(NSData *)encodePCMData:(NSData*)data +// +//{ +// +//// NSLog(@"原始数据长度--->>%lu",(unsigned long)data.length); +// +//return [self encode:(short *)[data bytes] length:[data length]]; +// +//} +// +//-(NSData *)decodeOpusData:(NSData*)data +// +//{ +// +//int len = (int)[data length]; +// +//Byte *byteData = (Byte*)malloc(len); +// +//memcpy(byteData, [data bytes], len); +// +//int frame_size = WB_FRAME_SIZE; +// +//short decodedBuffer[frame_size ]; +// +//int nDecodedByte = sizeof(short) * [self decode:byteData length:len output:decodedBuffer]; +// +//NSData *PCMData = [NSData dataWithBytes:(Byte *)decodedBuffer length:nDecodedByte]; +// +//return PCMData; +// +//} +// +//-(void)destroy +// +//{ +// +//opus_encoder_destroy(enc); +// +//opus_decoder_destroy(dec); +// +//} +// +//@end diff --git a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj index ea1e68d..e289625 100644 --- a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj @@ -18,7 +18,6 @@ 18BF1E052B81E18800B8D90F /* ConnectKitVC.swift in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E042B81E18800B8D90F /* ConnectKitVC.swift */; }; 18BF1E222B83066200B8D90F /* BleManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E212B83066200B8D90F /* BleManager.m */; }; 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E752B832D2E00B8D90F /* ScanVC.m */; }; - 18BF1E7F2B83472D00B8D90F /* BleTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 18BF1E7E2B83472D00B8D90F /* BleTools.m */; }; 18BF1E8A2B8362D300B8D90F /* AiSdk.h in Headers */ = {isa = PBXBuildFile; fileRef = 18BF1E892B8362D300B8D90F /* AiSdk.h */; settings = {ATTRIBUTES = (Public, ); }; }; 18BF1E8D2B8362D300B8D90F /* AiSdk.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF1E872B8362D300B8D90F /* AiSdk.framework */; }; 18BF1E8E2B8362D300B8D90F /* AiSdk.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = 18BF1E872B8362D300B8D90F /* AiSdk.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; @@ -37,6 +36,8 @@ 18F3238F2B8F4F7300CABA2E /* AiIotAudioManager.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F3238D2B8F4F7300CABA2E /* AiIotAudioManager.m */; }; 18F323952B8F525C00CABA2E /* AiIotTools.h in Headers */ = {isa = PBXBuildFile; fileRef = 18F323932B8F525C00CABA2E /* AiIotTools.h */; }; 18F323962B8F525C00CABA2E /* AiIotTools.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F323942B8F525C00CABA2E /* AiIotTools.m */; }; + 18F323A82B9095D300CABA2E /* opusCodec.h in Headers */ = {isa = PBXBuildFile; fileRef = 18F323A62B9095D300CABA2E /* opusCodec.h */; }; + 18F323A92B9095D300CABA2E /* opusCodec.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F323A72B9095D300CABA2E /* opusCodec.m */; }; 8A6657BD1406314D4CBB7CEF /* libPods-MouseSdk.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0311DF3AD67ED792549B96BD /* libPods-MouseSdk.a */; }; /* End PBXBuildFile section */ @@ -108,8 +109,6 @@ 18BF1E212B83066200B8D90F /* BleManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BleManager.m; sourceTree = ""; }; 18BF1E742B832D2E00B8D90F /* ScanVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanVC.h; sourceTree = ""; }; 18BF1E752B832D2E00B8D90F /* ScanVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanVC.m; sourceTree = ""; }; - 18BF1E7D2B83472D00B8D90F /* BleTools.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = BleTools.h; sourceTree = ""; }; - 18BF1E7E2B83472D00B8D90F /* BleTools.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BleTools.m; sourceTree = ""; }; 18BF1E872B8362D300B8D90F /* AiSdk.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = AiSdk.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 18BF1E892B8362D300B8D90F /* AiSdk.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiSdk.h; sourceTree = ""; }; 18F3235B2B8ED78300CABA2E /* ScanCell.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ScanCell.h; sourceTree = ""; }; @@ -129,6 +128,8 @@ 18F3238D2B8F4F7300CABA2E /* AiIotAudioManager.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiIotAudioManager.m; sourceTree = ""; }; 18F323932B8F525C00CABA2E /* AiIotTools.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AiIotTools.h; sourceTree = ""; }; 18F323942B8F525C00CABA2E /* AiIotTools.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AiIotTools.m; sourceTree = ""; }; + 18F323A62B9095D300CABA2E /* opusCodec.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = opusCodec.h; sourceTree = ""; }; + 18F323A72B9095D300CABA2E /* opusCodec.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = opusCodec.m; sourceTree = ""; }; 51DE1118FCBA78EBEE6FE2CD /* Pods-MouseSdk.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MouseSdk.release.xcconfig"; path = "Target Support Files/Pods-MouseSdk/Pods-MouseSdk.release.xcconfig"; sourceTree = ""; }; 811936622867478806C1A8BB /* Pods-MouseSdk.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-MouseSdk.debug.xcconfig"; path = "Target Support Files/Pods-MouseSdk/Pods-MouseSdk.debug.xcconfig"; sourceTree = ""; }; /* End PBXFileReference section */ @@ -244,8 +245,6 @@ children = ( 18BF1E202B83066200B8D90F /* BleManager.h */, 18BF1E212B83066200B8D90F /* BleManager.m */, - 18BF1E7D2B83472D00B8D90F /* BleTools.h */, - 18BF1E7E2B83472D00B8D90F /* BleTools.m */, ); path = Ble; sourceTree = ""; @@ -262,6 +261,8 @@ 18F3238D2B8F4F7300CABA2E /* AiIotAudioManager.m */, 18F323822B8F330100CABA2E /* AiIotConnectManager.h */, 18F323832B8F330100CABA2E /* AiIotConnectManager.m */, + 18F323A62B9095D300CABA2E /* opusCodec.h */, + 18F323A72B9095D300CABA2E /* opusCodec.m */, ); path = AiSdk; sourceTree = ""; @@ -298,6 +299,7 @@ isa = PBXHeadersBuildPhase; buildActionMask = 2147483647; files = ( + 18F323A82B9095D300CABA2E /* opusCodec.h in Headers */, 18F3238E2B8F4F7300CABA2E /* AiIotAudioManager.h in Headers */, 18BF1E8A2B8362D300B8D90F /* AiSdk.h in Headers */, 18F323672B8F1C1900CABA2E /* AiIot.h in Headers */, @@ -509,7 +511,6 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( - 18BF1E7F2B83472D00B8D90F /* BleTools.m in Sources */, 18BF1E052B81E18800B8D90F /* ConnectKitVC.swift in Sources */, 18F3235D2B8ED78300CABA2E /* ScanCell.m in Sources */, 18F323622B8F13D700CABA2E /* LogVC.m in Sources */, @@ -545,6 +546,7 @@ buildActionMask = 2147483647; files = ( 18F3238F2B8F4F7300CABA2E /* AiIotAudioManager.m in Sources */, + 18F323A92B9095D300CABA2E /* opusCodec.m in Sources */, 18F323682B8F1C1900CABA2E /* AiIot.m in Sources */, 18F323962B8F525C00CABA2E /* AiIotTools.m in Sources */, 18F323852B8F330100CABA2E /* AiIotConnectManager.m in Sources */, diff --git a/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist b/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist index 4c533f1..6cbfd27 100644 --- a/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist @@ -7,12 +7,12 @@ AiSdk.xcscheme_^#shared#^_ orderHint - 2 + 3 MouseSdk.xcscheme_^#shared#^_ orderHint - 3 + 2 diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 9ed07e6..b4c9963 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.m b/MouseSdk/MouseSdk/Ble/BleManager.m index 6cc640f..4518be5 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.m +++ b/MouseSdk/MouseSdk/Ble/BleManager.m @@ -9,12 +9,18 @@ #import "BleManager.h" #import + +#define kVoiceUUID @"0783B03E-8535-B5A0-7140-A304D2194CB8" +//#define kUnUUID @"0783B03E-8535-B5A0-7140-A304D2495CB9" +#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 *writeCha; -@property(nonatomic, strong) CBCharacteristic *notifyCha; +@property(nonatomic, strong) CBCharacteristic *authCha; @end @@ -76,7 +82,7 @@ if(peripheral.name == nil){ return; } - NSLog(@"%@",peripheral.name); + [self showLog:peripheral.name]; [self.scanArray insertObject:peripheral atIndex:0]; if(self.discoverDevice){ self.discoverDevice(peripheral, advertisementData); @@ -93,18 +99,21 @@ [peripheral discoverServices:nil]; } -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ - + [self showLog:[NSString stringWithFormat:@"断开连接。。。%@",error.description]]; } - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(nullable NSError *)error{ - NSLog(@"连接失败。。。"); + [self showLog:[NSString stringWithFormat:@"连接失败。。。%@",error.description]]; } #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { // for (CBService *service in peripheral.services){ // [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:@"FED7"],[CBUUID UUIDWithString:@"FED8"]] forService:service]; +// } +// for (CBService *service in peripheral.services){ +// [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:@"0783B03E-8535-B5A0-7140-A304D2495CB8"]] forService:service]; // } for (CBService *service in peripheral.services){ - [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:@"0783B03E-8535-B5A0-7140-A304D2495CB8"]] forService:service]; + [peripheral discoverCharacteristics:nil forService:service]; } } @@ -121,13 +130,15 @@ // } // } for (CBCharacteristic *characteristic in service.characteristics){ -// if ([characteristic.UUID.UUIDString isEqualToString:@"FED7"]) { -// self.writeCha = characteristic; -// //找到写入的uuid后,调用sdk配置方法 -//// [AliIot.shareInstance configSDK]; -// } - if ([characteristic.UUID.UUIDString isEqualToString:@"0783B03E-8535-B5A0-7140-A304D2495CB8"]) { - self.notifyCha = characteristic; + if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID]) { + self.authCha = characteristic; + //找到写入的uuid后,调用sdk配置方法 +// [AliIot.shareInstance configSDK]; + [peripheral setNotifyValue:YES forCharacteristic:characteristic]; + } + if ([characteristic.UUID.UUIDString isEqualToString:kAuthUUID] || + [characteristic.UUID.UUIDString isEqualToString:kVoiceUUID] + ) { [peripheral setNotifyValue:YES forCharacteristic:characteristic]; } } @@ -167,12 +178,16 @@ } - (void)sendToBle:(NSData *)data{ - NSLog(@"[send] %@",data); - [self.connectPeripheral writeValue:data forCharacteristic:self.writeCha type:CBCharacteristicWriteWithoutResponse]; + [self showLog:[NSString stringWithFormat:@"[send] %@",data]]; + [self.connectPeripheral writeValue:data forCharacteristic:self.authCha type:CBCharacteristicWriteWithoutResponse]; } - (void)receiveDataFromBle:(NSData *)data{ - NSLog(@"[receive] %@",data); + [self showLog:[NSString stringWithFormat:@"[receive] %@",data]]; [[AiIot shared] sendToSdk:data]; } + +- (void)showLog:(NSString *)log{ + NSLog(@"%@",log); +} @end diff --git a/MouseSdk/MouseSdk/Ble/BleTools.h b/MouseSdk/MouseSdk/Ble/BleTools.h deleted file mode 100644 index c58f62a..0000000 --- a/MouseSdk/MouseSdk/Ble/BleTools.h +++ /dev/null @@ -1,16 +0,0 @@ -// -// BleTools.h -// MouseSdk -// -// Created by incar on 2024/2/19. -// - -#import - -NS_ASSUME_NONNULL_BEGIN - -@interface BleTools : NSObject -+ (NSData *)dataWithHexString:(NSString *)hexStr; -@end - -NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/Ble/BleTools.m b/MouseSdk/MouseSdk/Ble/BleTools.m deleted file mode 100644 index 2995dc2..0000000 --- a/MouseSdk/MouseSdk/Ble/BleTools.m +++ /dev/null @@ -1,33 +0,0 @@ -// -// BleTools.m -// MouseSdk -// -// Created by incar on 2024/2/19. -// - -#import "BleTools.h" - -@implementation BleTools -+ (NSData *)dataWithHexString:(NSString *)hexStr { - hexStr = [hexStr stringByReplacingOccurrencesOfString:@" " withString:@""]; - hexStr = [hexStr lowercaseString]; - NSUInteger len = hexStr.length; - if (!len) return nil; - unichar *buf = malloc(sizeof(unichar) * len); - if (!buf) return nil; - [hexStr getCharacters:buf range:NSMakeRange(0, len)]; - - NSMutableData *result = [NSMutableData data]; - unsigned char bytes; - char str[3] = { '\0', '\0', '\0' }; - int i; - for (i = 0; i < len / 2; i++) { - str[0] = buf[i * 2]; - str[1] = buf[i * 2 + 1]; - bytes = strtol(str, NULL, 16); - [result appendBytes:&bytes length:1]; - } - free(buf); - return result; -} -@end diff --git a/MouseSdk/MouseSdk/LogVC.m b/MouseSdk/MouseSdk/LogVC.m index 4f3de2d..df24471 100644 --- a/MouseSdk/MouseSdk/LogVC.m +++ b/MouseSdk/MouseSdk/LogVC.m @@ -7,7 +7,6 @@ #import "LogVC.h" #import "Ble/BleManager.h" -#import "Ble/BleTools.h" @interface LogVC() @property (nonatomic,strong) NSTextField *textField; @end diff --git a/MouseSdk/MouseSdk/ScanVC.m b/MouseSdk/MouseSdk/ScanVC.m index 4b383ef..0214dfa 100644 --- a/MouseSdk/MouseSdk/ScanVC.m +++ b/MouseSdk/MouseSdk/ScanVC.m @@ -8,7 +8,7 @@ #import "ScanVC.h" #import "ScanCell.h" #import "Ble/BleManager.h" -#import "Ble/BleTools.h" +//#import "Ble/BleTools.h" @interface ScanVC () @property (nonatomic,strong) NSTableView *tableView; @property (nonatomic,strong) NSMutableArray *scanArray; diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 8fc77de..4649d77 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -7,9 +7,8 @@ #import "ViewController.h" #import "ScanVC.h" -#import "AliIotTools.h" +#import "AiIotTools.h" #import "Ble/BleManager.h" -#import "Ble/BleTools.h" @interface ViewController () @@ -102,7 +101,9 @@ } - (void)checkFGSStatus{ -// [[AiIot shared] startLogin]; + NSString *cmd = @"7B22736964223A22464753222C22766572223A312C2264617461223A227B5C227369647665725C223A312C5C226D73675F747970655C223A5C224647535F4D53475F545950455F53544152545F4647535F5245515C227D227D"; + NSData *data = [AiIotTools hexStringToData:cmd]; + [[BleManager shared] sendToBle:data]; } - (void)connectLp{