From: liangzhenying <867174124@qq.com> Date: Wed, 28 Feb 2024 07:20:11 +0000 (+0800) Subject: 添加扫描功能 X-Git-Tag: 阿里鉴权~36 X-Git-Url: http://git.snicker.com.cn/Users/apple/Desktop/static/gitweb.css?a=commitdiff_plain;h=c44107c4a03b5100cbe80d7e88b79c8f15017608;p=mirrors%2FMouseSdk-MacOS.git 添加扫描功能 --- diff --git "a/Document/MouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" "b/Document/MouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" new file mode 100644 index 0000000..7e3b602 --- /dev/null +++ "b/Document/MouseSdk\346\216\245\345\217\243\346\226\207\346\241\243.md" @@ -0,0 +1,126 @@ +# MouseSDK接口文档 + + + +## 文档修改记录 + +| 版本 | 修改内容 | 修改人 | 修改时间 | +| ----- | -------- | ------ | ---------- | +| 1.0.0 | 初始版本 | 梁振营 | 2024-01-31 | + + + + + +## 引入依赖库 + +将 AliIotConnectKit.framework 添加进项目中。 + +## 相关权限配置 + +因为sdk用到蓝牙权限,所以需要在info.plist进行相关配置 + +```xml + + +NSBluetoothAlwaysUsageDescription +App需要您的同意,才能访问蓝牙 +``` + + + +## 蓝牙相关接口 + +```objective-c +/// 配置SDK,每次连接上蓝牙设备后调用 +- (void)configSDK; + +/// 收到 蓝牙 数据 +/// - Parameter currData: 蓝牙数据 +- (void)handleReceivedData:(NSData *)data; + +/// 断开设备调用结束工作 +/// - Parameter completed: 清理结束回调 +- (void)done:(void(^ _Nonnull )(id _Nullable param))completed; +``` + +## 蓝牙相关代理 + +AliIotSDKCallbackProtocol + +```objective-c +/// 响应数据,返回给上层调用 +/// - Parameters: +/// - currData: 当前发出去的数据 +/// - device: 蓝牙设备 MAC +/// - error: 先判断空,再发送出去 +- (void)handleSendData:(NSData *)currData toDevice:(NSData *)mac error:(NSError * _Nullable)error; + +@optional +/// 初始化结果 +/// - Parameter isFinish: 是否完成初始化 +- (void)sdkInitResult:(BOOL)isFinish; + +/// 输出日志 +/// - Parameter log: 日志信息 +- (void)handleLog:(NSString *)log; +``` + +## 示例 + +- 设置代理 + + ```objective-c + AliIot.shareInstance.delegate = self; + AliIot.shareInstance.mapDelegate = self; + ``` + + + +- 蓝牙连接成功后,需要与蓝牙设备协商MTU。所以必须注意是连接上蓝牙设备后再调用configSDK方法。 + + ```objective-c + //配置sdk + - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { + 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:@"FED8"]) { + self.notifyCha = characteristic; + [peripheral setNotifyValue:YES forCharacteristic:characteristic]; + } + } + } + ``` + +- 蓝牙连接断开后,必须调用done方法完成清理工作 + + ```objective-c + -(void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ + //蓝牙断开要调用该方法清理sdk数据 + [AliIot.shareInstance done:^(id _Nullable param) { + + }]; + } + ``` + +- 收到蓝牙设备传过来的数据,传给sdk处理 + + ```objective-c + - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + //收到蓝牙设备发送过来的数据,转发给sdk处理 + [AliIot.shareInstance handleReceivedData:characteristic.value]; + } + ``` + + + + + + + + + diff --git a/MouseSdk/AiSdk/AiSdk.h b/MouseSdk/AiSdk/AiSdk.h new file mode 100644 index 0000000..b6744b9 --- /dev/null +++ b/MouseSdk/AiSdk/AiSdk.h @@ -0,0 +1,18 @@ +// +// AiSdk.h +// AiSdk +// +// Created by incar on 2024/2/19. +// + +#import + +//! Project version number for AiSdk. +FOUNDATION_EXPORT double AiSdkVersionNumber; + +//! Project version string for AiSdk. +FOUNDATION_EXPORT const unsigned char AiSdkVersionString[]; + +// In this header, you should import all the public headers of your framework using statements like #import + + diff --git a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj index b5581ee..e4681eb 100644 --- a/MouseSdk/MouseSdk.xcodeproj/project.pbxproj +++ b/MouseSdk/MouseSdk.xcodeproj/project.pbxproj @@ -18,6 +18,12 @@ 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, ); }; }; + 18F3235D2B8ED78300CABA2E /* ScanCell.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F3235C2B8ED78300CABA2E /* ScanCell.m */; }; + 18F323622B8F13D700CABA2E /* LogVC.m in Sources */ = {isa = PBXBuildFile; fileRef = 18F323612B8F13D600CABA2E /* LogVC.m */; }; 8A6657BD1406314D4CBB7CEF /* libPods-MouseSdk.a in Frameworks */ = {isa = PBXBuildFile; fileRef = 0311DF3AD67ED792549B96BD /* libPods-MouseSdk.a */; }; /* End PBXBuildFile section */ @@ -36,8 +42,29 @@ remoteGlobalIDString = 18BF1DC92B81AB7C00B8D90F; remoteInfo = MouseSdk; }; + 18BF1E8B2B8362D300B8D90F /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = 18BF1DC22B81AB7C00B8D90F /* Project object */; + proxyType = 1; + remoteGlobalIDString = 18BF1E862B8362D300B8D90F; + remoteInfo = AiSdk; + }; /* End PBXContainerItemProxy section */ +/* Begin PBXCopyFilesBuildPhase section */ + 18BF1E8F2B8362D300B8D90F /* Embed Frameworks */ = { + isa = PBXCopyFilesBuildPhase; + buildActionMask = 2147483647; + dstPath = ""; + dstSubfolderSpec = 10; + files = ( + 18BF1E8E2B8362D300B8D90F /* AiSdk.framework in Embed Frameworks */, + ); + name = "Embed Frameworks"; + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXCopyFilesBuildPhase section */ + /* Begin PBXFileReference section */ 0311DF3AD67ED792549B96BD /* libPods-MouseSdk.a */ = {isa = PBXFileReference; explicitFileType = archive.ar; includeInIndex = 0; path = "libPods-MouseSdk.a"; sourceTree = BUILT_PRODUCTS_DIR; }; 18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = MouseSdk.app; sourceTree = BUILT_PRODUCTS_DIR; }; @@ -60,6 +87,14 @@ 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 = ""; }; + 18F3235C2B8ED78300CABA2E /* ScanCell.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ScanCell.m; sourceTree = ""; }; + 18F323602B8F13D600CABA2E /* LogVC.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = LogVC.h; sourceTree = ""; }; + 18F323612B8F13D600CABA2E /* LogVC.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = LogVC.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 */ @@ -70,6 +105,7 @@ buildActionMask = 2147483647; files = ( 8A6657BD1406314D4CBB7CEF /* libPods-MouseSdk.a in Frameworks */, + 18BF1E8D2B8362D300B8D90F /* AiSdk.framework in Frameworks */, ); runOnlyForDeploymentPostprocessing = 0; }; @@ -87,6 +123,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 18BF1E842B8362D300B8D90F /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXFrameworksBuildPhase section */ /* Begin PBXGroup section */ @@ -97,6 +140,7 @@ 18BF1DCC2B81AB7C00B8D90F /* MouseSdk */, 18BF1DE22B81AB8000B8D90F /* MouseSdkTests */, 18BF1DEC2B81AB8000B8D90F /* MouseSdkUITests */, + 18BF1E882B8362D300B8D90F /* AiSdk */, 18BF1DCB2B81AB7C00B8D90F /* Products */, 723588BAA058E1012143D29F /* Pods */, 543EC4E5F9FC310D6B850947 /* Frameworks */, @@ -109,6 +153,7 @@ 18BF1DCA2B81AB7C00B8D90F /* MouseSdk.app */, 18BF1DDF2B81AB8000B8D90F /* MouseSdkTests.xctest */, 18BF1DE92B81AB8000B8D90F /* MouseSdkUITests.xctest */, + 18BF1E872B8362D300B8D90F /* AiSdk.framework */, ); name = Products; sourceTree = ""; @@ -123,6 +168,10 @@ 18BF1DD12B81AB7C00B8D90F /* ViewController.m */, 18BF1E742B832D2E00B8D90F /* ScanVC.h */, 18BF1E752B832D2E00B8D90F /* ScanVC.m */, + 18F3235B2B8ED78300CABA2E /* ScanCell.h */, + 18F3235C2B8ED78300CABA2E /* ScanCell.m */, + 18F323602B8F13D600CABA2E /* LogVC.h */, + 18F323612B8F13D600CABA2E /* LogVC.m */, 18BF1E042B81E18800B8D90F /* ConnectKitVC.swift */, 18BF1DD32B81AB7F00B8D90F /* Assets.xcassets */, 18BF1DD52B81AB7F00B8D90F /* Main.storyboard */, @@ -162,10 +211,20 @@ children = ( 18BF1E202B83066200B8D90F /* BleManager.h */, 18BF1E212B83066200B8D90F /* BleManager.m */, + 18BF1E7D2B83472D00B8D90F /* BleTools.h */, + 18BF1E7E2B83472D00B8D90F /* BleTools.m */, ); path = Ble; sourceTree = ""; }; + 18BF1E882B8362D300B8D90F /* AiSdk */ = { + isa = PBXGroup; + children = ( + 18BF1E892B8362D300B8D90F /* AiSdk.h */, + ); + path = AiSdk; + sourceTree = ""; + }; 543EC4E5F9FC310D6B850947 /* Frameworks */ = { isa = PBXGroup; children = ( @@ -185,6 +244,17 @@ }; /* End PBXGroup section */ +/* Begin PBXHeadersBuildPhase section */ + 18BF1E822B8362D300B8D90F /* Headers */ = { + isa = PBXHeadersBuildPhase; + buildActionMask = 2147483647; + files = ( + 18BF1E8A2B8362D300B8D90F /* AiSdk.h in Headers */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXHeadersBuildPhase section */ + /* Begin PBXNativeTarget section */ 18BF1DC92B81AB7C00B8D90F /* MouseSdk */ = { isa = PBXNativeTarget; @@ -194,10 +264,12 @@ 18BF1DC62B81AB7C00B8D90F /* Sources */, 18BF1DC72B81AB7C00B8D90F /* Frameworks */, 18BF1DC82B81AB7C00B8D90F /* Resources */, + 18BF1E8F2B8362D300B8D90F /* Embed Frameworks */, ); buildRules = ( ); dependencies = ( + 18BF1E8C2B8362D300B8D90F /* PBXTargetDependency */, ); name = MouseSdk; productName = MouseSdk; @@ -240,6 +312,24 @@ productReference = 18BF1DE92B81AB8000B8D90F /* MouseSdkUITests.xctest */; productType = "com.apple.product-type.bundle.ui-testing"; }; + 18BF1E862B8362D300B8D90F /* AiSdk */ = { + isa = PBXNativeTarget; + buildConfigurationList = 18BF1E922B8362D300B8D90F /* Build configuration list for PBXNativeTarget "AiSdk" */; + buildPhases = ( + 18BF1E822B8362D300B8D90F /* Headers */, + 18BF1E832B8362D300B8D90F /* Sources */, + 18BF1E842B8362D300B8D90F /* Frameworks */, + 18BF1E852B8362D300B8D90F /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = AiSdk; + productName = AiSdk; + productReference = 18BF1E872B8362D300B8D90F /* AiSdk.framework */; + productType = "com.apple.product-type.framework"; + }; /* End PBXNativeTarget section */ /* Begin PBXProject section */ @@ -261,6 +351,9 @@ CreatedOnToolsVersion = 15.0.1; TestTargetID = 18BF1DC92B81AB7C00B8D90F; }; + 18BF1E862B8362D300B8D90F = { + CreatedOnToolsVersion = 15.0.1; + }; }; }; buildConfigurationList = 18BF1DC52B81AB7C00B8D90F /* Build configuration list for PBXProject "MouseSdk" */; @@ -279,6 +372,7 @@ 18BF1DC92B81AB7C00B8D90F /* MouseSdk */, 18BF1DDE2B81AB8000B8D90F /* MouseSdkTests */, 18BF1DE82B81AB8000B8D90F /* MouseSdkUITests */, + 18BF1E862B8362D300B8D90F /* AiSdk */, ); }; /* End PBXProject section */ @@ -307,6 +401,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 18BF1E852B8362D300B8D90F /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXResourcesBuildPhase section */ /* Begin PBXShellScriptBuildPhase section */ @@ -339,7 +440,10 @@ 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 */, 18BF1DD22B81AB7C00B8D90F /* ViewController.m in Sources */, 18BF1DD92B81AB7F00B8D90F /* main.m in Sources */, 18BF1E762B832D2E00B8D90F /* ScanVC.m in Sources */, @@ -365,6 +469,13 @@ ); runOnlyForDeploymentPostprocessing = 0; }; + 18BF1E832B8362D300B8D90F /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; /* End PBXSourcesBuildPhase section */ /* Begin PBXTargetDependency section */ @@ -378,6 +489,11 @@ target = 18BF1DC92B81AB7C00B8D90F /* MouseSdk */; targetProxy = 18BF1DEA2B81AB8000B8D90F /* PBXContainerItemProxy */; }; + 18BF1E8C2B8362D300B8D90F /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = 18BF1E862B8362D300B8D90F /* AiSdk */; + targetProxy = 18BF1E8B2B8362D300B8D90F /* PBXContainerItemProxy */; + }; /* End PBXTargetDependency section */ /* Begin PBXVariantGroup section */ @@ -640,6 +756,68 @@ }; name = Release; }; + 18BF1E902B8362D300B8D90F /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSHumanReadableCopyright = ""; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++20"; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.AiSdk; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_EMIT_LOC_STRINGS = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Debug; + }; + 18BF1E912B8362D300B8D90F /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + CODE_SIGN_STYLE = Automatic; + COMBINE_HIDPI_IMAGES = YES; + CURRENT_PROJECT_VERSION = 1; + DEFINES_MODULE = YES; + DYLIB_COMPATIBILITY_VERSION = 1; + DYLIB_CURRENT_VERSION = 1; + DYLIB_INSTALL_NAME_BASE = "@rpath"; + ENABLE_MODULE_VERIFIER = YES; + GENERATE_INFOPLIST_FILE = YES; + INFOPLIST_KEY_NSHumanReadableCopyright = ""; + INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; + LD_RUNPATH_SEARCH_PATHS = ( + "$(inherited)", + "@executable_path/../Frameworks", + "@loader_path/Frameworks", + ); + MARKETING_VERSION = 1.0; + MODULE_VERIFIER_SUPPORTED_LANGUAGES = "objective-c objective-c++"; + MODULE_VERIFIER_SUPPORTED_LANGUAGE_STANDARDS = "gnu17 gnu++20"; + PRODUCT_BUNDLE_IDENTIFIER = zhihuishixun.AiSdk; + PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; + SKIP_INSTALL = YES; + SWIFT_EMIT_LOC_STRINGS = YES; + VERSIONING_SYSTEM = "apple-generic"; + VERSION_INFO_PREFIX = ""; + }; + name = Release; + }; /* End XCBuildConfiguration section */ /* Begin XCConfigurationList section */ @@ -679,6 +857,15 @@ defaultConfigurationIsVisible = 0; defaultConfigurationName = Release; }; + 18BF1E922B8362D300B8D90F /* Build configuration list for PBXNativeTarget "AiSdk" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + 18BF1E902B8362D300B8D90F /* Debug */, + 18BF1E912B8362D300B8D90F /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; /* End XCConfigurationList section */ }; rootObject = 18BF1DC22B81AB7C00B8D90F /* Project object */; diff --git a/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist b/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist index 7f0eeb4..80e0c4f 100644 --- a/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist +++ b/MouseSdk/MouseSdk.xcodeproj/xcuserdata/apple.xcuserdatad/xcschemes/xcschememanagement.plist @@ -4,6 +4,11 @@ SchemeUserState + AiSdk.xcscheme_^#shared#^_ + + orderHint + 1 + MouseSdk.xcscheme_^#shared#^_ orderHint diff --git a/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate b/MouseSdk/MouseSdk.xcworkspace/xcuserdata/apple.xcuserdatad/UserInterfaceState.xcuserstate index 4c1524b..838f345 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 532ca42..7e3cc9b 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.h +++ b/MouseSdk/MouseSdk/Ble/BleManager.h @@ -22,6 +22,8 @@ NS_ASSUME_NONNULL_BEGIN - (void)connectPeripheral:(CBPeripheral *)peripheral; - (void)lpConnect; + +- (void)sendToBle:(NSData *)data; @end NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/Ble/BleManager.m b/MouseSdk/MouseSdk/Ble/BleManager.m index 852d87a..7f8db83 100644 --- a/MouseSdk/MouseSdk/Ble/BleManager.m +++ b/MouseSdk/MouseSdk/Ble/BleManager.m @@ -5,6 +5,8 @@ // Created by incar on 2024/1/30. // + + #import "BleManager.h" @interface BleManager () @@ -71,6 +73,10 @@ if ([self.scanArray containsObject:peripheral]){ return; } + if(peripheral.name == nil){ + return; + } + NSLog(@"%@",peripheral.name); [self.scanArray insertObject:peripheral atIndex:0]; if(self.discoverDevice){ self.discoverDevice(peripheral, advertisementData); @@ -94,8 +100,9 @@ } #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { - for (CBService *service in peripheral.services) + for (CBService *service in peripheral.services){ [peripheral discoverCharacteristics:@[[CBUUID UUIDWithString:@"FED7"],[CBUUID UUIDWithString:@"FED8"]] forService:service]; + } } - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { @@ -119,50 +126,7 @@ } #pragma mark - AliIotSDKCallbackProtocol -- (void)handleSendData:(NSData *)data toDevice:(id)device error:(NSError *)error { - NSLog(@"[send] %@",data); - if (!error) { - //收到sdk回调的数据,发送给蓝牙设备 - [self.connectPeripheral writeValue:data forCharacteristic:self.writeCha type:CBCharacteristicWriteWithoutResponse]; - } -} -- (void)handleLog:(NSString *)log { - NSLog(@"[sdk log] %@",log); -} - -- (void)sdkInitResult:(BOOL)isFinish{ - self.isSdkInitSuccess = isFinish; - NSString *log = isFinish?@"初始化完成":@"初始化失败"; - NSLog(@"%@",log); -} - -#pragma mark - AliIotSdkMapCallbackProtocol -- (void)addCollect:(nonnull NSString *)collectionId content:(nonnull NSString *)jsonString { - //传到服务器或者本地缓存 -} - -- (void)addTrajectory:(nonnull NSString *)trajectoryId content:(nonnull NSString *)jsonString { - //传到服务器或者本地缓存 -} - -- (void)getCollectListView:(NSInteger)page pageSize:(NSInteger)pageSize { - //从服务器或者本地缓存获取收藏数据,然后将数据传给sdk -// [AliIot.shareInstance sendCollectList:jsonString]; -} - -- (void)getTrajectoryListView:(NSInteger)page pageSize:(NSInteger)pageSize { - //从服务器或者本地缓存获取轨迹数据,然后将数据传给sdk -// [AliIot.shareInstance sendTrajectoryList:jsonString]; -} - -- (void)removeCollect:(nonnull NSString *)collectionId { - //从服务器或者本地缓存移除记录 -} - -- (void)removeTrajectory:(nonnull NSString *)trajectoryId { - //从服务器或者本地缓存移除记录 -} #pragma mark - public - (void)startScanDevice{ @@ -180,33 +144,18 @@ //飞鸽书请求连接 - (void)lpConnect{ - NSData *data = [self tysm_dataWithHexString:@"000401"]; - NSMutableData *mData = NSMutableData.data; - uint8_t cmd = 0x7f; - [mData appendBytes:&cmd length:1]; - [mData appendData:data]; +// NSData *data = [self tysm_dataWithHexString:@"000000000401"]; +// NSMutableData *mData = NSMutableData.data; +// uint8_t cmd = 0x7f; +// [mData appendBytes:&cmd length:1]; +// [mData appendData:data]; // [AliIot.shareInstance handleReceivedData:mData.copy]; } -- (NSData *)tysm_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; + +- (void)sendToBle:(NSData *)data{ + NSLog(@"[send] %@",data); + [self.connectPeripheral writeValue:data forCharacteristic:self.writeCha type:CBCharacteristicWriteWithoutResponse]; } + + @end diff --git a/MouseSdk/MouseSdk/Ble/BleTools.h b/MouseSdk/MouseSdk/Ble/BleTools.h new file mode 100644 index 0000000..c58f62a --- /dev/null +++ b/MouseSdk/MouseSdk/Ble/BleTools.h @@ -0,0 +1,16 @@ +// +// 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 new file mode 100644 index 0000000..2995dc2 --- /dev/null +++ b/MouseSdk/MouseSdk/Ble/BleTools.m @@ -0,0 +1,33 @@ +// +// 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.h b/MouseSdk/MouseSdk/LogVC.h new file mode 100644 index 0000000..6be04f6 --- /dev/null +++ b/MouseSdk/MouseSdk/LogVC.h @@ -0,0 +1,16 @@ +// +// LogVC.h +// MouseSdk +// +// Created by incar on 2024/2/28. +// + +#import + +NS_ASSUME_NONNULL_BEGIN + +@interface LogVC : NSViewController + +@end + +NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/LogVC.m b/MouseSdk/MouseSdk/LogVC.m new file mode 100644 index 0000000..4f3de2d --- /dev/null +++ b/MouseSdk/MouseSdk/LogVC.m @@ -0,0 +1,35 @@ +// +// LogVC.m +// MouseSdk +// +// Created by incar on 2024/2/28. +// + +#import "LogVC.h" +#import "Ble/BleManager.h" +#import "Ble/BleTools.h" +@interface LogVC() +@property (nonatomic,strong) NSTextField *textField; +@end + +@implementation LogVC +- (void)viewDidLoad { + [super viewDidLoad]; + // Do view setup here. + [self setupViews]; + [self setupBle]; +} +- (void)loadView{ + self.title = @"扫描设备"; + self.view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; +} +- (void)setupViews{ + +} +- (void)setupBle{ + BleManager *manager = [BleManager shared]; + __weak typeof(self) weakSelf= self; + +} + +@end diff --git a/MouseSdk/MouseSdk/ScanCell.h b/MouseSdk/MouseSdk/ScanCell.h new file mode 100644 index 0000000..bb8a265 --- /dev/null +++ b/MouseSdk/MouseSdk/ScanCell.h @@ -0,0 +1,17 @@ +// +// ScanCell.h +// MouseSdk +// +// Created by incar on 2024/2/28. +// + +#import +#import +NS_ASSUME_NONNULL_BEGIN + +@interface ScanCell : NSTableCellView +@property(nonatomic,strong)CBPeripheral *peripheral; +@property(nonatomic,strong)void (^connecCallBack)(CBPeripheral *peripheral); +@end + +NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/ScanCell.m b/MouseSdk/MouseSdk/ScanCell.m new file mode 100644 index 0000000..a0bf16f --- /dev/null +++ b/MouseSdk/MouseSdk/ScanCell.m @@ -0,0 +1,63 @@ +// +// ScanCell.m +// MouseSdk +// +// Created by incar on 2024/2/28. +// + +#import "ScanCell.h" + +@interface ScanCell() +@property(nonatomic,strong)NSTextField *label; +@property(nonatomic,strong)NSButton *connectBtn; +@end + +@implementation ScanCell +- (instancetype)initWithFrame:(CGRect)frame +{ + self = [super initWithFrame:frame]; + if (self) { + [self setupView]; + } + return self; +} + +- (void)drawRect:(NSRect)dirtyRect{ + [super drawRect:dirtyRect]; +} + +- (void)setupView{ + NSTextField *label = [[NSTextField alloc] initWithFrame:CGRectMake(0, 10, 200, 35)]; + label.editable = NO; + label.bordered = NO; + label.stringValue = @"000000"; + self.label = label; + [self addSubview:label]; + + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(250, 5, 100, 35)]; + btn.bordered = true; + btn.title = @"连接"; + [btn setTarget:self]; + [btn setAction:@selector(connetAction)]; + btn.state = 1; + [self addSubview:btn]; + self.connectBtn = btn; + + +} + +-(void)setPeripheral:(CBPeripheral *)peripheral{ + _peripheral = peripheral; + self.label.stringValue = peripheral.name; + [self.label sizeToFit]; + CGRect f = self.label.frame; + f.origin.y = (self.bounds.size.height - f.size.height)/2; + self.label.frame = f; +} + +- (void)connetAction{ + if (self.connecCallBack) { + self.connecCallBack(self.peripheral); + } +} +@end diff --git a/MouseSdk/MouseSdk/ScanVC.h b/MouseSdk/MouseSdk/ScanVC.h index a198ad7..930dbcf 100644 --- a/MouseSdk/MouseSdk/ScanVC.h +++ b/MouseSdk/MouseSdk/ScanVC.h @@ -11,6 +11,7 @@ NS_ASSUME_NONNULL_BEGIN @interface ScanVC : NSViewController + @end NS_ASSUME_NONNULL_END diff --git a/MouseSdk/MouseSdk/ScanVC.m b/MouseSdk/MouseSdk/ScanVC.m index 1b3dc24..5e65855 100644 --- a/MouseSdk/MouseSdk/ScanVC.m +++ b/MouseSdk/MouseSdk/ScanVC.m @@ -6,16 +6,125 @@ // #import "ScanVC.h" - -@interface ScanVC () - +#import "ScanCell.h" +#import "Ble/BleManager.h" +#import "Ble/BleTools.h" +@interface ScanVC () +@property (nonatomic,strong) NSTableView *tableView; +@property (nonatomic,strong) NSMutableArray *scanArray; @end @implementation ScanVC +-(NSMutableArray *)scanArray{ + if (_scanArray == nil) { + _scanArray = [[NSMutableArray alloc] init]; + } + return _scanArray; +} - (void)viewDidLoad { [super viewDidLoad]; // Do view setup here. + self.title = @"扫描设备"; + [self tableViewSetting]; + [self setupBle]; +} +- (void)loadView{ + self.view = [[NSView alloc] initWithFrame:CGRectMake(0, 0, 500, 500)]; +} + +- (void)tableViewSetting{ + CALayer *viewLayer = [CALayer layer]; + NSView *backgroundView= [[NSView alloc]init]; + [backgroundView setWantsLayer:YES]; + [backgroundView setLayer:viewLayer]; + backgroundView.layer.backgroundColor = [NSColor greenColor].CGColor; + [backgroundView setNeedsDisplay:YES]; + [self.view addSubview:backgroundView]; + + //实现tableview的滚动效果 + NSScrollView *scrollView = [[NSScrollView alloc] initWithFrame:self.view.bounds]; + scrollView.backgroundColor = [NSColor redColor]; + [scrollView setDrawsBackground:NO];//不画背景(背景默认画成白色) + [scrollView setHasVerticalScroller:YES];//有垂直滚动条 + //[_tableContainer setHasHorizontalScroller:YES]; //有水平滚动条 + scrollView.autohidesScrollers = YES;//自动隐藏滚动条(滚动的时候出现) + [self.view addSubview:scrollView]; + + self.tableView = [[NSTableView alloc] initWithFrame:scrollView.bounds]; + [scrollView setDocumentView:self.tableView]; + + NSTableColumn *column1 = [[NSTableColumn alloc] initWithIdentifier:@"columnFrist"]; + 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{ + BleManager *manager = [BleManager shared]; + __weak typeof(self) weakSelf= self; +// __weak typeof(BleManager) *weakManager = manager; + manager.discoverDevice = ^(CBPeripheral *peripheral,NSDictionary *advertisementData){ + if ([weakSelf.scanArray containsObject:peripheral]){ + return; + } + if(peripheral.name.length == 0){ + return; + } + [weakSelf.scanArray addObject:peripheral]; + [weakSelf.tableView reloadData]; +// NSLog(@"%@",peripheral.name); +// iMouse Ublue_BT895x +// if([peripheral.name isEqualToString:@"iMouse"]){ +// [weakManager connectPeripheral:peripheral]; +// [weakManager stopScanDevice]; +// } + }; + manager.didConnectDevice = ^(CBPeripheral * _Nonnull peripheral) { + NSLog(@"连接成功"); + [weakSelf dismissViewController:weakSelf]; + }; + [manager startScanDevice]; +} + +- (void)connectDevice:(CBPeripheral *)peripheral{ + [[BleManager shared] connectPeripheral:peripheral]; +} +#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]; + }; + return cell; +} +-(CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row{ + return 44; } @end diff --git a/MouseSdk/MouseSdk/ViewController.m b/MouseSdk/MouseSdk/ViewController.m index 008733b..2197b78 100644 --- a/MouseSdk/MouseSdk/ViewController.m +++ b/MouseSdk/MouseSdk/ViewController.m @@ -8,6 +8,7 @@ #import "ViewController.h" #import "ScanVC.h" #import "Ble/BleManager.h" +#import "Ble/BleTools.h" //#import "AliIotConnectKit/AliIotConnectKit.h" @@ -17,37 +18,40 @@ @implementation ViewController + + - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view. + self.title = @"AI SDK"; { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(20, 60, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 40, 100, 45)]; btn.bordered = true; - btn.title = @"扫描"; // 按钮文字 + btn.title = @"扫描"; [btn setTarget:self]; - [btn setAction:@selector(scanAction)]; // 按钮触发的方法 + [btn setAction:@selector(scanAction)]; btn.state = 1; [self.view addSubview:btn]; } { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, 100, 100, 45)]; + NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(100, self.view.bounds.size.height - 150, 100, 45)]; btn.bordered = true; - btn.title = @"fgs"; // 按钮文字 + btn.title = @"initSDK"; [btn setTarget:self]; - [btn setAction:@selector(checkFGSStatus)]; // 按钮触发的方法 - btn.state = 1; - [self.view addSubview:btn]; - } - { - NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(300, 100, 100, 45)]; - btn.bordered = true; - btn.title = @"connectLp"; // 按钮文字 - [btn setTarget:self]; - [btn setAction:@selector(connectLp)]; // 按钮触发的方法 + [btn setAction:@selector(initSDK)]; btn.state = 1; [self.view addSubview:btn]; } +// { +// NSButton *btn = [[NSButton alloc]initWithFrame:NSMakeRect(300, 100, 100, 45)]; +// btn.bordered = true; +// btn.title = @"connectLp"; // 按钮文字 +// [btn setTarget:self]; +// [btn setAction:@selector(connectLp)]; // 按钮触发的方法 +// btn.state = 1; +// [self.view addSubview:btn]; +// } [self setupBleManger]; } @@ -59,19 +63,20 @@ } - (void)scanAction{ - [[BleManager shared] startScanDevice]; - + ScanVC *vc = [[ScanVC alloc] initWithNibName:nil bundle:nil]; + [self presentViewControllerAsModalWindow:vc]; +// [[BleManager shared] startScanDevice]; } - (void)setupBleManger{ BleManager *manager = [BleManager shared]; - __weak typeof(ViewController) *weakSelf = self; __weak typeof(BleManager) *weakManager = manager; manager.discoverDevice = ^(CBPeripheral *peripheral,NSDictionary *advertisementData){ if(peripheral.name.length == 0){ return; } - NSLog(@"%@",peripheral.name); - if([peripheral.name isEqualToString:@"Ublue_BT895x"]){ +// NSLog(@"%@",peripheral.name); +// iMouse Ublue_BT895x + if([peripheral.name isEqualToString:@"iMouse"]){ [weakManager connectPeripheral:peripheral]; [weakManager stopScanDevice]; } @@ -80,9 +85,11 @@ NSLog(@"连接成功"); }; -// [manager startScanDevice]; } - +- (void)initSDK{ + NSData *data = [BleTools dataWithHexString:@"0000004f0000"]; + [[BleManager shared] sendToBle:data]; +} //- (void)checkFGSStatus{ // [AliConnectMananger_C.shared checkFgsStateWithResult:^(BOOL isSuccess, NSDictionary * dict) { // NSLog(@"FGS成功 = %@,dict = %@",[NSNumber numberWithBool:isSuccess],dict); diff --git a/MouseSdk/Podfile b/MouseSdk/Podfile index b2c9051..09c0c95 100644 --- a/MouseSdk/Podfile +++ b/MouseSdk/Podfile @@ -1,26 +1,14 @@ # Uncomment the next line to define a global platform for your project # platform :ios, '11.0' -#platform :osx, '11.0' - -#target 'AliIotSdk' do -# # Comment the next line if you don't want to use dynamic frameworks -# use_frameworks! -## platform :osx, '11.0' -# -# # Pods for AliIotSdk -## pod 'NBULog' -## pod 'fishhook' -## pod 'Bugly' -# target 'AliIotSdkTests' do -# # Pods for testing -# end -# -#end +platform :osx, '11.0' +#source 'https://kkgithub.com/CocoaPods/Specs.git' +#source 'https://kkgithub.com/CocoaPods/Specs.git' +# source 'https://gitee.com/mirrors/CocoaPods-Specs.git' target 'MouseSdk' do # Comment the next line if you don't want to use dynamic frameworks - + pod 'Masonry'