diff --git a/.gitignore b/.gitignore index c964cd8..5d4b7cd 100644 --- a/.gitignore +++ b/.gitignore @@ -1,6 +1,61 @@ +# Created by https://www.gitignore.io/api/macos,windows,objective-c,swift + +### macOS ### +*.DS_Store +.AppleDouble +.LSOverride + +# Icon must end with two \r +Icon +# Thumbnails +._* +# Files that might appear in the root of a volume +.DocumentRevisions-V100 +.fseventsd +.Spotlight-V100 +.TemporaryItems +.Trashes +.VolumeIcon.icns +.com.apple.timemachine.donotpresent +# Directories potentially created on remote AFP share +.AppleDB +.AppleDesktop +Network Trash Folder +Temporary Items +.apdisk + + +### Windows ### +# Windows image file caches +Thumbs.db +ehthumbs.db + +# Folder config file +Desktop.ini + +# Recycle Bin used on file shares +$RECYCLE.BIN/ + +# Windows Installer files +*.cab +*.msi +*.msm +*.msp + +# Windows shortcuts +*.lnk + + +### Objective-C ### # Xcode # +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## Build generated build/ +DerivedData/ + +## Various settings *.pbxuser !default.pbxuser *.mode1v3 @@ -9,18 +64,96 @@ build/ !default.mode2v3 *.perspectivev3 !default.perspectivev3 -xcuserdata -*.xccheckout +xcuserdata/ + +## Other *.moved-aside -DerivedData +*.xcuserstate + +## Obj-C/Swift specific *.hmap *.ipa -*.xcuserstate +*.dSYM.zip +*.dSYM + +# CocoaPods +# +# We recommend against adding the Pods directory to your .gitignore. However +# you should judge for yourself, the pros and cons are mentioned at: +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# Pods/ + +# Carthage +# +# Add this line if you want to avoid checking in source code from Carthage dependencies. +# Carthage/Checkouts + +Carthage/Build + +# fastlane +# +# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the +# screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md + +fastlane/report.xml +fastlane/Preview.html +fastlane/screenshots +fastlane/test_output + +# Code Injection +# +# After new code Injection tools there's a generated folder /iOSInjectionProject +# https://github.com/johnno1962/injectionforxcode + +iOSInjectionProject/ + +### Objective-C Patch ### +*.xcscmblueprint + + +### Swift ### +# Xcode +# +# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore + +## Build generated + +## Various settings + +## Other + +## Obj-C/Swift specific + +## Playgrounds +timeline.xctimeline +playground.xcworkspace + +# Swift Package Manager +# +# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies. +# Packages/ +.build/ # CocoaPods # # We recommend against adding the Pods directory to your .gitignore. However # you should judge for yourself, the pros and cons are mentioned at: -# http://guides.cocoapods.org/using/using-cocoapods.html#should-i-ignore-the-pods-directory-in-source-control +# https://guides.cocoapods.org/using/using-cocoapods.html#should-i-check-the-pods-directory-into-source-control +# +# Pods/ + +# Carthage +# +# Add this line if you want to avoid checking in source code from Carthage dependencies. +# Carthage/Checkouts + + +# fastlane # -#Pods/ +# It is recommended to not store the screenshots in the git repo. Instead, use fastlane to re-generate the +# screenshots whenever they are needed. +# For more information about the recommended setup visit: +# https://github.com/fastlane/fastlane/blob/master/fastlane/docs/Gitignore.md diff --git a/BabyBluetooth.podspec b/BabyBluetooth.podspec index a9e2d6a..2a77128 100644 --- a/BabyBluetooth.podspec +++ b/BabyBluetooth.podspec @@ -2,7 +2,7 @@ Pod::Spec.new do |s| s.name = "BabyBluetooth" - s.version = "0.4.0" + s.version = "0.7.0" s.summary = "bluetooth library on ios/osx" s.description = <<-DESC @@ -14,11 +14,12 @@ Pod::Spec.new do |s| s.license = "MIT" s.author = { "liuyanwei" => "coolnameismy@hotmail.com" } - s.source = { :git => "https://github.com/coolnameismy/BabyBluetooth.git", :tag => "0.4.0" } + s.source = { :git => "https://github.com/coolnameismy/BabyBluetooth.git", :tag => "0.7.0" } - s.platform = :ios, "7.0" + s.ios.deployment_target = '7.0' + s.osx.deployment_target = '10.10' s.requires_arc = true - + s.source_files = "Classes", "Classes/objc/*.{h,m}" end diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo.xcodeproj/project.pbxproj b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo.xcodeproj/project.pbxproj index a0cd36f..9932a9d 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo.xcodeproj/project.pbxproj +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo.xcodeproj/project.pbxproj @@ -7,7 +7,10 @@ objects = { /* Begin PBXBuildFile section */ - E13AD4D51BB78EF900D61C84 /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E13AD4D41BB78EF900D61C84 /* BabyOptions.m */; settings = {ASSET_TAGS = (); }; }; + 79CD9BD21CDB7395009DAD1C /* BabyDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 79CD9BD11CDB7395009DAD1C /* BabyDefine.m */; }; + E13AD4D51BB78EF900D61C84 /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E13AD4D41BB78EF900D61C84 /* BabyOptions.m */; }; + E16F24A81C20484E00A5C274 /* BabyPeripheralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E16F24A71C20484E00A5C274 /* BabyPeripheralManager.m */; }; + E16F24AB1C2123B800A5C274 /* BabyCentralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E16F24AA1C2123B800A5C274 /* BabyCentralManager.m */; }; E1807E731B6CA29100E99C63 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E1807E721B6CA29100E99C63 /* main.m */; }; E1807E761B6CA29100E99C63 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E1807E751B6CA29100E99C63 /* AppDelegate.m */; }; E1807E791B6CA29100E99C63 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E1807E781B6CA29100E99C63 /* ViewController.m */; }; @@ -16,10 +19,9 @@ E1807E811B6CA29100E99C63 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = E1807E7F1B6CA29100E99C63 /* LaunchScreen.xib */; }; E1DEF5151B98857B0005C9F2 /* BabyBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DEF5081B98857B0005C9F2 /* BabyBluetooth.m */; }; E1DEF5161B98857B0005C9F2 /* BabyCallback.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DEF50A1B98857B0005C9F2 /* BabyCallback.m */; }; - E1DEF5171B98857B0005C9F2 /* Babysister.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DEF50C1B98857B0005C9F2 /* Babysister.m */; }; E1DEF5181B98857B0005C9F2 /* BabySpeaker.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DEF50E1B98857B0005C9F2 /* BabySpeaker.m */; }; E1DEF5191B98857B0005C9F2 /* BabyToy.m in Sources */ = {isa = PBXBuildFile; fileRef = E1DEF5101B98857B0005C9F2 /* BabyToy.m */; }; - F05F7BA21B70AB0F0051C68A /* PeripheralViewContriller.m in Sources */ = {isa = PBXBuildFile; fileRef = F05F7BA11B70AB0F0051C68A /* PeripheralViewContriller.m */; }; + F05F7BA21B70AB0F0051C68A /* PeripheralViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F05F7BA11B70AB0F0051C68A /* PeripheralViewController.m */; }; F05F7BA61B733A240051C68A /* PeripheralInfo.m in Sources */ = {isa = PBXBuildFile; fileRef = F05F7BA51B733A240051C68A /* PeripheralInfo.m */; }; F05F7BAD1B7372300051C68A /* SVIndefiniteAnimatedView.m in Sources */ = {isa = PBXBuildFile; fileRef = F05F7BA91B7372300051C68A /* SVIndefiniteAnimatedView.m */; }; F05F7BAE1B7372300051C68A /* SVProgressHUD.bundle in Resources */ = {isa = PBXBuildFile; fileRef = F05F7BAA1B7372300051C68A /* SVProgressHUD.bundle */; }; @@ -29,39 +31,43 @@ /* End PBXBuildFile section */ /* Begin PBXFileReference section */ + 79CD9BD01CDB7395009DAD1C /* BabyDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyDefine.h; sourceTree = ""; }; + 79CD9BD11CDB7395009DAD1C /* BabyDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyDefine.m; sourceTree = ""; }; E13AD4D31BB78EF900D61C84 /* BabyOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyOptions.h; sourceTree = ""; }; E13AD4D41BB78EF900D61C84 /* BabyOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyOptions.m; sourceTree = ""; }; + E16F24A61C20484E00A5C274 /* BabyPeripheralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyPeripheralManager.h; sourceTree = ""; }; + E16F24A71C20484E00A5C274 /* BabyPeripheralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyPeripheralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + E16F24A91C2123B800A5C274 /* BabyCentralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCentralManager.h; sourceTree = ""; }; + E16F24AA1C2123B800A5C274 /* BabyCentralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyCentralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; E1807E6D1B6CA29100E99C63 /* BabyBluetoothAppDemo.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BabyBluetoothAppDemo.app; sourceTree = BUILT_PRODUCTS_DIR; }; E1807E711B6CA29100E99C63 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; E1807E721B6CA29100E99C63 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; E1807E741B6CA29100E99C63 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; E1807E751B6CA29100E99C63 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; E1807E771B6CA29100E99C63 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; - E1807E781B6CA29100E99C63 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; + E1807E781B6CA29100E99C63 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = ViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; E1807E7B1B6CA29100E99C63 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; E1807E7D1B6CA29100E99C63 /* Images.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Images.xcassets; sourceTree = ""; }; E1807E801B6CA29100E99C63 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/LaunchScreen.xib; sourceTree = ""; }; E1DEF5071B98857B0005C9F2 /* BabyBluetooth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyBluetooth.h; sourceTree = ""; }; - E1DEF5081B98857B0005C9F2 /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyBluetooth.m; sourceTree = ""; }; - E1DEF5091B98857B0005C9F2 /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCallback.h; sourceTree = ""; }; - E1DEF50A1B98857B0005C9F2 /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCallback.m; sourceTree = ""; }; - E1DEF50B1B98857B0005C9F2 /* Babysister.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Babysister.h; sourceTree = ""; }; - E1DEF50C1B98857B0005C9F2 /* Babysister.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Babysister.m; sourceTree = ""; }; + E1DEF5081B98857B0005C9F2 /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyBluetooth.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + E1DEF5091B98857B0005C9F2 /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = BabyCallback.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + E1DEF50A1B98857B0005C9F2 /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyCallback.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; E1DEF50D1B98857B0005C9F2 /* BabySpeaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabySpeaker.h; sourceTree = ""; }; - E1DEF50E1B98857B0005C9F2 /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabySpeaker.m; sourceTree = ""; }; + E1DEF50E1B98857B0005C9F2 /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabySpeaker.m; sourceTree = ""; }; E1DEF50F1B98857B0005C9F2 /* BabyToy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyToy.h; sourceTree = ""; }; E1DEF5101B98857B0005C9F2 /* BabyToy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyToy.m; sourceTree = ""; }; - F05F7BA01B70AB0F0051C68A /* PeripheralViewContriller.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PeripheralViewContriller.h; sourceTree = ""; }; - F05F7BA11B70AB0F0051C68A /* PeripheralViewContriller.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PeripheralViewContriller.m; sourceTree = ""; }; + F05F7BA01B70AB0F0051C68A /* PeripheralViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PeripheralViewController.h; sourceTree = ""; }; + F05F7BA11B70AB0F0051C68A /* PeripheralViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PeripheralViewController.m; sourceTree = ""; }; F05F7BA41B733A240051C68A /* PeripheralInfo.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PeripheralInfo.h; sourceTree = ""; }; F05F7BA51B733A240051C68A /* PeripheralInfo.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = PeripheralInfo.m; sourceTree = ""; }; F05F7BA81B7372300051C68A /* SVIndefiniteAnimatedView.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVIndefiniteAnimatedView.h; sourceTree = ""; }; F05F7BA91B7372300051C68A /* SVIndefiniteAnimatedView.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVIndefiniteAnimatedView.m; sourceTree = ""; }; F05F7BAA1B7372300051C68A /* SVProgressHUD.bundle */ = {isa = PBXFileReference; lastKnownFileType = "wrapper.plug-in"; path = SVProgressHUD.bundle; sourceTree = ""; }; F05F7BAB1B7372300051C68A /* SVProgressHUD.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = SVProgressHUD.h; sourceTree = ""; }; - F05F7BAC1B7372300051C68A /* SVProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = SVProgressHUD.m; sourceTree = ""; }; + F05F7BAC1B7372300051C68A /* SVProgressHUD.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = SVProgressHUD.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; F05F7BB01B7442B50051C68A /* CharacteristicViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CharacteristicViewController.h; sourceTree = ""; }; - F05F7BB11B7442B50051C68A /* CharacteristicViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CharacteristicViewController.m; sourceTree = ""; }; + F05F7BB11B7442B50051C68A /* CharacteristicViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = CharacteristicViewController.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; F0644E4F1BA7B1C600AA0F9F /* BabyRhythm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyRhythm.h; sourceTree = ""; }; F0644E501BA7B1C600AA0F9F /* BabyRhythm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyRhythm.m; sourceTree = ""; }; /* End PBXFileReference section */ @@ -102,8 +108,8 @@ E1807E751B6CA29100E99C63 /* AppDelegate.m */, E1807E771B6CA29100E99C63 /* ViewController.h */, E1807E781B6CA29100E99C63 /* ViewController.m */, - F05F7BA01B70AB0F0051C68A /* PeripheralViewContriller.h */, - F05F7BA11B70AB0F0051C68A /* PeripheralViewContriller.m */, + F05F7BA01B70AB0F0051C68A /* PeripheralViewController.h */, + F05F7BA11B70AB0F0051C68A /* PeripheralViewController.m */, F05F7BB01B7442B50051C68A /* CharacteristicViewController.h */, F05F7BB11B7442B50051C68A /* CharacteristicViewController.m */, E1807E7A1B6CA29100E99C63 /* Main.storyboard */, @@ -127,21 +133,25 @@ E1807EA91B6CA53A00E99C63 /* Framework */ = { isa = PBXGroup; children = ( - E1DEF5051B98857A0005C9F2 /* BabyBluetoothLib */, + E1DEF5051B98857A0005C9F2 /* BabyBluetooth */, F05F7BA71B7372300051C68A /* SVProgressHUD */, ); name = Framework; sourceTree = ""; }; - E1DEF5051B98857A0005C9F2 /* BabyBluetoothLib */ = { + E1DEF5051B98857A0005C9F2 /* BabyBluetooth */ = { isa = PBXGroup; children = ( + 79CD9BD01CDB7395009DAD1C /* BabyDefine.h */, + 79CD9BD11CDB7395009DAD1C /* BabyDefine.m */, + E16F24A91C2123B800A5C274 /* BabyCentralManager.h */, + E16F24AA1C2123B800A5C274 /* BabyCentralManager.m */, + E16F24A61C20484E00A5C274 /* BabyPeripheralManager.h */, + E16F24A71C20484E00A5C274 /* BabyPeripheralManager.m */, E1DEF5071B98857B0005C9F2 /* BabyBluetooth.h */, E1DEF5081B98857B0005C9F2 /* BabyBluetooth.m */, E1DEF5091B98857B0005C9F2 /* BabyCallback.h */, E1DEF50A1B98857B0005C9F2 /* BabyCallback.m */, - E1DEF50B1B98857B0005C9F2 /* Babysister.h */, - E1DEF50C1B98857B0005C9F2 /* Babysister.m */, E1DEF50D1B98857B0005C9F2 /* BabySpeaker.h */, E1DEF50E1B98857B0005C9F2 /* BabySpeaker.m */, E1DEF50F1B98857B0005C9F2 /* BabyToy.h */, @@ -151,7 +161,7 @@ E13AD4D31BB78EF900D61C84 /* BabyOptions.h */, E13AD4D41BB78EF900D61C84 /* BabyOptions.m */, ); - name = BabyBluetoothLib; + name = BabyBluetooth; path = ../../Classes/objc; sourceTree = ""; }; @@ -207,7 +217,7 @@ TargetAttributes = { E1807E6C1B6CA29100E99C63 = { CreatedOnToolsVersion = 6.3.1; - DevelopmentTeam = 9V9UW57Z55; + DevelopmentTeam = UK5GAN4FW3; SystemCapabilities = { com.apple.BackgroundModes = { enabled = 0; @@ -254,12 +264,14 @@ buildActionMask = 2147483647; files = ( E1DEF5151B98857B0005C9F2 /* BabyBluetooth.m in Sources */, - F05F7BA21B70AB0F0051C68A /* PeripheralViewContriller.m in Sources */, + E16F24AB1C2123B800A5C274 /* BabyCentralManager.m in Sources */, + F05F7BA21B70AB0F0051C68A /* PeripheralViewController.m in Sources */, E1807E791B6CA29100E99C63 /* ViewController.m in Sources */, + 79CD9BD21CDB7395009DAD1C /* BabyDefine.m in Sources */, F05F7BA61B733A240051C68A /* PeripheralInfo.m in Sources */, F05F7BAF1B7372300051C68A /* SVProgressHUD.m in Sources */, - E1DEF5171B98857B0005C9F2 /* Babysister.m in Sources */, E1807E761B6CA29100E99C63 /* AppDelegate.m in Sources */, + E16F24A81C20484E00A5C274 /* BabyPeripheralManager.m in Sources */, E1807E731B6CA29100E99C63 /* main.m in Sources */, F05F7BB21B7442B50051C68A /* CharacteristicViewController.m in Sources */, E13AD4D51BB78EF900D61C84 /* BabyOptions.m in Sources */, @@ -380,10 +392,11 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = UK5GAN4FW3; INFOPLIST_FILE = BabyBluetoothAppDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "com.jumppo.app.demo.$(PRODUCT_NAME:rfc1034identifier)"; + PRODUCT_BUNDLE_IDENTIFIER = com.liuyanwei.babyBluetooth; PRODUCT_NAME = BabyBluetoothAppDemo; PROVISIONING_PROFILE = ""; }; @@ -395,10 +408,11 @@ ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; CODE_SIGN_IDENTITY = "iPhone Developer"; "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + DEVELOPMENT_TEAM = UK5GAN4FW3; INFOPLIST_FILE = BabyBluetoothAppDemo/Info.plist; - IPHONEOS_DEPLOYMENT_TARGET = 9.0; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; - PRODUCT_BUNDLE_IDENTIFIER = "com.jumppo.app.demo.$(PRODUCT_NAME:rfc1034identifier)"; + PRODUCT_BUNDLE_IDENTIFIER = com.liuyanwei.babyBluetooth; PRODUCT_NAME = BabyBluetoothAppDemo; PROVISIONING_PROFILE = ""; }; diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/AppDelegate.h b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/AppDelegate.h index a717617..2805953 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/AppDelegate.h +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/AppDelegate.h @@ -10,7 +10,7 @@ @interface AppDelegate : UIResponder -@property (strong, nonatomic) UIWindow *window; +@property (nonatomic, strong) UIWindow *window; @end diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.h b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.h index 16df2fe..411fb03 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.h +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.h @@ -24,5 +24,4 @@ @property (nonatomic,strong)CBCharacteristic *characteristic; @property (nonatomic,strong)CBPeripheral *currPeripheral; - @end diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.m b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.m index e7c5238..996a75e 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.m +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/CharacteristicViewController.m @@ -135,13 +135,13 @@ -(void)setNotifiy:(id)sender{ __weak typeof(self)weakSelf = self; UIButton *btn = sender; - if(self.currPeripheral.state != CBPeripheralStateConnected){ + if(self.currPeripheral.state != CBPeripheralStateConnected) { [SVProgressHUD showErrorWithStatus:@"peripheral已经断开连接,请重新连接"]; return; } - if (self.characteristic.properties & CBCharacteristicPropertyNotify || self.characteristic.properties & CBCharacteristicPropertyIndicate){ + if (self.characteristic.properties & CBCharacteristicPropertyNotify || self.characteristic.properties & CBCharacteristicPropertyIndicate) { - if(self.characteristic.isNotifying){ + if(self.characteristic.isNotifying) { [baby cancelNotify:self.currPeripheral characteristic:self.characteristic]; [btn setTitle:@"通知" forState:UIControlStateNormal]; }else{ @@ -300,7 +300,7 @@ -(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger) [setNotifiyBtn setBackgroundColor:[UIColor darkGrayColor]]; [setNotifiyBtn addTarget:self action:@selector(setNotifiy:) forControlEvents:UIControlEventTouchUpInside]; //恢复状态 - if(self.characteristic.isNotifying){ + if(self.characteristic.isNotifying) { [baby notify:self.currPeripheral characteristic:self.characteristic block:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) { NSLog(@"resume notify block"); [self insertReadValues:characteristics]; diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/Info.plist b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/Info.plist index 4170e32..b612a44 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/Info.plist +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/Info.plist @@ -2,11 +2,6 @@ - UIBackgroundModes - - bluetooth-peripheral - bluetooth-central - CFBundleDevelopmentRegion en CFBundleExecutable @@ -27,6 +22,13 @@ 1 LSRequiresIPhoneOS + NSBluetoothPeripheralUsageDescription + BabyBluetooth能请求打开蓝牙 + UIBackgroundModes + + bluetooth-peripheral + bluetooth-central + UILaunchStoryboardName LaunchScreen UIMainStoryboardFile diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralInfo.m b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralInfo.m index 4dffb49..b09f82e 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralInfo.m +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralInfo.m @@ -13,7 +13,7 @@ @implementation PeripheralInfo -(instancetype)init{ self = [super init]; if (self) { - self.characteristics = [[NSMutableArray alloc]init]; + _characteristics = [[NSMutableArray alloc]init]; } return self; } diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.h b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.h similarity index 88% rename from BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.h rename to BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.h index c1c6df7..090a94d 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.h +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.h @@ -14,7 +14,7 @@ #import "CharacteristicViewController.h" -@interface PeripheralViewContriller : UITableViewController{ +@interface PeripheralViewController : UITableViewController{ @public BabyBluetooth *baby; } diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.m b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.m similarity index 98% rename from BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.m rename to BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.m index f4e123d..ae52681 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewContriller.m +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/PeripheralViewController.m @@ -6,17 +6,17 @@ // Copyright (c) 2015年 刘彦玮. All rights reserved. // -#import "PeripheralViewContriller.h" +#import "PeripheralViewController.h" #define width [UIScreen mainScreen].bounds.size.width #define height [UIScreen mainScreen].bounds.size.height #define channelOnPeropheralView @"peripheralView" -@interface PeripheralViewContriller () +@interface PeripheralViewController () @end -@implementation PeripheralViewContriller{ +@implementation PeripheralViewController{ } diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.h b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.h index df1d6f6..6c16f9b 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.h +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.h @@ -9,11 +9,11 @@ #import #import #import "BabyBluetooth.h" -#import "PeripheralViewContriller.h" +#import "PeripheralViewController.h" @interface ViewController : UIViewController -@property (weak, nonatomic) IBOutlet UITableView *tableView; +@property (nonatomic, weak) IBOutlet UITableView *tableView; @end diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.m b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.m index ac5446e..43dc819 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.m +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/BabyBluetoothAppDemo/ViewController.m @@ -15,9 +15,7 @@ #define height [UIScreen mainScreen].bounds.size.height @interface ViewController (){ -// UITableView *tableView; - NSMutableArray *peripherals; - NSMutableArray *peripheralsAD; + NSMutableArray *peripheralDataArray; BabyBluetooth *baby; } @@ -28,26 +26,18 @@ @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; - NSLog(@"viewDidLoad"); [SVProgressHUD showInfoWithStatus:@"准备打开设备"]; + NSLog(@"viewDidLoad"); + peripheralDataArray = [[NSMutableArray alloc]init]; - //初始化其他数据 init other - peripherals = [[NSMutableArray alloc]init]; - peripheralsAD = [[NSMutableArray alloc]init]; - //初始化BabyBluetooth 蓝牙库 baby = [BabyBluetooth shareBabyBluetooth]; //设置蓝牙委托 [self babyDelegate]; - - //启动一个定时任务 - [NSTimer scheduledTimerWithTimeInterval:5 target:self selector:@selector(timerTask) userInfo:nil repeats:YES]; + } --(void)timerTask{ -// NSLog(@"timerTask"); -} -(void)viewDidAppear:(BOOL)animated{ NSLog(@"viewDidAppear"); @@ -77,22 +67,10 @@ -(void)babyDelegate{ //设置扫描到设备的委托 [baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) { NSLog(@"搜索到了设备:%@",peripheral.name); - [weakSelf insertTableView:peripheral advertisementData:advertisementData]; + [weakSelf insertTableView:peripheral advertisementData:advertisementData RSSI:RSSI]; }]; - //设置发现设备的Services的委托 - [baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) { - for (CBService *service in peripheral.services) { - NSLog(@"搜索到服务:%@",service.UUID.UUIDString); - } - //找到cell并修改detaisText - for (int i=0;i 2 - if (peripheralName.length >2) { + //最常用的场景是查找某一个前缀开头的设备 +// if ([peripheralName hasPrefix:@"Pxxxx"] ) { +// return YES; +// } +// return NO; + + //设置查找规则是名称大于0 , the search rule is peripheral.name length > 0 + if (peripheralName.length >0) { return YES; } return NO; @@ -160,13 +144,20 @@ -(void)babyDelegate{ #pragma mark -UIViewController 方法 //插入table数据 --(void)insertTableView:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData{ - if(![peripherals containsObject:peripheral]){ +-(void)insertTableView:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI{ + + NSArray *peripherals = [peripheralDataArray valueForKey:@"peripheral"]; + if(![peripherals containsObject:peripheral]) { NSMutableArray *indexPaths = [[NSMutableArray alloc] init]; NSIndexPath *indexPath = [NSIndexPath indexPathForRow:peripherals.count inSection:0]; [indexPaths addObject:indexPath]; - [peripherals addObject:peripheral]; - [peripheralsAD addObject:advertisementData]; + + NSMutableDictionary *item = [[NSMutableDictionary alloc] init]; + [item setValue:peripheral forKey:@"peripheral"]; + [item setValue:RSSI forKey:@"RSSI"]; + [item setValue:advertisementData forKey:@"advertisementData"]; + [peripheralDataArray addObject:item]; + [self.tableView insertRowsAtIndexPaths:indexPaths withRowAnimation:UITableViewRowAnimationAutomatic]; } } @@ -174,43 +165,40 @@ -(void)insertTableView:(CBPeripheral *)peripheral advertisementData:(NSDictionar #pragma mark -table委托 table delegate - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{ - return peripherals.count; + return peripheralDataArray.count; } -- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ +- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{ + UITableViewCell *cell = [self.tableView dequeueReusableCellWithIdentifier:@"cell"]; - CBPeripheral *peripheral = [peripherals objectAtIndex:indexPath.row]; - NSDictionary *ad = [peripheralsAD objectAtIndex:indexPath.row]; - + NSDictionary *item = [peripheralDataArray objectAtIndex:indexPath.row]; + CBPeripheral *peripheral = [item objectForKey:@"peripheral"]; + NSDictionary *advertisementData = [item objectForKey:@"advertisementData"]; + NSNumber *RSSI = [item objectForKey:@"RSSI"]; + if (!cell) { cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:@"cell"]; cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; } cell.selectionStyle = UITableViewCellSelectionStyleNone; - + //peripheral的显示名称,优先用kCBAdvDataLocalName的定义,若没有再使用peripheral name - NSString *localName; - if ([ad objectForKey:@"kCBAdvDataLocalName"]) { - localName = [NSString stringWithFormat:@"%@",[ad objectForKey:@"kCBAdvDataLocalName"]]; + NSString *peripheralName; + if ([advertisementData objectForKey:@"kCBAdvDataLocalName"]) { + peripheralName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + }else if(!([peripheral.name isEqualToString:@""] || peripheral.name == nil)){ + peripheralName = peripheral.name; }else{ - localName = peripheral.name; + peripheralName = [peripheral.identifier UUIDString]; } - cell.textLabel.text = localName; + cell.textLabel.text = peripheralName; //信号和服务 - cell.detailTextLabel.text = @"读取中..."; - //找到cell并修改detaisText - NSArray *serviceUUIDs = [ad objectForKey:@"kCBAdvDataServiceUUIDs"]; - if (serviceUUIDs) { - cell.detailTextLabel.text = [NSString stringWithFormat:@"%lu个service",(unsigned long)serviceUUIDs.count]; - }else{ - cell.detailTextLabel.text = [NSString stringWithFormat:@"0个service"]; - } + cell.detailTextLabel.text = [NSString stringWithFormat:@"RSSI:%@",RSSI]; - //次线程读取RSSI和服务数量 return cell; } @@ -220,8 +208,10 @@ -(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath * [baby cancelScan]; [self.tableView deselectRowAtIndexPath:indexPath animated:YES]; - PeripheralViewContriller *vc = [[PeripheralViewContriller alloc]init]; - vc.currPeripheral = [peripherals objectAtIndex:indexPath.row]; + PeripheralViewController *vc = [[PeripheralViewController alloc]init]; + NSDictionary *item = [peripheralDataArray objectAtIndex:indexPath.row]; + CBPeripheral *peripheral = [item objectForKey:@"peripheral"]; + vc.currPeripheral = peripheral; vc->baby = self->baby; [self.navigationController pushViewController:vc animated:YES]; diff --git a/BabyBluetoothExamples/BabyBluetoothAppDemo/SVProgressHUD/SVProgressHUD.m b/BabyBluetoothExamples/BabyBluetoothAppDemo/SVProgressHUD/SVProgressHUD.m index 1d735d2..e6ecdaa 100644 --- a/BabyBluetoothExamples/BabyBluetoothAppDemo/SVProgressHUD/SVProgressHUD.m +++ b/BabyBluetoothExamples/BabyBluetoothAppDemo/SVProgressHUD/SVProgressHUD.m @@ -588,10 +588,10 @@ - (void)overlayViewDidReceiveTouchEvent:(id)sender forEvent:(UIEvent *)event { #pragma mark - Master show/dismiss methods - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgressHUDMaskType)hudMaskType { - if(!self.overlayView.superview){ + if(!self.overlayView.superview) { #if !defined(SV_APP_EXTENSIONS) NSEnumerator *frontToBackWindows = [UIApplication.sharedApplication.windows reverseObjectEnumerator]; - for (UIWindow *window in frontToBackWindows){ + for (UIWindow *window in frontToBackWindows) { BOOL windowOnMainScreen = window.screen == UIScreen.mainScreen; BOOL windowIsVisible = !window.hidden && window.alpha > 0; BOOL windowLevelNormal = window.windowLevel == UIWindowLevelNormal; @@ -602,7 +602,7 @@ - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgre } } #else - if(SVProgressHUDExtensionView){ + if(SVProgressHUDExtensionView) { [SVProgressHUDExtensionView addSubview:self.overlayView]; } #endif @@ -676,7 +676,7 @@ - (void)showProgress:(float)progress status:(NSString*)string maskType:(SVProgre else self.alpha = 1; } - completion:^(BOOL finished){ + completion:^(BOOL finished) { [[NSNotificationCenter defaultCenter] postNotificationName:SVProgressHUDDidAppearNotification object:nil userInfo:userInfo]; @@ -756,7 +756,7 @@ - (void)dismiss { else self.alpha = 0.0f; } - completion:^(BOOL finished){ + completion:^(BOOL finished) { if(self.alpha == 0.0f || self.hudView.alpha == 0.0f) { self.alpha = 0.0f; self.hudView.alpha = 0.0f; diff --git a/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo.xcodeproj/project.pbxproj b/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo.xcodeproj/project.pbxproj index 3f5485e..c4e5a58 100644 --- a/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo.xcodeproj/project.pbxproj +++ b/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo.xcodeproj/project.pbxproj @@ -7,8 +7,11 @@ objects = { /* Begin PBXBuildFile section */ - F0E079311BB92B1F0092509E /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = F0E0792E1BB92B1F0092509E /* BabyOptions.m */; settings = {ASSET_TAGS = (); }; }; - F0E079321BB92B1F0092509E /* BabyRhythm.m in Sources */ = {isa = PBXBuildFile; fileRef = F0E079301BB92B1F0092509E /* BabyRhythm.m */; settings = {ASSET_TAGS = (); }; }; + 79CD9BD51CDB7457009DAD1C /* BabyDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 79CD9BD41CDB7457009DAD1C /* BabyDefine.m */; }; + E16F24AE1C21298200A5C274 /* BabyCentralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E16F24AD1C21298200A5C274 /* BabyCentralManager.m */; }; + E1E08ADE1C1F0A77007356D3 /* BabyPeripheralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E1E08ADD1C1F0A77007356D3 /* BabyPeripheralManager.m */; }; + F0E079311BB92B1F0092509E /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = F0E0792E1BB92B1F0092509E /* BabyOptions.m */; }; + F0E079321BB92B1F0092509E /* BabyRhythm.m in Sources */ = {isa = PBXBuildFile; fileRef = F0E079301BB92B1F0092509E /* BabyRhythm.m */; }; F0F5349E1B9C0FBE00285A3C /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F5349D1B9C0FBE00285A3C /* AppDelegate.m */; }; F0F534A01B9C0FBE00285A3C /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F5349F1B9C0FBE00285A3C /* main.m */; }; F0F534A31B9C0FBE00285A3C /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534A21B9C0FBE00285A3C /* ViewController.m */; }; @@ -17,7 +20,6 @@ F0F534B41B9C0FBE00285A3C /* BabyBluetoothOSDemoTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534B31B9C0FBE00285A3C /* BabyBluetoothOSDemoTests.m */; }; F0F534C81B9C104100285A3C /* BabyBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534BF1B9C104100285A3C /* BabyBluetooth.m */; }; F0F534C91B9C104100285A3C /* BabyCallback.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534C11B9C104100285A3C /* BabyCallback.m */; }; - F0F534CA1B9C104100285A3C /* Babysister.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534C31B9C104100285A3C /* Babysister.m */; }; F0F534CB1B9C104100285A3C /* BabySpeaker.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534C51B9C104100285A3C /* BabySpeaker.m */; }; F0F534CC1B9C104100285A3C /* BabyToy.m in Sources */ = {isa = PBXBuildFile; fileRef = F0F534C71B9C104100285A3C /* BabyToy.m */; }; /* End PBXBuildFile section */ @@ -33,6 +35,12 @@ /* End PBXContainerItemProxy section */ /* Begin PBXFileReference section */ + 79CD9BD31CDB7457009DAD1C /* BabyDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyDefine.h; sourceTree = ""; }; + 79CD9BD41CDB7457009DAD1C /* BabyDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyDefine.m; sourceTree = ""; }; + E16F24AC1C21298200A5C274 /* BabyCentralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCentralManager.h; sourceTree = ""; }; + E16F24AD1C21298200A5C274 /* BabyCentralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCentralManager.m; sourceTree = ""; }; + E1E08ADC1C1F0A77007356D3 /* BabyPeripheralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyPeripheralManager.h; sourceTree = ""; }; + E1E08ADD1C1F0A77007356D3 /* BabyPeripheralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyPeripheralManager.m; sourceTree = ""; }; F0E0792D1BB92B1F0092509E /* BabyOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyOptions.h; sourceTree = ""; }; F0E0792E1BB92B1F0092509E /* BabyOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyOptions.m; sourceTree = ""; }; F0E0792F1BB92B1F0092509E /* BabyRhythm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyRhythm.h; sourceTree = ""; }; @@ -53,8 +61,6 @@ F0F534BF1B9C104100285A3C /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyBluetooth.m; sourceTree = ""; }; F0F534C01B9C104100285A3C /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCallback.h; sourceTree = ""; }; F0F534C11B9C104100285A3C /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCallback.m; sourceTree = ""; }; - F0F534C21B9C104100285A3C /* Babysister.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Babysister.h; sourceTree = ""; }; - F0F534C31B9C104100285A3C /* Babysister.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = Babysister.m; sourceTree = ""; }; F0F534C41B9C104100285A3C /* BabySpeaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabySpeaker.h; sourceTree = ""; }; F0F534C51B9C104100285A3C /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabySpeaker.m; sourceTree = ""; }; F0F534C61B9C104100285A3C /* BabyToy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyToy.h; sourceTree = ""; }; @@ -141,6 +147,12 @@ F0F534BD1B9C104100285A3C /* BabyBluetooth */ = { isa = PBXGroup; children = ( + 79CD9BD31CDB7457009DAD1C /* BabyDefine.h */, + 79CD9BD41CDB7457009DAD1C /* BabyDefine.m */, + E16F24AC1C21298200A5C274 /* BabyCentralManager.h */, + E16F24AD1C21298200A5C274 /* BabyCentralManager.m */, + E1E08ADC1C1F0A77007356D3 /* BabyPeripheralManager.h */, + E1E08ADD1C1F0A77007356D3 /* BabyPeripheralManager.m */, F0E0792D1BB92B1F0092509E /* BabyOptions.h */, F0E0792E1BB92B1F0092509E /* BabyOptions.m */, F0E0792F1BB92B1F0092509E /* BabyRhythm.h */, @@ -149,8 +161,6 @@ F0F534BF1B9C104100285A3C /* BabyBluetooth.m */, F0F534C01B9C104100285A3C /* BabyCallback.h */, F0F534C11B9C104100285A3C /* BabyCallback.m */, - F0F534C21B9C104100285A3C /* Babysister.h */, - F0F534C31B9C104100285A3C /* Babysister.m */, F0F534C41B9C104100285A3C /* BabySpeaker.h */, F0F534C51B9C104100285A3C /* BabySpeaker.m */, F0F534C61B9C104100285A3C /* BabyToy.h */, @@ -259,6 +269,8 @@ isa = PBXSourcesBuildPhase; buildActionMask = 2147483647; files = ( + 79CD9BD51CDB7457009DAD1C /* BabyDefine.m in Sources */, + E1E08ADE1C1F0A77007356D3 /* BabyPeripheralManager.m in Sources */, F0F534A31B9C0FBE00285A3C /* ViewController.m in Sources */, F0E079321BB92B1F0092509E /* BabyRhythm.m in Sources */, F0F534A01B9C0FBE00285A3C /* main.m in Sources */, @@ -267,8 +279,8 @@ F0F534C91B9C104100285A3C /* BabyCallback.m in Sources */, F0F5349E1B9C0FBE00285A3C /* AppDelegate.m in Sources */, F0F534CC1B9C104100285A3C /* BabyToy.m in Sources */, + E16F24AE1C21298200A5C274 /* BabyCentralManager.m in Sources */, F0E079311BB92B1F0092509E /* BabyOptions.m in Sources */, - F0F534CA1B9C104100285A3C /* Babysister.m in Sources */, ); runOnlyForDeploymentPostprocessing = 0; }; diff --git a/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo/ViewController.m b/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo/ViewController.m index 6f9056a..5b68e74 100644 --- a/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo/ViewController.m +++ b/BabyBluetoothExamples/BabyBluetoothOSDemo/BabyBluetoothOSDemo/ViewController.m @@ -20,20 +20,13 @@ - (void)viewDidLoad { baby = [BabyBluetooth shareBabyBluetooth]; //设置蓝牙委托 [self babyDelegate]; - __weak typeof(baby) weakBaby = baby; - //因为蓝牙设备打开需要时间,所以只有监听到蓝牙设备状态打开后才能安全的使用蓝牙 - [baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) { - if (central.state == CBCentralManagerStatePoweredOn) { - //设置peripheral 然后读取services,然后读取characteristics名称和值和属性,获取characteristics对应的description的名称和值 - weakBaby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics() - .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); - } - }]; + baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics() + .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); } //蓝牙网关初始化和委托方法设置 --(void)babyDelegate{ +- (void)babyDelegate{ __weak typeof(baby) weakBaby = baby; @@ -43,9 +36,9 @@ -(void)babyDelegate{ }]; //设置查找设备的过滤器 - [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) { + [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { //设置查找规则是名称大于1 , the search rule is peripheral.name length > 2 - if (peripheralName.length >2) { + if (peripheralName.length >1) { return YES; } return NO; @@ -53,13 +46,16 @@ -(void)babyDelegate{ //连接过滤器 __block BOOL isFirst = YES; - [baby setFilterOnConnetToPeripherals:^BOOL(NSString *peripheralName) { + [baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + //这里的规则是:连接第一个设备 + isFirst = NO; + return YES; //这里的规则是:连接第一个P打头的设备 - if(isFirst && [peripheralName hasPrefix:@"P"]){ - isFirst = NO; - return YES; - } - return NO; +// if(isFirst && [peripheralName hasPrefix:@"“刘彦玮”"]){ +// isFirst = NO; +// return YES; +// } +// return NO; }]; //设置设备连接成功的委托 diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.pbxproj b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.pbxproj new file mode 100644 index 0000000..caa8dad --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.pbxproj @@ -0,0 +1,378 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 79CD9BD81CDB7472009DAD1C /* BabyDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 79CD9BD71CDB7472009DAD1C /* BabyDefine.m */; }; + E1D0BC281C1B189B00F46E04 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC271C1B189B00F46E04 /* main.m */; }; + E1D0BC2B1C1B189B00F46E04 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC2A1C1B189B00F46E04 /* AppDelegate.m */; }; + E1D0BC2E1C1B189B00F46E04 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC2D1C1B189B00F46E04 /* ViewController.m */; }; + E1D0BC311C1B189B00F46E04 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E1D0BC2F1C1B189B00F46E04 /* Main.storyboard */; }; + E1D0BC331C1B189B00F46E04 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = E1D0BC321C1B189B00F46E04 /* Assets.xcassets */; }; + E1D0BC361C1B189B00F46E04 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = E1D0BC341C1B189B00F46E04 /* LaunchScreen.storyboard */; }; + E1D0BC581C1D61AB00F46E04 /* peripheral_servers.json in Resources */ = {isa = PBXBuildFile; fileRef = E1D0BC571C1D61AB00F46E04 /* peripheral_servers.json */; }; + E1D0BC6A1C1DACE100F46E04 /* BabyBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC5B1C1DACE100F46E04 /* BabyBluetooth.m */; }; + E1D0BC6B1C1DACE100F46E04 /* BabyCallback.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC5D1C1DACE100F46E04 /* BabyCallback.m */; }; + E1D0BC6C1C1DACE100F46E04 /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC5F1C1DACE100F46E04 /* BabyOptions.m */; }; + E1D0BC6D1C1DACE100F46E04 /* BabyPeripheralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC611C1DACE100F46E04 /* BabyPeripheralManager.m */; }; + E1D0BC6E1C1DACE100F46E04 /* BabyRhythm.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC631C1DACE100F46E04 /* BabyRhythm.m */; }; + E1D0BC6F1C1DACE100F46E04 /* BabyCentralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC651C1DACE100F46E04 /* BabyCentralManager.m */; }; + E1D0BC701C1DACE100F46E04 /* BabySpeaker.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC671C1DACE100F46E04 /* BabySpeaker.m */; }; + E1D0BC711C1DACE100F46E04 /* BabyToy.m in Sources */ = {isa = PBXBuildFile; fileRef = E1D0BC691C1DACE100F46E04 /* BabyToy.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + 79CD9BD61CDB7472009DAD1C /* BabyDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyDefine.h; sourceTree = ""; }; + 79CD9BD71CDB7472009DAD1C /* BabyDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyDefine.m; sourceTree = ""; }; + E1D0BC231C1B189B00F46E04 /* BluetoothStubOnIOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BluetoothStubOnIOS.app; sourceTree = BUILT_PRODUCTS_DIR; }; + E1D0BC271C1B189B00F46E04 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + E1D0BC291C1B189B00F46E04 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + E1D0BC2A1C1B189B00F46E04 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + E1D0BC2C1C1B189B00F46E04 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; + E1D0BC2D1C1B189B00F46E04 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; + E1D0BC301C1B189B00F46E04 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + E1D0BC321C1B189B00F46E04 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + E1D0BC351C1B189B00F46E04 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + E1D0BC371C1B189B00F46E04 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + E1D0BC571C1D61AB00F46E04 /* peripheral_servers.json */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.json; path = peripheral_servers.json; sourceTree = ""; }; + E1D0BC5A1C1DACE100F46E04 /* BabyBluetooth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyBluetooth.h; sourceTree = ""; }; + E1D0BC5B1C1DACE100F46E04 /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyBluetooth.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + E1D0BC5C1C1DACE100F46E04 /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; lineEnding = 0; path = BabyCallback.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + E1D0BC5D1C1DACE100F46E04 /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyCallback.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + E1D0BC5E1C1DACE100F46E04 /* BabyOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyOptions.h; sourceTree = ""; }; + E1D0BC5F1C1DACE100F46E04 /* BabyOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyOptions.m; sourceTree = ""; }; + E1D0BC601C1DACE100F46E04 /* BabyPeripheralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyPeripheralManager.h; sourceTree = ""; }; + E1D0BC611C1DACE100F46E04 /* BabyPeripheralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyPeripheralManager.m; sourceTree = ""; }; + E1D0BC621C1DACE100F46E04 /* BabyRhythm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyRhythm.h; sourceTree = ""; }; + E1D0BC631C1DACE100F46E04 /* BabyRhythm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyRhythm.m; sourceTree = ""; }; + E1D0BC641C1DACE100F46E04 /* BabyCentralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCentralManager.h; sourceTree = ""; }; + E1D0BC651C1DACE100F46E04 /* BabyCentralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; lineEnding = 0; path = BabyCentralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + E1D0BC661C1DACE100F46E04 /* BabySpeaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabySpeaker.h; sourceTree = ""; }; + E1D0BC671C1DACE100F46E04 /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabySpeaker.m; sourceTree = ""; }; + E1D0BC681C1DACE100F46E04 /* BabyToy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyToy.h; sourceTree = ""; }; + E1D0BC691C1DACE100F46E04 /* BabyToy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyToy.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + E1D0BC201C1B189B00F46E04 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + E1D0BC1A1C1B189B00F46E04 = { + isa = PBXGroup; + children = ( + E1D0BC251C1B189B00F46E04 /* BluetoothStubOnIOS */, + E1D0BC241C1B189B00F46E04 /* Products */, + ); + sourceTree = ""; + }; + E1D0BC241C1B189B00F46E04 /* Products */ = { + isa = PBXGroup; + children = ( + E1D0BC231C1B189B00F46E04 /* BluetoothStubOnIOS.app */, + ); + name = Products; + sourceTree = ""; + }; + E1D0BC251C1B189B00F46E04 /* BluetoothStubOnIOS */ = { + isa = PBXGroup; + children = ( + E1D0BC591C1DACE100F46E04 /* BabyBluetooth */, + E1D0BC291C1B189B00F46E04 /* AppDelegate.h */, + E1D0BC2A1C1B189B00F46E04 /* AppDelegate.m */, + E1D0BC2C1C1B189B00F46E04 /* ViewController.h */, + E1D0BC2D1C1B189B00F46E04 /* ViewController.m */, + E1D0BC2F1C1B189B00F46E04 /* Main.storyboard */, + E1D0BC321C1B189B00F46E04 /* Assets.xcassets */, + E1D0BC341C1B189B00F46E04 /* LaunchScreen.storyboard */, + E1D0BC371C1B189B00F46E04 /* Info.plist */, + E1D0BC261C1B189B00F46E04 /* Supporting Files */, + E1D0BC571C1D61AB00F46E04 /* peripheral_servers.json */, + ); + path = BluetoothStubOnIOS; + sourceTree = ""; + }; + E1D0BC261C1B189B00F46E04 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + E1D0BC271C1B189B00F46E04 /* main.m */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + E1D0BC591C1DACE100F46E04 /* BabyBluetooth */ = { + isa = PBXGroup; + children = ( + 79CD9BD61CDB7472009DAD1C /* BabyDefine.h */, + 79CD9BD71CDB7472009DAD1C /* BabyDefine.m */, + E1D0BC5A1C1DACE100F46E04 /* BabyBluetooth.h */, + E1D0BC5B1C1DACE100F46E04 /* BabyBluetooth.m */, + E1D0BC5C1C1DACE100F46E04 /* BabyCallback.h */, + E1D0BC5D1C1DACE100F46E04 /* BabyCallback.m */, + E1D0BC5E1C1DACE100F46E04 /* BabyOptions.h */, + E1D0BC5F1C1DACE100F46E04 /* BabyOptions.m */, + E1D0BC601C1DACE100F46E04 /* BabyPeripheralManager.h */, + E1D0BC611C1DACE100F46E04 /* BabyPeripheralManager.m */, + E1D0BC621C1DACE100F46E04 /* BabyRhythm.h */, + E1D0BC631C1DACE100F46E04 /* BabyRhythm.m */, + E1D0BC641C1DACE100F46E04 /* BabyCentralManager.h */, + E1D0BC651C1DACE100F46E04 /* BabyCentralManager.m */, + E1D0BC661C1DACE100F46E04 /* BabySpeaker.h */, + E1D0BC671C1DACE100F46E04 /* BabySpeaker.m */, + E1D0BC681C1DACE100F46E04 /* BabyToy.h */, + E1D0BC691C1DACE100F46E04 /* BabyToy.m */, + ); + name = BabyBluetooth; + path = ../../../Classes/objc; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + E1D0BC221C1B189B00F46E04 /* BluetoothStubOnIOS */ = { + isa = PBXNativeTarget; + buildConfigurationList = E1D0BC3A1C1B189B00F46E04 /* Build configuration list for PBXNativeTarget "BluetoothStubOnIOS" */; + buildPhases = ( + E1D0BC1F1C1B189B00F46E04 /* Sources */, + E1D0BC201C1B189B00F46E04 /* Frameworks */, + E1D0BC211C1B189B00F46E04 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = BluetoothStubOnIOS; + productName = BluetoothStubOnIOS; + productReference = E1D0BC231C1B189B00F46E04 /* BluetoothStubOnIOS.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + E1D0BC1B1C1B189B00F46E04 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0700; + ORGANIZATIONNAME = "刘彦玮"; + TargetAttributes = { + E1D0BC221C1B189B00F46E04 = { + CreatedOnToolsVersion = 7.0; + DevelopmentTeam = QGDUF2E9QW; + }; + }; + }; + buildConfigurationList = E1D0BC1E1C1B189B00F46E04 /* Build configuration list for PBXProject "BluetoothStubOnIOS" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = E1D0BC1A1C1B189B00F46E04; + productRefGroup = E1D0BC241C1B189B00F46E04 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + E1D0BC221C1B189B00F46E04 /* BluetoothStubOnIOS */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + E1D0BC211C1B189B00F46E04 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E1D0BC581C1D61AB00F46E04 /* peripheral_servers.json in Resources */, + E1D0BC361C1B189B00F46E04 /* LaunchScreen.storyboard in Resources */, + E1D0BC331C1B189B00F46E04 /* Assets.xcassets in Resources */, + E1D0BC311C1B189B00F46E04 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + E1D0BC1F1C1B189B00F46E04 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 79CD9BD81CDB7472009DAD1C /* BabyDefine.m in Sources */, + E1D0BC2E1C1B189B00F46E04 /* ViewController.m in Sources */, + E1D0BC711C1DACE100F46E04 /* BabyToy.m in Sources */, + E1D0BC6E1C1DACE100F46E04 /* BabyRhythm.m in Sources */, + E1D0BC6F1C1DACE100F46E04 /* BabyCentralManager.m in Sources */, + E1D0BC2B1C1B189B00F46E04 /* AppDelegate.m in Sources */, + E1D0BC6A1C1DACE100F46E04 /* BabyBluetooth.m in Sources */, + E1D0BC6B1C1DACE100F46E04 /* BabyCallback.m in Sources */, + E1D0BC6D1C1DACE100F46E04 /* BabyPeripheralManager.m in Sources */, + E1D0BC701C1DACE100F46E04 /* BabySpeaker.m in Sources */, + E1D0BC6C1C1DACE100F46E04 /* BabyOptions.m in Sources */, + E1D0BC281C1B189B00F46E04 /* main.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + E1D0BC2F1C1B189B00F46E04 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + E1D0BC301C1B189B00F46E04 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + E1D0BC341C1B189B00F46E04 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + E1D0BC351C1B189B00F46E04 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + E1D0BC381C1B189B00F46E04 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + }; + name = Debug; + }; + E1D0BC391C1B189B00F46E04 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + E1D0BC3B1C1B189B00F46E04 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + INFOPLIST_FILE = BluetoothStubOnIOS/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.bluetoothStubOnIOS.BluetoothStubOnIOS; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + E1D0BC3C1C1B189B00F46E04 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + INFOPLIST_FILE = BluetoothStubOnIOS/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.bluetoothStubOnIOS.BluetoothStubOnIOS; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + E1D0BC1E1C1B189B00F46E04 /* Build configuration list for PBXProject "BluetoothStubOnIOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E1D0BC381C1B189B00F46E04 /* Debug */, + E1D0BC391C1B189B00F46E04 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + E1D0BC3A1C1B189B00F46E04 /* Build configuration list for PBXNativeTarget "BluetoothStubOnIOS" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + E1D0BC3B1C1B189B00F46E04 /* Debug */, + E1D0BC3C1C1B189B00F46E04 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = E1D0BC1B1C1B189B00F46E04 /* Project object */; +} diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..b00c886 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.h b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.h new file mode 100644 index 0000000..b0c094f --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.h @@ -0,0 +1,17 @@ +// +// AppDelegate.h +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/11. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + + +@end + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.m b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.m new file mode 100644 index 0000000..8b27d9d --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/AppDelegate.m @@ -0,0 +1,45 @@ +// +// AppDelegate.m +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/11. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import "AppDelegate.h" + +@interface AppDelegate () + +@end + +@implementation AppDelegate + + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Override point for customization after application launch. + return YES; +} + +- (void)applicationWillResignActive:(UIApplication *)application { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. +} + +- (void)applicationDidEnterBackground:(UIApplication *)application { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. +} + +- (void)applicationDidBecomeActive:(UIApplication *)application { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. +} + +- (void)applicationWillTerminate:(UIApplication *)application { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. +} + +@end diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Assets.xcassets/AppIcon.appiconset/Contents.json b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..118c98f --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,38 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/LaunchScreen.storyboard b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..2e721e1 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/Main.storyboard b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/Main.storyboard new file mode 100644 index 0000000..3978f81 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Base.lproj/Main.storyboard @@ -0,0 +1,63 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Info.plist b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Info.plist new file mode 100644 index 0000000..95cb486 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/Info.plist @@ -0,0 +1,42 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + NSBluetoothPeripheralUsageDescription + BabyBluetooth能请求打开蓝牙 + + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.h b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.h new file mode 100644 index 0000000..ebd76b3 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.h @@ -0,0 +1,15 @@ +// +// ViewController.h +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/11. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import + +@interface ViewController : UIViewController + + +@end + diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.m b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.m new file mode 100644 index 0000000..854709a --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/ViewController.m @@ -0,0 +1,118 @@ +// +// ViewController.m +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/11. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import "ViewController.h" +#import "BabyBluetooth.h" + + +@interface ViewController (){ + BabyBluetooth *baby; +} + +@end + +@implementation ViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + + //配置第一个服务s1 + CBMutableService *s1 = makeCBService(@"FFF0"); + //配置s1的3个characteristic + makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//读 + makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//写 + makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//读写,自动生成uuid + makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段 + makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段 + //配置第一个服务s2 + CBMutableService *s2 = makeCBService(@"FFE0"); + makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读 + //实例化baby + baby = [BabyBluetooth shareBabyBluetooth]; + //配置委托 + [self babyDelegate]; + //添加服务和启动外设 + baby.bePeripheral().addServices(@[s1,s2]).startAdvertising(); +} + +//配置委托 +- (void)babyDelegate{ + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) { + NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) { + NSLog(@"didStartAdvertising !!!"); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) { + NSLog(@"Did Add Service uuid: %@ ",service.UUID); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidReceiveReadRequest:^(CBPeripheralManager *peripheral,CBATTRequest *request) { + NSLog(@"request characteristic uuid:%@",request.characteristic.UUID); + //判断是否有读数据的权限 + if (request.characteristic.properties & CBCharacteristicPropertyRead) { + NSData *data = request.characteristic.value; + [request setValue:data]; + //对请求作出成功响应 + [peripheral respondToRequest:request withResult:CBATTErrorSuccess]; + }else{ + //错误的响应 + [peripheral respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; + } + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidReceiveWriteRequests:^(CBPeripheralManager *peripheral,NSArray *requests) { + NSLog(@"didReceiveWriteRequests"); + CBATTRequest *request = requests[0]; + //判断是否有写数据的权限 + if (request.characteristic.properties & CBCharacteristicPropertyWrite) { + //需要转换成CBMutableCharacteristic对象才能进行写值 + CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic; + c.value = request.value; + [peripheral respondToRequest:request withResult:CBATTErrorSuccess]; + }else{ + [peripheral respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; + } + + }]; + + __block NSTimer *timer; + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidSubscribeToCharacteristic:^(CBPeripheralManager *peripheral, CBCentral *central, CBCharacteristic *characteristic) { + NSLog(@"订阅了 %@的数据",characteristic.UUID); + //每秒执行一次给主设备发送一个当前时间的秒数 + timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(sendData:) userInfo:characteristic repeats:YES]; + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidUnSubscribeToCharacteristic:^(CBPeripheralManager *peripheral, CBCentral *central, CBCharacteristic *characteristic) { + NSLog(@"peripheralManagerIsReadyToUpdateSubscribers"); + [timer fireDate]; + }]; + +} + +//发送数据,发送当前时间的秒数 +-(BOOL)sendData:(NSTimer *)t { + CBMutableCharacteristic *characteristic = t.userInfo; + NSDateFormatter *dft = [[NSDateFormatter alloc]init]; + [dft setDateFormat:@"ss"]; +// NSLog(@"%@",[dft stringFromDate:[NSDate date]]); + //执行回应Central通知数据 + return [baby.peripheralManager updateValue:[[dft stringFromDate:[NSDate date]] dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:nil]; +} + +@end diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/main.m b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/main.m new file mode 100644 index 0000000..7d43c34 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/main.m @@ -0,0 +1,16 @@ +// +// main.m +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/11. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +} diff --git a/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/peripheral_servers.json b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/peripheral_servers.json new file mode 100644 index 0000000..ef57eb5 --- /dev/null +++ b/BabyBluetoothExamples/BluetoothStubOnIOS/BluetoothStubOnIOS/peripheral_servers.json @@ -0,0 +1,47 @@ +{ + "services": [ + { + "UUID":"services1", + "charateristics":[ + { + "UUID":"charateristics1", + "value":"", + "properties":"", + "permissions":"", + "descriptors":"" + }, + { + "UUID":"charateristics2", + "value":"", + "properties":"", + "permissions":"", + "descriptors":"" + } + ] + }, + { + "UUID":"services2", + "charateristics":[ + + { + "UUID":"charateristics1", + "value":"", + "properties":"", + "permissions":"", + "descriptors":"" + }, + { + "UUID":"charateristics2", + "value":"", + "properties":"", + "permissions":"", + "descriptors":"" + } + ] + } + ], + "advertising":{ + "UUIDs":[], + "LocalName":"" + } +} \ No newline at end of file diff --git a/BabyBluetoothExamples/BluetoothStubOnOSX/BluetoothStubOnOSX/ViewController.swift b/BabyBluetoothExamples/BluetoothStubOnOSX/BluetoothStubOnOSX/ViewController.swift index fb77609..e0027a4 100644 --- a/BabyBluetoothExamples/BluetoothStubOnOSX/BluetoothStubOnOSX/ViewController.swift +++ b/BabyBluetoothExamples/BluetoothStubOnOSX/BluetoothStubOnOSX/ViewController.swift @@ -21,7 +21,6 @@ class ViewController: NSViewController,CBPeripheralManagerDelegate{ let readCharacteristicUUID = "FFF2"; let readwriteCharacteristicUUID = "FFE3"; - var peripheralManager:CBPeripheralManager! var timer:NSTimer! @@ -139,10 +138,5 @@ class ViewController: NSViewController,CBPeripheralManagerDelegate{ func peripheralManagerIsReadyToUpdateSubscribers(peripheral: CBPeripheralManager) { NSLog("peripheralManagerIsReadyToUpdateSubscribers") } - - - - - } diff --git a/Classes/objc/BabyBluetooth.h b/Classes/objc/BabyBluetooth.h index 234e0d1..fb45387 100644 --- a/Classes/objc/BabyBluetooth.h +++ b/Classes/objc/BabyBluetooth.h @@ -2,6 +2,8 @@ BabyBluetooth 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 https://github.com/coolnameismy/BabyBluetooth + + version:0.7.0 */ // Created by 刘彦玮 on 15/3/31. @@ -11,225 +13,321 @@ #import #import -#import "Babysister.h" +#import "BabyCentralManager.h" +#import "BabyPeripheralManager.h" #import "BabyToy.h" #import "BabySpeaker.h" #import "BabyRhythm.h" - +#import "BabyDefine.h" @interface BabyBluetooth : NSObject -#pragma mark -babybluetooth的委托 -/* - 默认频道的委托 +#pragma mark - babybluetooth的委托 + +//默认频道的委托 + +/** +设备状态改变的block | when CentralManager state changed +*/ +- (void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block; + +/** + 找到Peripherals的block | when find peripheral */ -//设备状态改变的block -//when CentralManager state changed --(void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block; +- (void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block; -//找到Peripherals的block -//when find peripheral --(void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block; +/** +连接Peripherals成功的block +| when connected peripheral +*/ +- (void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block; -//连接Peripherals成功的block -//when connected peripheral --(void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block; +/** +连接Peripherals失败的block +| when fail to connect peripheral +*/ +- (void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; -//连接Peripherals失败的block -//when fail to connect peripheral --(void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; +/** +断开Peripherals的连接的block +| when disconnected peripheral +*/ +- (void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; -//断开Peripherals的连接的block -//when disconnected peripheral --(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; +/** +设置查找服务的block +| when discover services of peripheral +*/ +- (void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block; -//设置查找服务的block -//when discover services of peripheral --(void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block; +/** +设置查找到Characteristics的block +| when discovered Characteristics +*/ +- (void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block; -//设置查找到Characteristics的block -//when discovered Characteristics --(void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block; +/** +设置获取到最新Characteristics值的block +| when read new characteristics value or notiy a characteristics value +*/ +- (void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block; -//设置获取到最新Characteristics值的block -//when read new characteristics value or notiy a characteristics value --(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block; +/** +设置查找到Descriptors名称的block +| when discover descriptors for characteristic +*/ +- (void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block; -//设置查找到Descriptors名称的block -//when discover descriptors for characteristic --(void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block; +/** +设置读取到Descriptors值的block +| when read descriptors for characteristic +*/ +- (void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block; -//设置读取到Descriptors值的block -//when read descriptors for characteristic --(void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block; +/** +写Characteristic成功后的block +| when did write value for characteristic successed +*/ +- (void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block; -//写Characteristic成功后的block -//when did write value for characteristic successed --(void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block; +/** +写descriptor成功后的block +| when did write value for descriptor successed +*/ +- (void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block; -//写descriptor成功后的block -//when did write value for descriptor successed --(void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block; +/** +characteristic订阅状态改变的block +| when characteristic notification state changed +*/ +- (void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block; -//characteristic订阅状态改变的block -//when characteristic notification state changed --(void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block; +/** +读取RSSI的委托 +| when did read RSSI +*/ +- (void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block; -//读取RSSI的委托 -//when did read RSSI --(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block; +/** +discoverIncludedServices的回调,暂时在babybluetooth中无作用 +| no used in babybluetooth +*/ +- (void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block; -//discoverIncludedServices的回调,暂时在babybluetooth中无作用 -//no used in babybluetooth --(void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block; +/** +外设更新名字后的block +| when peripheral update name +*/ +- (void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block; -//外设更新名字后的block -//when peripheral update name --(void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block; +/** +外设更新服务后的block +| when peripheral update services +*/ +- (void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block; -//外设更新服务后的block -//when peripheral update services --(void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block; +// channel的委托 -/* - channel的委托 - */ -//设备状态改变的block -//when CentralManager state changed --(void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel +/** +设备状态改变的block +| when CentralManager state changed +*/ +- (void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel block:(void (^)(CBCentralManager *central))block; - -//找到Peripherals的block -//when find peripheral --(void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel +/** +找到Peripherals的block +| when find peripheral +*/ +- (void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block; -//连接Peripherals成功的block -//when connected peripheral --(void)setBlockOnConnectedAtChannel:(NSString *)channel +/** +连接Peripherals成功的block +| when connected peripheral +*/ +- (void)setBlockOnConnectedAtChannel:(NSString *)channel block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block; -//连接Peripherals失败的block -//when fail to connect peripheral --(void)setBlockOnFailToConnectAtChannel:(NSString *)channel +/** +连接Peripherals失败的block +| when fail to connect peripheral +*/ +- (void)setBlockOnFailToConnectAtChannel:(NSString *)channel block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; -//断开Peripherals的连接的block -//when disconnected peripheral --(void)setBlockOnDisconnectAtChannel:(NSString *)channel +/** +断开Peripherals的连接的block +| when disconnected peripheral +*/ +- (void)setBlockOnDisconnectAtChannel:(NSString *)channel block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block; -//设置查找服务的block -//when discover services of peripheral --(void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel +/** +设置查找服务的block +| when discover services of peripheral +*/ +- (void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral,NSError *error))block; -//设置查找到Characteristics的block -//when discovered Characteristics --(void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel +/** +设置查找到Characteristics的block +| when discovered Characteristics +*/ +- (void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block; -//设置获取到最新Characteristics值的block -//when read new characteristics value or notiy a characteristics value --(void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel +/** +设置获取到最新Characteristics值的block +| when read new characteristics value or notiy a characteristics value +*/ +- (void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block; -//设置查找到Characteristics描述的block -//when discover descriptors for characteristic --(void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel +/** +设置查找到Characteristics描述的block +| when discover descriptors for characteristic +*/ +- (void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block; -//设置读取到Characteristics描述的值的block -//when read descriptors for characteristic --(void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block; +/** +设置读取到Characteristics描述的值的block +| when read descriptors for characteristic +*/ +- (void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block; -//写Characteristic成功后的block -//when did write value for characteristic successed --(void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel +/** +写Characteristic成功后的block +| when did write value for characteristic successed +*/ +- (void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel block:(void (^)(CBCharacteristic *characteristic,NSError *error))block; -//写descriptor成功后的block -//when did write value for descriptor successed --(void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel +/** +写descriptor成功后的block +| when did write value for descriptor successed +*/ +- (void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel block:(void (^)(CBDescriptor *descriptor,NSError *error))block; -//characteristic订阅状态改变的block -//when characteristic notification state changed --(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel +/** +characteristic订阅状态改变的block +| when characteristic notification state changed +*/ +- (void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel block:(void (^)(CBCharacteristic *characteristic,NSError *error))block; -//读取RSSI的委托 -//when did read RSSI --(void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel +/** +读取RSSI的委托 +| when did read RSSI +*/ +- (void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel block:(void (^)(NSNumber *RSSI,NSError *error))block; -//discoverIncludedServices的回调,暂时在babybluetooth中无作用 -//no used in babybluetooth --(void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel +/** +discoverIncludedServices的回调,暂时在babybluetooth中无作用 +| no used in babybluetooth +*/ +- (void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel block:(void (^)(CBService *service,NSError *error))block; -//外设更新名字后的block -//when peripheral update name --(void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel + +/** +外设更新名字后的block +| when peripheral update name +*/ +- (void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral))block; -//外设更新服务后的block -//when peripheral update services --(void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel +/** +外设更新服务后的block +| when peripheral update services +*/ +- (void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block; -#pragma mark -babybluetooth filter +#pragma mark - babybluetooth filter + +/** +设置查找Peripherals的规则 +| filter of discover peripherals +*/ +- (void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter; + +/** +设置连接Peripherals的规则 +| setting filter of connect to peripherals peripherals +*/ +- (void)setFilterOnConnectToPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter; -//设置查找Peripherals的规则 -//filter of discover peripherals --(void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName))filter; -//设置连接Peripherals的规则 -//setting filter of connect to peripherals peripherals --(void)setFilterOnConnetToPeripherals:(BOOL (^)(NSString *peripheralName))filter; +/** +设置查找Peripherals的规则 +| filter of discover peripherals +*/ +- (void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel + filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter; +/** +设置连接Peripherals的规则 +| setting filter of connect to peripherals peripherals +*/ +- (void)setFilterOnConnectToPeripheralsAtChannel:(NSString *)channel + filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter; -//设置查找Peripherals的规则 -//filter of discover peripherals --(void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel - filter:(BOOL (^)(NSString *peripheralName))filter; -//设置连接Peripherals的规则 -//setting filter of connect to peripherals peripherals --(void)setFilterOnConnetToPeripheralsAtChannel:(NSString *)channel - filter:(BOOL (^)(NSString *peripheralName))filter; +#pragma mark - babybluetooth Special +/** +babyBluettooth cancelScan方法调用后的回调 +| when after call cancelScan +*/ +- (void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block; -#pragma mark -babybluetooth Special -//babyBluettooth cancelScan方法调用后的回调 --(void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block; -//babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调 --(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block; +/** +babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调 +| when did all peripheral disConnect +*/ +- (void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block; -//babyBluettooth cancelScan方法调用后的回调 --(void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel +/** +babyBluettooth cancelScan方法调用后的回调 +| when after call cancelScan +*/ +- (void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel block:(void(^)(CBCentralManager *centralManager))block; -//babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调 --(void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel + +/** +babyBluettooth cancelAllPeripheralsConnectionBlock 方法执行后并且全部设备断开后的回调 +| when did all peripheral disConnect +*/ +- (void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel block:(void(^)(CBCentralManager *centralManager))block; -//设置蓝牙运行时的参数 --(void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions +/** +设置蓝牙运行时的参数 +| set ble runtime parameters +*/ +- (void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithServices:(NSArray *)discoverWithServices discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics; -//设置蓝牙运行时的参数 --(void)setBabyOptionsAtChannel:(NSString *)channel + +/** +设置蓝牙运行时的参数 +| set ble runtime parameters +*/ +- (void)setBabyOptionsAtChannel:(NSString *)channel scanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices @@ -237,65 +335,220 @@ scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics; -#pragma mark -链式函数 -//查找Peripherals --(BabyBluetooth *(^)()) scanForPeripherals; -//连接Peripherals --(BabyBluetooth *(^)()) connectToPeripherals; -//发现Services --(BabyBluetooth *(^)()) discoverServices; -//获取Characteristics --(BabyBluetooth *(^)()) discoverCharacteristics; -//更新Characteristics的值 --(BabyBluetooth *(^)()) readValueForCharacteristic; -//获取Characteristics的名称 --(BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic; -//获取Descriptors的值 --(BabyBluetooth *(^)()) readValueForDescriptors; -//开始执行 --(BabyBluetooth *(^)()) begin; -//sec秒后停止 --(BabyBluetooth *(^)(int sec)) stop; -//持有对象 --(BabyBluetooth *(^)(id obj)) having; -//切换委托的频道 --(BabyBluetooth *(^)(NSString *channel)) channel; -//谓词,返回self --(BabyBluetooth *) and; --(BabyBluetooth *) then; --(BabyBluetooth *) with; - -#pragma mark -工具方法 -//断开连接 --(void)cancelPeripheralConnection:(CBPeripheral *)peripheral; -//断开所有连接 --(void)cancelAllPeripheralsConnection; -//停止扫描 --(void)cancelScan; -//更新Characteristics的值 --(BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails; -//设置characteristic的notify --(void)notify:(CBPeripheral *)peripheral +#pragma mark - 链式函数 + +/** +查找Peripherals + */ +- (BabyBluetooth *(^)()) scanForPeripherals; + +/** +连接Peripherals + */ +- (BabyBluetooth *(^)()) connectToPeripherals; + +/** +发现Services + */ +- (BabyBluetooth *(^)()) discoverServices; + +/** +获取Characteristics + */ +- (BabyBluetooth *(^)()) discoverCharacteristics; + +/** +更新Characteristics的值 + */ +- (BabyBluetooth *(^)()) readValueForCharacteristic; + +/** +获取Characteristics的名称 + */ +- (BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic; + +/** +获取Descriptors的值 + */ +- (BabyBluetooth *(^)()) readValueForDescriptors; + +/** +开始执行 + */ +- (BabyBluetooth *(^)()) begin; + +/** +sec秒后停止 + */ +- (BabyBluetooth *(^)(int sec)) stop; + +/** +持有对象 + */ +- (BabyBluetooth *(^)(id obj)) having; + +/** +切换委托的频道 + */ +- (BabyBluetooth *(^)(NSString *channel)) channel; + +/** +谓词,返回self + */ +- (BabyBluetooth *) and; +/** +谓词,返回self + */ +- (BabyBluetooth *) then; +/** +谓词,返回self + */ +- (BabyBluetooth *) with; + +/** + * enjoy 祝你使用愉快, + * + * 说明:enjoy是蓝牙全套串行方法的简写(发现服务,发现特征,读取特征,读取特征描述),前面可以必须有scanForPeripherals或having方法,channel可以选择添加。 + + enjoy() 等同于 connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); + + 它可以让你少敲很多代码 + + ## 例子: + - 扫描后来个全套(发现服务,发现特征,读取特征,读取特征描述) + + ` baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics() + .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); + ` + + - 直接使用已有的外设连接后全套(发现服务,发现特征,读取特征,读取特征描述) + + ` baby.having(self.peripheral).connectToPeripherals().discoverServices().discoverCharacteristics() + .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); + ` + enjoy后面也可以加stop()方法 + + */ + +- (BabyBluetooth *(^)()) enjoy; + +#pragma mark - 工具方法 + +/** + * 单例构造方法 + * @return BabyBluetooth共享实例 + */ ++ (instancetype)shareBabyBluetooth; + + +/** +断开连接 + */ +- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral; + +/** +断开所有连接 + */ +- (void)cancelAllPeripheralsConnection; + +/** +停止扫描 + */ +- (void)cancelScan; + +/** +更新Characteristics的值 + */ +- (BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails; + +/** +设置characteristic的notify + */ +- (void)notify:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic block:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block; -//取消characteristic的notify --(void)cancelNotify:(CBPeripheral *)peripheral + +/** +取消characteristic的notify + */ +- (void)cancelNotify:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic; -//获取当前连接的peripherals --(NSArray *)findConnectedPeripherals; +/** +获取当前连接的peripherals + */ +- (NSArray *)findConnectedPeripherals; + +/** +获取当前连接的peripheral + */ +- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName; + +/** +获取当前corebluetooth的centralManager对象 + */ +- (CBCentralManager *)centralManager; -//获取当前连接的peripheral --(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName; +/** + 添加断开自动重连的外设 + */ +- (void)AutoReconnect:(CBPeripheral *)peripheral; -//获取当前corebluetooth的centralManager对象 --(CBCentralManager *)centralManager; +/** + 删除断开自动重连的外设 + */ +- (void)AutoReconnectCancel:(CBPeripheral *)peripheral; /** - * 单例构造方法 - * @return BabyBluetooth共享实例 + 根据外设UUID对应的string获取已配对的外设 + + 通过方法获取外设后可以直接连接外设,跳过扫描过程 + */ +- (CBPeripheral *)retrievePeripheralWithUUIDString:(NSString *)UUIDString; + + +#pragma mark - peripheral model + +//进入外设模式 +- (BabyPeripheralManager *(^)()) bePeripheral; +- (BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName; + +@property (nonatomic, readonly) CBPeripheralManager *peripheralManager; + +//peripheral model block + +/** + PeripheralManager did update state block + */ +- (void)peripheralModelBlockOnPeripheralManagerDidUpdateState:(void(^)(CBPeripheralManager *peripheral))block; +/** + PeripheralManager did add service block + */ +- (void)peripheralModelBlockOnDidAddService:(void(^)(CBPeripheralManager *peripheral,CBService *service,NSError *error))block; +/** + PeripheralManager did start advertising block + */ +- (void)peripheralModelBlockOnDidStartAdvertising:(void(^)(CBPeripheralManager *peripheral,NSError *error))block; +/** + peripheralManager did receive read request block + */ +- (void)peripheralModelBlockOnDidReceiveReadRequest:(void(^)(CBPeripheralManager *peripheral,CBATTRequest *request))block; +/** + peripheralManager did receive write request block */ -+(instancetype)shareBabyBluetooth; +- (void)peripheralModelBlockOnDidReceiveWriteRequests:(void(^)(CBPeripheralManager *peripheral,NSArray *requests))block; +/** + peripheralManager is ready to update subscribers + */ +- (void)peripheralModelBlockOnIsReadyToUpdateSubscribers:(void(^)(CBPeripheralManager *peripheral))block; +/** + peripheralManager did subscribe to characteristic block + */ +- (void)peripheralModelBlockOnDidSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block; +/** + peripheralManager did unsubscribe to characteristic block +*/ +- (void)peripheralModelBlockOnDidUnSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block; @end diff --git a/Classes/objc/BabyBluetooth.m b/Classes/objc/BabyBluetooth.m index 0f58561..81323c4 100644 --- a/Classes/objc/BabyBluetooth.m +++ b/Classes/objc/BabyBluetooth.m @@ -13,13 +13,14 @@ @implementation BabyBluetooth{ - Babysister *babysister; + BabyCentralManager *babyCentralManager; + BabyPeripheralManager *babyPeripheralManager; BabySpeaker *babySpeaker; int CENTRAL_MANAGER_INIT_WAIT_TIMES; NSTimer *timerForStop; } //单例模式 -+(instancetype)shareBabyBluetooth{ ++ (instancetype)shareBabyBluetooth { static BabyBluetooth *share = nil; static dispatch_once_t oneToken; dispatch_once(&oneToken, ^{ @@ -27,99 +28,102 @@ +(instancetype)shareBabyBluetooth{ }); return share; } --(instancetype)init{ + +- (instancetype)init { self = [super init]; if (self) { //初始化对象 - babysister = [[Babysister alloc]init]; + babyCentralManager = [[BabyCentralManager alloc]init]; babySpeaker = [[BabySpeaker alloc]init]; - babysister->babySpeaker = babySpeaker; + babyCentralManager->babySpeaker = babySpeaker; + babyPeripheralManager = [[BabyPeripheralManager alloc]init]; + babyPeripheralManager->babySpeaker = babySpeaker; } return self; } -#pragma mark -babybluetooth的委托 +#pragma mark - babybluetooth的委托 /* 默认频道的委托 */ //设备状态改变的委托 --(void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block{ +- (void)setBlockOnCentralManagerDidUpdateState:(void (^)(CBCentralManager *central))block { [[babySpeaker callback]setBlockOnCentralManagerDidUpdateState:block]; } //找到Peripherals的委托 --(void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block{ +- (void)setBlockOnDiscoverToPeripherals:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block{ [[babySpeaker callback]setBlockOnDiscoverPeripherals:block]; } //连接Peripherals成功的委托 --(void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block{ +- (void)setBlockOnConnected:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block { [[babySpeaker callback]setBlockOnConnectedPeripheral:block]; } //连接Peripherals失败的委托 --(void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnFailToConnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callback]setBlockOnFailToConnect:block]; } //断开Peripherals的连接 --(void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnDisconnect:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callback]setBlockOnDisconnect:block]; } //设置查找服务回叫 --(void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnDiscoverServices:(void (^)(CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callback]setBlockOnDiscoverServices:block]; } //设置查找到Characteristics的block --(void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block{ +- (void)setBlockOnDiscoverCharacteristics:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block { [[babySpeaker callback]setBlockOnDiscoverCharacteristics:block]; } //设置获取到最新Characteristics值的block --(void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnReadValueForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callback]setBlockOnReadValueForCharacteristic:block]; } //设置查找到Characteristics描述的block --(void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block{ +- (void)setBlockOnDiscoverDescriptorsForCharacteristic:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block { [[babySpeaker callback]setBlockOnDiscoverDescriptorsForCharacteristic:block]; } //设置读取到Characteristics描述的值的block --(void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block{ +- (void)setBlockOnReadValueForDescriptors:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block { [[babySpeaker callback]setBlockOnReadValueForDescriptors:block]; } //写Characteristic成功后的block --(void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnDidWriteValueForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callback]setBlockOnDidWriteValueForCharacteristic:block]; } //写descriptor成功后的block --(void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block{ +- (void)setBlockOnDidWriteValueForDescriptor:(void (^)(CBDescriptor *descriptor,NSError *error))block { [[babySpeaker callback]setBlockOnDidWriteValueForDescriptor:block]; } //characteristic订阅状态改变的block --(void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnDidUpdateNotificationStateForCharacteristic:(void (^)(CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callback]setBlockOnDidUpdateNotificationStateForCharacteristic:block]; } //读取RSSI的委托 --(void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block{ +- (void)setBlockOnDidReadRSSI:(void (^)(NSNumber *RSSI,NSError *error))block { [[babySpeaker callback]setBlockOnDidReadRSSI:block]; } //discoverIncludedServices的回调,暂时在babybluetooth中无作用 --(void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block{ +- (void)setBlockOnDidDiscoverIncludedServicesForService:(void (^)(CBService *service,NSError *error))block { [[babySpeaker callback]setBlockOnDidDiscoverIncludedServicesForService:block]; } //外设更新名字后的block --(void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block{ +- (void)setBlockOnDidUpdateName:(void (^)(CBPeripheral *peripheral))block { [[babySpeaker callback]setBlockOnDidUpdateName:block]; } //外设更新服务后的block --(void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block{ +- (void)setBlockOnDidModifyServices:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block { [[babySpeaker callback]setBlockOnDidModifyServices:block]; } //设置蓝牙使用的参数参数 --(void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions +- (void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithServices:(NSArray *)discoverWithServices - discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics{ + discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics { BabyOptions *option = [[BabyOptions alloc]initWithscanForPeripheralsWithOptions:scanForPeripheralsWithOptions connectPeripheralWithOptions:connectPeripheralWithOptions scanForPeripheralsWithServices:scanForPeripheralsWithServices discoverWithServices:discoverWithServices discoverWithCharacteristics:discoverWithCharacteristics]; [[babySpeaker callback]setBabyOptions:option]; } @@ -128,241 +132,245 @@ -(void)setBabyOptionsWithScanForPeripheralsWithOptions:(NSDictionary *) scanForP channel的委托 */ //设备状态改变的委托 --(void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel - block:(void (^)(CBCentralManager *central))block{ +- (void)setBlockOnCentralManagerDidUpdateStateAtChannel:(NSString *)channel + block:(void (^)(CBCentralManager *central))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnCentralManagerDidUpdateState:block]; } //找到Peripherals的委托 --(void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel - block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block{ +- (void)setBlockOnDiscoverToPeripheralsAtChannel:(NSString *)channel + block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnDiscoverPeripherals:block]; } //连接Peripherals成功的委托 --(void)setBlockOnConnectedAtChannel:(NSString *)channel - block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block{ +- (void)setBlockOnConnectedAtChannel:(NSString *)channel + block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnConnectedPeripheral:block]; } //连接Peripherals失败的委托 --(void)setBlockOnFailToConnectAtChannel:(NSString *)channel - block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnFailToConnectAtChannel:(NSString *)channel + block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnFailToConnect:block]; } //断开Peripherals的连接 --(void)setBlockOnDisconnectAtChannel:(NSString *)channel - block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnDisconnectAtChannel:(NSString *)channel + block:(void (^)(CBCentralManager *central,CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnDisconnect:block]; } //设置查找服务回叫 --(void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,NSError *error))block{ +- (void)setBlockOnDiscoverServicesAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnDiscoverServices:block]; } //设置查找到Characteristics的block --(void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block{ +- (void)setBlockOnDiscoverCharacteristicsAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,CBService *service,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnDiscoverCharacteristics:block]; } //设置获取到最新Characteristics值的block --(void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnReadValueForCharacteristicAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnReadValueForCharacteristic:block]; } //设置查找到Characteristics描述的block --(void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block{ +- (void)setBlockOnDiscoverDescriptorsForCharacteristicAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,CBCharacteristic *service,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnDiscoverDescriptorsForCharacteristic:block]; } //设置读取到Characteristics描述的值的block --(void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptorNSError,NSError *error))block{ +- (void)setBlockOnReadValueForDescriptorsAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,CBDescriptor *descriptor,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnReadValueForDescriptors:block]; } //写Characteristic成功后的block --(void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel - block:(void (^)(CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnDidWriteValueForCharacteristicAtChannel:(NSString *)channel + block:(void (^)(CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidWriteValueForCharacteristic:block]; } //写descriptor成功后的block --(void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel - block:(void (^)(CBDescriptor *descriptor,NSError *error))block{ +- (void)setBlockOnDidWriteValueForDescriptorAtChannel:(NSString *)channel + block:(void (^)(CBDescriptor *descriptor,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidWriteValueForDescriptor:block]; } //characteristic订阅状态改变的block --(void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel - block:(void (^)(CBCharacteristic *characteristic,NSError *error))block{ +- (void)setBlockOnDidUpdateNotificationStateForCharacteristicAtChannel:(NSString *)channel + block:(void (^)(CBCharacteristic *characteristic,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidUpdateNotificationStateForCharacteristic:block]; } //读取RSSI的委托 --(void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel - block:(void (^)(NSNumber *RSSI,NSError *error))block{ +- (void)setBlockOnDidReadRSSIAtChannel:(NSString *)channel + block:(void (^)(NSNumber *RSSI,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidReadRSSI:block]; } //discoverIncludedServices的回调,暂时在babybluetooth中无作用 --(void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel - block:(void (^)(CBService *service,NSError *error))block{ +- (void)setBlockOnDidDiscoverIncludedServicesForServiceAtChannel:(NSString *)channel + block:(void (^)(CBService *service,NSError *error))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidDiscoverIncludedServicesForService:block]; } //外设更新名字后的block --(void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral))block{ +- (void)setBlockOnDidUpdateNameAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidUpdateName:block]; } //外设更新服务后的block --(void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel - block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block{ +- (void)setBlockOnDidModifyServicesAtChannel:(NSString *)channel + block:(void (^)(CBPeripheral *peripheral,NSArray *invalidatedServices))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBlockOnDidModifyServices:block]; } //设置蓝牙运行时的参数 --(void)setBabyOptionsAtChannel:(NSString *)channel +- (void)setBabyOptionsAtChannel:(NSString *)channel scanForPeripheralsWithOptions:(NSDictionary *) scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *) connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithServices:(NSArray *)discoverWithServices - discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics{ + discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics { BabyOptions *option = [[BabyOptions alloc]initWithscanForPeripheralsWithOptions:scanForPeripheralsWithOptions connectPeripheralWithOptions:connectPeripheralWithOptions scanForPeripheralsWithServices:scanForPeripheralsWithServices discoverWithServices:discoverWithServices discoverWithCharacteristics:discoverWithCharacteristics]; [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES]setBabyOptions:option]; } -#pragma mark -babybluetooth filter委托 +#pragma mark - babybluetooth filter委托 //设置查找Peripherals的规则 --(void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName))filter{ +- (void)setFilterOnDiscoverPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter { [[babySpeaker callback]setFilterOnDiscoverPeripherals:filter]; } //设置连接Peripherals的规则 --(void)setFilterOnConnetToPeripherals:(BOOL (^)(NSString *peripheralName))filter{ - [[babySpeaker callback]setFilterOnConnetToPeripherals:filter]; +- (void)setFilterOnConnectToPeripherals:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter { + [[babySpeaker callback]setFilterOnconnectToPeripherals:filter]; } //设置查找Peripherals的规则 --(void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel - filter:(BOOL (^)(NSString *peripheralName))filter{ +- (void)setFilterOnDiscoverPeripheralsAtChannel:(NSString *)channel + filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setFilterOnDiscoverPeripherals:filter]; } //设置连接Peripherals的规则 --(void)setFilterOnConnetToPeripheralsAtChannel:(NSString *)channel - filter:(BOOL (^)(NSString *peripheralName))filter{ - [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setFilterOnConnetToPeripherals:filter]; +- (void)setFilterOnConnectToPeripheralsAtChannel:(NSString *)channel + filter:(BOOL (^)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI))filter { + [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setFilterOnconnectToPeripherals:filter]; } -#pragma mark -babybluetooth Special +#pragma mark - babybluetooth Special //babyBluettooth cancelScan方法调用后的回调 --(void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block{ +- (void)setBlockOnCancelScanBlock:(void(^)(CBCentralManager *centralManager))block { [[babySpeaker callback]setBlockOnCancelScan:block]; } //babyBluettooth cancelAllPeripheralsConnectionBlock 方法调用后的回调 --(void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block{ +- (void)setBlockOnCancelAllPeripheralsConnectionBlock:(void(^)(CBCentralManager *centralManager))block{ [[babySpeaker callback]setBlockOnCancelAllPeripheralsConnection:block]; } //babyBluettooth cancelScan方法调用后的回调 --(void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel - block:(void(^)(CBCentralManager *centralManager))block{ +- (void)setBlockOnCancelScanBlockAtChannel:(NSString *)channel + block:(void(^)(CBCentralManager *centralManager))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnCancelScan:block]; } //babyBluettooth cancelAllPeripheralsConnectionBlock 方法调用后的回调 --(void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel - block:(void(^)(CBCentralManager *centralManager))block{ +- (void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channel + block:(void(^)(CBCentralManager *centralManager))block { [[babySpeaker callbackOnChnnel:channel createWhenNotExist:YES] setBlockOnCancelAllPeripheralsConnection:block]; } -#pragma mark -链式函数 +#pragma mark - 链式函数 //查找Peripherals --(BabyBluetooth *(^)()) scanForPeripherals{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needScanForPeripherals"]; +- (BabyBluetooth *(^)()) scanForPeripherals { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needScanForPeripherals"]; return self; }; } //连接Peripherals --(BabyBluetooth *(^)()) connectToPeripherals{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needConnectPeripheral"]; +- (BabyBluetooth *(^)()) connectToPeripherals { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needConnectPeripheral"]; return self; }; } //发现Services --(BabyBluetooth *(^)()) discoverServices{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needDiscoverServices"]; +- (BabyBluetooth *(^)()) discoverServices { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needDiscoverServices"]; return self; }; } //获取Characteristics --(BabyBluetooth *(^)()) discoverCharacteristics{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needDiscoverCharacteristics"]; +- (BabyBluetooth *(^)()) discoverCharacteristics { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needDiscoverCharacteristics"]; return self; }; } + //更新Characteristics的值 --(BabyBluetooth *(^)()) readValueForCharacteristic{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needReadValueForCharacteristic"]; +- (BabyBluetooth *(^)()) readValueForCharacteristic { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needReadValueForCharacteristic"]; return self; }; } + //设置查找到Descriptors名称的block --(BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needDiscoverDescriptorsForCharacteristic"]; +- (BabyBluetooth *(^)()) discoverDescriptorsForCharacteristic { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needDiscoverDescriptorsForCharacteristic"]; return self; }; } + //设置读取到Descriptors值的block --(BabyBluetooth *(^)()) readValueForDescriptors{ - return ^BabyBluetooth *(){ - [babysister->pocket setObject:@"YES" forKey:@"needDiscoverDescriptorsForCharacteristic"]; +- (BabyBluetooth *(^)()) readValueForDescriptors { + return ^BabyBluetooth *() { + [babyCentralManager->pocket setObject:@"YES" forKey:@"needReadValueForDescriptors"]; return self; }; } + //开始并执行 --(BabyBluetooth *(^)()) begin{ - return ^BabyBluetooth *(){ +- (BabyBluetooth *(^)()) begin { + return ^BabyBluetooth *() { //取消未执行的stop定时任务 [timerForStop invalidate]; dispatch_sync(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self resetSeriseParmeter]; //处理链式函数缓存的数据 - if ([[babysister->pocket valueForKey:@"needScanForPeripherals"] isEqualToString:@"YES"]) { - babysister->needScanForPeripherals = YES; + if ([[babyCentralManager->pocket valueForKey:@"needScanForPeripherals"] isEqualToString:@"YES"]) { + babyCentralManager->needScanForPeripherals = YES; } - if ([[babysister->pocket valueForKey:@"needConnectPeripheral"] isEqualToString:@"YES"]) { - babysister->needConnectPeripheral = YES; + if ([[babyCentralManager->pocket valueForKey:@"needConnectPeripheral"] isEqualToString:@"YES"]) { + babyCentralManager->needConnectPeripheral = YES; } - if ([[babysister->pocket valueForKey:@"needDiscoverServices"] isEqualToString:@"YES"]) { - babysister->needDiscoverServices = YES; + if ([[babyCentralManager->pocket valueForKey:@"needDiscoverServices"] isEqualToString:@"YES"]) { + babyCentralManager->needDiscoverServices = YES; } - if ([[babysister->pocket valueForKey:@"needDiscoverCharacteristics"] isEqualToString:@"YES"]) { - babysister->needDiscoverCharacteristics = YES; + if ([[babyCentralManager->pocket valueForKey:@"needDiscoverCharacteristics"] isEqualToString:@"YES"]) { + babyCentralManager->needDiscoverCharacteristics = YES; } - if ([[babysister->pocket valueForKey:@"needReadValueForCharacteristic"] isEqualToString:@"YES"]) { - babysister->needReadValueForCharacteristic = YES; + if ([[babyCentralManager->pocket valueForKey:@"needReadValueForCharacteristic"] isEqualToString:@"YES"]) { + babyCentralManager->needReadValueForCharacteristic = YES; } - if ([[babysister->pocket valueForKey:@"needDiscoverDescriptorsForCharacteristic"] isEqualToString:@"YES"]) { - babysister->needDiscoverDescriptorsForCharacteristic = YES; + if ([[babyCentralManager->pocket valueForKey:@"needDiscoverDescriptorsForCharacteristic"] isEqualToString:@"YES"]) { + babyCentralManager->needDiscoverDescriptorsForCharacteristic = YES; } - if ([[babysister->pocket valueForKey:@"needReadValueForDescriptors"] isEqualToString:@"YES"]) { - babysister->needReadValueForDescriptors = YES; + if ([[babyCentralManager->pocket valueForKey:@"needReadValueForDescriptors"] isEqualToString:@"YES"]) { + babyCentralManager->needReadValueForDescriptors = YES; } //调整委托方法的channel,如果没设置默认为缺省频道 - NSString *channel = [babysister->pocket valueForKey:@"channel"]; + NSString *channel = [babyCentralManager->pocket valueForKey:@"channel"]; [babySpeaker switchChannel:channel]; //缓存的peripheral - CBPeripheral *cachedPeripheral = [babysister->pocket valueForKey:NSStringFromClass([CBPeripheral class])]; + CBPeripheral *cachedPeripheral = [babyCentralManager->pocket valueForKey:NSStringFromClass([CBPeripheral class])]; //校验series合法性 [self validateProcess]; //清空pocjet - babysister->pocket = [[NSMutableDictionary alloc]init]; + babyCentralManager->pocket = [[NSMutableDictionary alloc]init]; //开始扫描或连接设备 [self start:cachedPeripheral]; }); @@ -372,41 +380,41 @@ -(void)setBlockOnCancelAllPeripheralsConnectionBlockAtChannel:(NSString *)channe //私有方法,扫描或连接设备 --(void)start:(CBPeripheral *)cachedPeripheral{ - if (babysister->bleManager.state == CBCentralManagerStatePoweredOn) { +- (void)start:(CBPeripheral *)cachedPeripheral { + if (babyCentralManager->centralManager.state == CBCentralManagerStatePoweredOn) { CENTRAL_MANAGER_INIT_WAIT_TIMES = 0; //扫描后连接 - if (babysister->needScanForPeripherals) { + if (babyCentralManager->needScanForPeripherals) { //开始扫描peripherals - [babysister scanPeripherals]; + [babyCentralManager scanPeripherals]; } //直接连接 - else{ + else { if (cachedPeripheral) { - [babysister connectToPeripheral:cachedPeripheral]; + [babyCentralManager connectToPeripheral:cachedPeripheral]; } } return; } //尝试重新等待CBCentralManager打开 CENTRAL_MANAGER_INIT_WAIT_TIMES ++; - if (CENTRAL_MANAGER_INIT_WAIT_TIMES >=5 ) { - NSLog(@">>> 第%d次等待CBCentralManager 打开任然失败,请检查你蓝牙使用权限或检查设备问题。",CENTRAL_MANAGER_INIT_WAIT_TIMES); + if (CENTRAL_MANAGER_INIT_WAIT_TIMES >= KBABY_CENTRAL_MANAGER_INIT_WAIT_TIMES ) { + BabyLog(@">>> 第%d次等待CBCentralManager 打开任然失败,请检查你蓝牙使用权限或检查设备问题。",CENTRAL_MANAGER_INIT_WAIT_TIMES); return; //[NSException raise:@"CBCentralManager打开异常" format:@"尝试等待打开CBCentralManager5次,但任未能打开"]; } - dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC); + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, KBABY_CENTRAL_MANAGER_INIT_WAIT_SECOND * NSEC_PER_SEC); dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ [self start:cachedPeripheral]; }); - NSLog(@">>> 第%d次等待CBCentralManager打开",CENTRAL_MANAGER_INIT_WAIT_TIMES); + BabyLog(@">>> 第%d次等待CBCentralManager打开",CENTRAL_MANAGER_INIT_WAIT_TIMES); } //sec秒后停止 --(BabyBluetooth *(^)(int sec)) stop { +- (BabyBluetooth *(^)(int sec)) stop { - return ^BabyBluetooth *(int sec){ - NSLog(@">>> stop in %d sec",sec); + return ^BabyBluetooth *(int sec) { + BabyLog(@">>> stop in %d sec",sec); //听见定时器执行babyStop timerForStop = [NSTimer timerWithTimeInterval:sec target:self selector:@selector(babyStop) userInfo:nil repeats:NO]; @@ -418,83 +426,81 @@ -(void)start:(CBPeripheral *)cachedPeripheral{ } //私有方法,停止扫描和断开连接,清空pocket --(void)babyStop{ - NSLog(@">>>did stop"); +- (void)babyStop { + BabyLog(@">>>did stop"); [timerForStop invalidate]; [self resetSeriseParmeter]; - babysister->pocket = [[NSMutableDictionary alloc]init]; + babyCentralManager->pocket = [[NSMutableDictionary alloc]init]; //停止扫描,断开连接 - [babysister cancelScan]; - [babysister cancelAllPeripheralsConnection]; + [babyCentralManager cancelScan]; + [babyCentralManager cancelAllPeripheralsConnection]; } //重置串行方法参数 --(void)resetSeriseParmeter{ - babysister->needScanForPeripherals = NO; - babysister->needConnectPeripheral = NO; - babysister->needDiscoverServices = NO; - babysister->needDiscoverCharacteristics = NO; - babysister->needReadValueForCharacteristic = NO; - babysister->needDiscoverDescriptorsForCharacteristic = NO; - babysister->needReadValueForDescriptors = NO; +- (void)resetSeriseParmeter { + babyCentralManager->needScanForPeripherals = NO; + babyCentralManager->needConnectPeripheral = NO; + babyCentralManager->needDiscoverServices = NO; + babyCentralManager->needDiscoverCharacteristics = NO; + babyCentralManager->needReadValueForCharacteristic = NO; + babyCentralManager->needDiscoverDescriptorsForCharacteristic = NO; + babyCentralManager->needReadValueForDescriptors = NO; } //持有对象 --(BabyBluetooth *(^)(id obj)) having{ - return ^(id obj){ - [babysister->pocket setObject:obj forKey:NSStringFromClass([obj class])]; +- (BabyBluetooth *(^)(id obj)) having { + return ^(id obj) { + [babyCentralManager->pocket setObject:obj forKey:NSStringFromClass([obj class])]; return self; }; } //切换委托频道 --(BabyBluetooth *(^)(NSString *channel)) channel{ - return ^BabyBluetooth *(NSString *channel){ +- (BabyBluetooth *(^)(NSString *channel)) channel { + return ^BabyBluetooth *(NSString *channel) { //先缓存数据,到begin方法统一处理 - [babysister->pocket setValue:channel forKey:@"channel"]; + [babyCentralManager->pocket setValue:channel forKey:@"channel"]; return self; }; } - - --(void)validateProcess{ +- (void)validateProcess { NSMutableArray *faildReason = [[NSMutableArray alloc]init]; //规则:不执行discoverDescriptorsForCharacteristic()时,不能执行readValueForDescriptors() - if (!babysister->needDiscoverDescriptorsForCharacteristic) { - if (babysister->needReadValueForDescriptors) { + if (!babyCentralManager->needDiscoverDescriptorsForCharacteristic) { + if (babyCentralManager->needReadValueForDescriptors) { [faildReason addObject:@"未执行discoverDescriptorsForCharacteristic()不能执行readValueForDescriptors()"]; } } //规则:不执行discoverCharacteristics()时,不能执行readValueForCharacteristic()或者是discoverDescriptorsForCharacteristic() - if (!babysister->needDiscoverCharacteristics) { - if (babysister->needReadValueForCharacteristic||babysister->needDiscoverDescriptorsForCharacteristic) { + if (!babyCentralManager->needDiscoverCharacteristics) { + if (babyCentralManager->needReadValueForCharacteristic||babyCentralManager->needDiscoverDescriptorsForCharacteristic) { [faildReason addObject:@"未执行discoverCharacteristics()不能执行readValueForCharacteristic()或discoverDescriptorsForCharacteristic()"]; } } //规则: 不执行discoverServices()不能执行discoverCharacteristics()、readValueForCharacteristic()、discoverDescriptorsForCharacteristic()、readValueForDescriptors() - if (!babysister->needDiscoverServices) { - if (babysister->needDiscoverCharacteristics||babysister->needDiscoverDescriptorsForCharacteristic ||babysister->needReadValueForCharacteristic ||babysister->needReadValueForDescriptors) { + if (!babyCentralManager->needDiscoverServices) { + if (babyCentralManager->needDiscoverCharacteristics||babyCentralManager->needDiscoverDescriptorsForCharacteristic ||babyCentralManager->needReadValueForCharacteristic ||babyCentralManager->needReadValueForDescriptors) { [faildReason addObject:@"未执行discoverServices()不能执行discoverCharacteristics()、readValueForCharacteristic()、discoverDescriptorsForCharacteristic()、readValueForDescriptors()"]; } } //规则:不执行connectToPeripherals()时,不能执行discoverServices() - if(!babysister->needConnectPeripheral){ - if (babysister->needDiscoverServices) { + if(!babyCentralManager->needConnectPeripheral) { + if (babyCentralManager->needDiscoverServices) { [faildReason addObject:@"未执行connectToPeripherals()不能执行discoverServices()"]; } } //规则:不执行needScanForPeripherals(),那么执行connectToPeripheral()方法时必须用having(peripheral)传入peripheral实例 - if (!babysister->needScanForPeripherals) { - CBPeripheral *peripheral = [babysister->pocket valueForKey:NSStringFromClass([CBPeripheral class])]; + if (!babyCentralManager->needScanForPeripherals) { + CBPeripheral *peripheral = [babyCentralManager->pocket valueForKey:NSStringFromClass([CBPeripheral class])]; if (!peripheral) { [faildReason addObject:@"若不执行scanForPeripherals()方法,则必须执行connectToPeripheral方法并且需要传入参数(CBPeripheral *)peripheral"]; } @@ -508,78 +514,157 @@ -(void)validateProcess{ } --(BabyBluetooth *) and{ +- (BabyBluetooth *) and { return self; } --(BabyBluetooth *) then{ +- (BabyBluetooth *) then { return self; } --(BabyBluetooth *) with{ +- (BabyBluetooth *) with { return self; } -#pragma mark -工具方法 +- (BabyBluetooth *(^)()) enjoy { + return ^BabyBluetooth *(int sec) { + self.connectToPeripherals().discoverServices().discoverCharacteristics() + .readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin(); + return self; + }; +} + +#pragma mark - 工具方法 //断开连接 --(void)cancelPeripheralConnection:(CBPeripheral *)peripheral{ - [babysister cancelPeripheralConnection:peripheral]; +- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral { + [babyCentralManager cancelPeripheralConnection:peripheral]; } //断开所有连接 --(void)cancelAllPeripheralsConnection{ - [babysister cancelAllPeripheralsConnection]; +- (void)cancelAllPeripheralsConnection { + [babyCentralManager cancelAllPeripheralsConnection]; } //停止扫描 --(void)cancelScan{ - [babysister cancelScan]; +- (void)cancelScan{ + [babyCentralManager cancelScan]; } //读取Characteristic的详细信息 --(BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails{ +- (BabyBluetooth *(^)(CBPeripheral *peripheral,CBCharacteristic *characteristic)) characteristicDetails { //切换频道 - [babySpeaker switchChannel:[babysister->pocket valueForKey:@"channel"]]; - babysister->pocket = [[NSMutableDictionary alloc]init]; + [babySpeaker switchChannel:[babyCentralManager->pocket valueForKey:@"channel"]]; + babyCentralManager->pocket = [[NSMutableDictionary alloc]init]; - return ^(CBPeripheral *peripheral,CBCharacteristic *characteristic){ + return ^(CBPeripheral *peripheral,CBCharacteristic *characteristic) { //判断连接状态 if (peripheral.state == CBPeripheralStateConnected) { - self->babysister->oneReadValueForDescriptors = YES; + self->babyCentralManager->oneReadValueForDescriptors = YES; [peripheral readValueForCharacteristic:characteristic]; [peripheral discoverDescriptorsForCharacteristic:characteristic]; - }else{ - NSLog(@"!!!设备当前处于非连接状态"); + } + else { + BabyLog(@"!!!设备当前处于非连接状态"); } return self; }; } --(void)notify:(CBPeripheral *)peripheral +- (void)notify:(CBPeripheral *)peripheral characteristic:(CBCharacteristic *)characteristic - block:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block{ + block:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block { //设置通知 [peripheral setNotifyValue:YES forCharacteristic:characteristic]; [babySpeaker addNotifyCallback:characteristic withBlock:block]; } --(void)cancelNotify:(CBPeripheral *)peripheral - characteristic:(CBCharacteristic *)characteristic{ + +- (void)cancelNotify:(CBPeripheral *)peripheral + characteristic:(CBCharacteristic *)characteristic { [peripheral setNotifyValue:NO forCharacteristic:characteristic]; [babySpeaker removeNotifyCallback:characteristic]; } - //获取当前连接的peripherals --(NSArray *)findConnectedPeripherals{ - return [babysister findConnectedPeripherals]; +- (NSArray *)findConnectedPeripherals { + return [babyCentralManager findConnectedPeripherals]; } //获取当前连接的peripheral --(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName{ - return [babysister findConnectedPeripheral:peripheralName]; +- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName { + return [babyCentralManager findConnectedPeripheral:peripheralName]; } //获取当前corebluetooth的centralManager对象 --(CBCentralManager *)centralManager{ - return babysister->bleManager; +- (CBCentralManager *)centralManager { + return babyCentralManager->centralManager; +} + +/** + 添加断开自动重连的外设 + */ +- (void)AutoReconnect:(CBPeripheral *)peripheral{ + [babyCentralManager sometimes_ever:peripheral]; +} + +/** + 删除断开自动重连的外设 + */ +- (void)AutoReconnectCancel:(CBPeripheral *)peripheral{ + [babyCentralManager sometimes_never:peripheral]; +} + +- (CBPeripheral *)retrievePeripheralWithUUIDString:(NSString *)UUIDString { + CBPeripheral *p = nil; + @try { + NSUUID *uuid = [[NSUUID alloc]initWithUUIDString:UUIDString]; + p = [self.centralManager retrievePeripheralsWithIdentifiers:@[uuid]][0]; + } @catch (NSException *exception) { + BabyLog(@">>> retrievePeripheralWithUUIDString error:%@",exception) + } @finally { + } + return p; +} + +#pragma mark - peripheral model + +//进入外设模式 + +- (CBPeripheralManager *)peripheralManager { + return babyPeripheralManager.peripheralManager; } +- (BabyPeripheralManager *(^)()) bePeripheral { + return ^BabyPeripheralManager* () { + return babyPeripheralManager; + }; +} +- (BabyPeripheralManager *(^)(NSString *localName)) bePeripheralWithName { + return ^BabyPeripheralManager* (NSString *localName) { + babyPeripheralManager.localName = localName; + return babyPeripheralManager; + }; +} + +- (void)peripheralModelBlockOnPeripheralManagerDidUpdateState:(void(^)(CBPeripheralManager *peripheral))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidUpdateState:block]; +} +- (void)peripheralModelBlockOnDidAddService:(void(^)(CBPeripheralManager *peripheral,CBService *service,NSError *error))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidAddService:block]; +} +- (void)peripheralModelBlockOnDidStartAdvertising:(void(^)(CBPeripheralManager *peripheral,NSError *error))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidStartAdvertising:block]; +} +- (void)peripheralModelBlockOnDidReceiveReadRequest:(void(^)(CBPeripheralManager *peripheral,CBATTRequest *request))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidReceiveReadRequest:block]; +} +- (void)peripheralModelBlockOnDidReceiveWriteRequests:(void(^)(CBPeripheralManager *peripheral,NSArray *requests))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidReceiveWriteRequests:block]; +} +- (void)peripheralModelBlockOnIsReadyToUpdateSubscribers:(void(^)(CBPeripheralManager *peripheral))block { + [[babySpeaker callback]setBlockOnPeripheralModelIsReadyToUpdateSubscribers:block]; +} +- (void)peripheralModelBlockOnDidSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidSubscribeToCharacteristic:block]; +} +- (void)peripheralModelBlockOnDidUnSubscribeToCharacteristic:(void(^)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic))block { + [[babySpeaker callback]setBlockOnPeripheralModelDidUnSubscribeToCharacteristic:block]; +} @end diff --git a/Classes/objc/BabyCallback.h b/Classes/objc/BabyCallback.h index 76ec0e3..696cbb4 100644 --- a/Classes/objc/BabyCallback.h +++ b/Classes/objc/BabyCallback.h @@ -2,6 +2,9 @@ BabyBluetooth 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 https://github.com/coolnameismy/BabyBluetooth + + @brief babybluetooth 的block定义和储存 + */ // Created by 刘彦玮 on 15/9/2. @@ -13,7 +16,7 @@ //设备状态改变的委托 -typedef void (^BBcentralManagerDidUpdateStateBlock)(CBCentralManager *central); +typedef void (^BBCentralManagerDidUpdateStateBlock)(CBCentralManager *central); //找到设备的委托 typedef void (^BBDiscoverPeripheralsBlock)(CBCentralManager *central,CBPeripheral *peripheral,NSDictionary *advertisementData, NSNumber *RSSI); //连接设备成功的block @@ -39,75 +42,98 @@ typedef void (^BBCancelScanBlock)(CBCentralManager *centralManager); typedef void (^BBCancelAllPeripheralsConnectionBlock)(CBCentralManager *centralManager); -typedef void (^BBDidWriteValueForCharacteristic)(CBCharacteristic *characteristic,NSError *error); +typedef void (^BBDidWriteValueForCharacteristicBlock)(CBCharacteristic *characteristic,NSError *error); -typedef void (^BBDidWriteValueForDescriptor)(CBDescriptor *descriptor,NSError *error); +typedef void (^BBDidWriteValueForDescriptorBlock)(CBDescriptor *descriptor,NSError *error); -typedef void (^BBDidUpdateNotificationStateForCharacteristic)(CBCharacteristic *characteristic,NSError *error); +typedef void (^BBDidUpdateNotificationStateForCharacteristicBlock)(CBCharacteristic *characteristic,NSError *error); -typedef void (^BBDidReadRSSI)(NSNumber *RSSI,NSError *error); +typedef void (^BBDidReadRSSIBlock)(NSNumber *RSSI,NSError *error); -typedef void (^BBDidDiscoverIncludedServicesForService)(CBService *service,NSError *error); +typedef void (^BBDidDiscoverIncludedServicesForServiceBlock)(CBService *service,NSError *error); -typedef void (^BBDidUpdateName)(CBPeripheral *peripheral); +typedef void (^BBDidUpdateNameBlock)(CBPeripheral *peripheral); + +typedef void (^BBDidModifyServicesBlock)(CBPeripheral *peripheral,NSArray *invalidatedServices); + + +//peripheral model +typedef void (^BBPeripheralModelDidUpdateState)(CBPeripheralManager *peripheral); +typedef void (^BBPeripheralModelDidAddService)(CBPeripheralManager *peripheral,CBService *service,NSError *error); +typedef void (^BBPeripheralModelDidStartAdvertising)(CBPeripheralManager *peripheral,NSError *error); +typedef void (^BBPeripheralModelDidReceiveReadRequest)(CBPeripheralManager *peripheral,CBATTRequest *request); +typedef void (^BBPeripheralModelDidReceiveWriteRequests)(CBPeripheralManager *peripheral,NSArray *requests); +typedef void (^BBPeripheralModelIsReadyToUpdateSubscribers)(CBPeripheralManager *peripheral); +typedef void (^BBPeripheralModelDidSubscribeToCharacteristic)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic); +typedef void (^BBPeripheralModelDidUnSubscribeToCharacteristic)(CBPeripheralManager *peripheral,CBCentral *central,CBCharacteristic *characteristic); -typedef void (^BBDidModifyServices)(CBPeripheral *peripheral,NSArray *invalidatedServices); @interface BabyCallback : NSObject -#pragma mark -callback block +#pragma mark - callback block //设备状态改变的委托 -@property(nonatomic,strong) BBcentralManagerDidUpdateStateBlock blockOnCentralManagerDidUpdateState; +@property (nonatomic, copy) BBCentralManagerDidUpdateStateBlock blockOnCentralManagerDidUpdateState; //发现peripherals -@property(nonatomic,strong) BBDiscoverPeripheralsBlock blockOnDiscoverPeripherals; +@property (nonatomic, copy) BBDiscoverPeripheralsBlock blockOnDiscoverPeripherals; //连接callback -@property(nonatomic,strong) BBConnectedPeripheralBlock blockOnConnectedPeripheral; +@property (nonatomic, copy) BBConnectedPeripheralBlock blockOnConnectedPeripheral; //连接设备失败的block -@property(nonatomic,strong) BBFailToConnectBlock blockOnFailToConnect; +@property (nonatomic, copy) BBFailToConnectBlock blockOnFailToConnect; //断开设备连接的bock -@property(nonatomic,strong) BBDisconnectBlock blockOnDisconnect; +@property (nonatomic, copy) BBDisconnectBlock blockOnDisconnect; //发现services -@property(nonatomic,strong) BBDiscoverServicesBlock blockOnDiscoverServices; +@property (nonatomic, copy) BBDiscoverServicesBlock blockOnDiscoverServices; //发现Characteristics -@property(nonatomic,strong) BBDiscoverCharacteristicsBlock blockOnDiscoverCharacteristics; +@property (nonatomic, copy) BBDiscoverCharacteristicsBlock blockOnDiscoverCharacteristics; //发现更新Characteristics的 -@property(nonatomic,strong) BBReadValueForCharacteristicBlock blockOnReadValueForCharacteristic; +@property (nonatomic, copy) BBReadValueForCharacteristicBlock blockOnReadValueForCharacteristic; //获取Characteristics的名称 -@property(nonatomic,strong) BBDiscoverDescriptorsForCharacteristicBlock blockOnDiscoverDescriptorsForCharacteristic; +@property (nonatomic, copy) BBDiscoverDescriptorsForCharacteristicBlock blockOnDiscoverDescriptorsForCharacteristic; //获取Descriptors的值 +@property (nonatomic,copy) BBReadValueForDescriptorsBlock blockOnReadValueForDescriptors; +@property (nonatomic, copy) BBDidWriteValueForCharacteristicBlock blockOnDidWriteValueForCharacteristic; -@property(nonatomic,strong) BBReadValueForDescriptorsBlock blockOnReadValueForDescriptors; - -@property(nonatomic,strong) BBDidWriteValueForCharacteristic blockOnDidWriteValueForCharacteristic; +@property (nonatomic, copy) BBDidWriteValueForDescriptorBlock blockOnDidWriteValueForDescriptor; -@property(nonatomic,strong) BBDidWriteValueForDescriptor blockOnDidWriteValueForDescriptor; +@property (nonatomic, copy) BBDidUpdateNotificationStateForCharacteristicBlock blockOnDidUpdateNotificationStateForCharacteristic; -@property(nonatomic,strong) BBDidUpdateNotificationStateForCharacteristic blockOnDidUpdateNotificationStateForCharacteristic; +@property (nonatomic, copy) BBDidReadRSSIBlock blockOnDidReadRSSI; -@property(nonatomic,strong) BBDidReadRSSI blockOnDidReadRSSI; +@property (nonatomic, copy) BBDidDiscoverIncludedServicesForServiceBlock blockOnDidDiscoverIncludedServicesForService; -@property(nonatomic,strong) BBDidDiscoverIncludedServicesForService blockOnDidDiscoverIncludedServicesForService; +@property (nonatomic, copy) BBDidUpdateNameBlock blockOnDidUpdateName; -@property(nonatomic,strong) BBDidUpdateName blockOnDidUpdateName; - -@property(nonatomic,strong) BBDidModifyServices blockOnDidModifyServices; +@property (nonatomic, copy) BBDidModifyServicesBlock blockOnDidModifyServices; //babyBluettooth stopScan方法调用后的回调 -@property(nonatomic,strong) BBCancelScanBlock blockOnCancelScan; +@property(nonatomic,copy) BBCancelScanBlock blockOnCancelScan; //babyBluettooth stopConnectAllPerihperals 方法调用后的回调 -@property(nonatomic,strong) BBCancelAllPeripheralsConnectionBlock blockOnCancelAllPeripheralsConnection; +@property(nonatomic,copy) BBCancelAllPeripheralsConnectionBlock blockOnCancelAllPeripheralsConnection; //babyBluettooth 蓝牙使用的参数参数 @property(nonatomic,strong) BabyOptions *babyOptions; -#pragma mark -过滤器Filter +#pragma mark - 过滤器Filter //发现peripherals规则 -@property(nonatomic,strong) BOOL (^filterOnDiscoverPeripherals)(NSString *peripheralName); +@property (nonatomic, copy) BOOL (^filterOnDiscoverPeripherals)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI); //连接peripherals规则 -@property(nonatomic,strong) BOOL (^filterOnConnetToPeripherals)(NSString *peripheralName); +@property (nonatomic, copy) BOOL (^filterOnconnectToPeripherals)(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI); + + +#pragma mark - peripheral model + +//peripheral model +@property (nonatomic, copy) BBPeripheralModelDidUpdateState blockOnPeripheralModelDidUpdateState; +@property (nonatomic, copy) BBPeripheralModelDidAddService blockOnPeripheralModelDidAddService; +@property (nonatomic, copy) BBPeripheralModelDidStartAdvertising blockOnPeripheralModelDidStartAdvertising; +@property (nonatomic, copy) BBPeripheralModelDidReceiveReadRequest blockOnPeripheralModelDidReceiveReadRequest; +@property (nonatomic, copy) BBPeripheralModelDidReceiveWriteRequests blockOnPeripheralModelDidReceiveWriteRequests; +@property (nonatomic, copy) BBPeripheralModelIsReadyToUpdateSubscribers blockOnPeripheralModelIsReadyToUpdateSubscribers; +@property (nonatomic, copy) BBPeripheralModelDidSubscribeToCharacteristic blockOnPeripheralModelDidSubscribeToCharacteristic; +@property (nonatomic, copy) BBPeripheralModelDidUnSubscribeToCharacteristic blockOnPeripheralModelDidUnSubscribeToCharacteristic; @end diff --git a/Classes/objc/BabyCallback.m b/Classes/objc/BabyCallback.m index 18e7936..9b01228 100644 --- a/Classes/objc/BabyCallback.m +++ b/Classes/objc/BabyCallback.m @@ -13,17 +13,19 @@ @implementation BabyCallback --(instancetype)init{ +- (instancetype)init { self = [super init]; - if (self){ - [self setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) { - if(![peripheralName isEqualToString:@""]) + if (self) { + [self setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + if (![peripheralName isEqualToString:@""]) { return YES; + } return NO; }]; - [self setFilterOnConnetToPeripherals:^BOOL(NSString *peripheralName) { - if(![peripheralName isEqualToString:@""]) + [self setFilterOnconnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + if (![peripheralName isEqualToString:@""]) { return YES; + } return NO; }]; } diff --git a/Classes/objc/Babysister.h b/Classes/objc/BabyCentralManager.h similarity index 59% rename from Classes/objc/Babysister.h rename to Classes/objc/BabyCentralManager.h index 1fd1f26..8009e04 100644 --- a/Classes/objc/Babysister.h +++ b/Classes/objc/BabyCentralManager.h @@ -2,6 +2,9 @@ BabyBluetooth 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 https://github.com/coolnameismy/BabyBluetooth + + @brief 蓝牙中心模式实现类 + */ // Created by 刘彦玮 on 15/7/30. @@ -12,11 +15,11 @@ #import #import "BabyToy.h" #import "BabySpeaker.h" +#import "BabyDefine.h" - -@interface Babysister : NSObject{ +@interface BabyCentralManager : NSObject { @public @@ -37,36 +40,51 @@ NSTimer *connectTimer; //pocket NSMutableDictionary *pocket; - //已经连接的设备 - NSMutableArray *connectedPeripherals; - + //主设备 - CBCentralManager *bleManager; + CBCentralManager *centralManager; //回叫方法 BabySpeaker *babySpeaker; @private - - + //已经连接的设备 + NSMutableArray *connectedPeripherals; + //已经连接的设备 + NSMutableArray *discoverPeripherals; + //需要自动重连的外设 + NSMutableArray *reConnectPeripherals; } + + //扫描Peripherals --(void)scanPeripherals; +- (void)scanPeripherals; //连接Peripherals --(void)connectToPeripheral:(CBPeripheral *)peripheral; +- (void)connectToPeripheral:(CBPeripheral *)peripheral; //断开设备连接 --(void)cancelPeripheralConnection:(CBPeripheral *)peripheral; +- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral; //断开所有已连接的设备 --(void)cancelAllPeripheralsConnection; +- (void)cancelAllPeripheralsConnection; //停止扫描 --(void)cancelScan; +- (void)cancelScan; //获取当前连接的peripherals --(NSArray *)findConnectedPeripherals; +- (NSArray *)findConnectedPeripherals; //获取当前连接的peripheral --(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName; +- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName; + +/** + sometimes ever,sometimes never. 相聚有时,后会无期 + + this is center with peripheral's story + **/ + +//sometimes ever:添加断开重连接的设备 +- (void)sometimes_ever:(CBPeripheral *)peripheral ; +//sometimes never:删除需要重连接的设备 +- (void)sometimes_never:(CBPeripheral *)peripheral ; @end diff --git a/Classes/objc/BabyCentralManager.m b/Classes/objc/BabyCentralManager.m new file mode 100644 index 0000000..6685eb9 --- /dev/null +++ b/Classes/objc/BabyCentralManager.m @@ -0,0 +1,483 @@ +/* + BabyBluetooth + 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 + https://github.com/coolnameismy/BabyBluetooth + */ + +// Created by 刘彦玮 on 15/7/30. +// Copyright (c) 2015年 刘彦玮. All rights reserved. +// + +#import "BabyCentralManager.h" +#import "BabyCallback.h" + + + +@implementation BabyCentralManager + +#define currChannel [babySpeaker callbackOnCurrChannel] + +- (instancetype)init { + self = [super init]; + if (self) { + + +#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0 + NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: + //蓝牙power没打开时alert提示框 + [NSNumber numberWithBool:YES],CBCentralManagerOptionShowPowerAlertKey, + //重设centralManager恢复的IdentifierKey + @"babyBluetoothRestore",CBCentralManagerOptionRestoreIdentifierKey, + nil]; + +#else + NSDictionary *options = nil; +#endif + + NSArray *backgroundModes = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"UIBackgroundModes"]; + if ([backgroundModes containsObject:@"bluetooth-central"]) { + //后台模式 + centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:options]; + } + else { + //非后台模式 + centralManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; + } + + //pocket + pocket = [[NSMutableDictionary alloc]init]; + connectedPeripherals = [[NSMutableArray alloc]init]; + discoverPeripherals = [[NSMutableArray alloc]init]; + reConnectPeripherals = [[NSMutableArray alloc]init]; + } + return self; + +} + + + +#pragma mark - 接收到通知 + +//扫描Peripherals +- (void)scanPeripherals { + [centralManager scanForPeripheralsWithServices:[currChannel babyOptions].scanForPeripheralsWithServices options:[currChannel babyOptions].scanForPeripheralsWithOptions]; +} + +//连接Peripherals +- (void)connectToPeripheral:(CBPeripheral *)peripheral{ + [centralManager connectPeripheral:peripheral options:[currChannel babyOptions].connectPeripheralWithOptions]; +} + +//断开设备连接 +- (void)cancelPeripheralConnection:(CBPeripheral *)peripheral { + [centralManager cancelPeripheralConnection:peripheral]; +} + +//断开所有已连接的设备 +- (void)cancelAllPeripheralsConnection { + for (int i=0;i>>CBCentralManagerStateUnknown"); + break; + case CBCentralManagerStateResetting: + BabyLog(@">>>CBCentralManagerStateResetting"); + break; + case CBCentralManagerStateUnsupported: + BabyLog(@">>>CBCentralManagerStateUnsupported"); + break; + case CBCentralManagerStateUnauthorized: + BabyLog(@">>>CBCentralManagerStateUnauthorized"); + break; + case CBCentralManagerStatePoweredOff: + BabyLog(@">>>CBCentralManagerStatePoweredOff"); + break; + case CBCentralManagerStatePoweredOn: + BabyLog(@">>>CBCentralManagerStatePoweredOn"); + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtCentralManagerEnable object:@{@"central":central}]; + break; + default: + break; + } + //状态改变callback + if ([currChannel blockOnCentralManagerDidUpdateState]) { + [currChannel blockOnCentralManagerDidUpdateState](central); + } +} + +- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict { + +} + +//扫描到Peripherals +- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI { + + //日志 + //BabyLog(@"当扫描到设备:%@",peripheral.name); + [self addDiscoverPeripheral:peripheral]; + + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidDiscoverPeripheral + object:@{@"central":central,@"peripheral":peripheral,@"advertisementData":advertisementData,@"RSSI":RSSI}]; + //扫描到设备callback + if ([currChannel filterOnDiscoverPeripherals]) { + if ([currChannel filterOnDiscoverPeripherals](peripheral.name,advertisementData,RSSI)) { + if ([currChannel blockOnDiscoverPeripherals]) { + [[babySpeaker callbackOnCurrChannel] blockOnDiscoverPeripherals](central,peripheral,advertisementData,RSSI); + } + } + } + + //处理连接设备 + if (needConnectPeripheral) { + if ([currChannel filterOnconnectToPeripherals](peripheral.name,advertisementData,RSSI)) { + [centralManager connectPeripheral:peripheral options:[currChannel babyOptions].connectPeripheralWithOptions]; + //开一个定时器监控连接超时的情况 + connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(disconnect:) userInfo:peripheral repeats:NO]; + } + } +} + +//停止扫描 +- (void)disconnect:(id)sender { + [centralManager stopScan]; +} + +//连接到Peripherals-成功 +- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral { + + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidConnectPeripheral + object:@{@"central":central,@"peripheral":peripheral}]; + + //设置委托 + [peripheral setDelegate:self]; + + //BabyLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name); + [connectTimer invalidate];//停止时钟 + [self addPeripheral:peripheral]; + + //执行回叫 + //扫描到设备callback + if ([currChannel blockOnConnectedPeripheral]) { + [currChannel blockOnConnectedPeripheral](central,peripheral); + } + + //扫描外设的服务 + if (needDiscoverServices) { + [peripheral discoverServices:[currChannel babyOptions].discoverWithServices]; + //discoverIncludedServices + } + +} + +//连接到Peripherals-失败 +- (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidFailToConnectPeripheral + object:@{@"central":central,@"peripheral":peripheral,@"error":error?error:@""}]; + + // BabyLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheral name],[error localizedDescription]); + if ([currChannel blockOnFailToConnect]) { + [currChannel blockOnFailToConnect](central,peripheral,error); + } +} + +//Peripherals断开连接 +- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error { + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidDisconnectPeripheral + object:@{@"central":central,@"peripheral":peripheral,@"error":error?error:@""}]; + + // BabyLog(@">>>外设连接断开连接 %@: %@\n", [peripheral name], [error localizedDescription]); + if (error) + { + BabyLog(@">>> didDisconnectPeripheral for %@ with error: %@", peripheral.name, [error localizedDescription]); + } + + [self deletePeripheral:peripheral]; + if ([currChannel blockOnDisconnect]) { + [currChannel blockOnDisconnect](central,peripheral,error); + } + + //判断是否全部链接都已经段开,调用blockOnCancelAllPeripheralsConnection委托 + if ([self findConnectedPeripherals].count == 0) { + //停止扫描callback + if ([currChannel blockOnCancelAllPeripheralsConnection]) { + [currChannel blockOnCancelAllPeripheralsConnection](centralManager); + } + // BabyLog(@">>> stopConnectAllPerihperals"); + } + + //检查并重新连接需要重连的设备 + if ([reConnectPeripherals containsObject:peripheral]) { + [self connectToPeripheral:peripheral]; + } +} + +//扫描到服务 +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { + + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidDiscoverServices + object:@{@"peripheral":peripheral,@"error":error?error:@""}]; + + // BabyLog(@">>>扫描到服务:%@",peripheral.services); + if (error) { + BabyLog(@">>>didDiscoverServices for %@ with error: %@", peripheral.name, [error localizedDescription]); + } + //回叫block + if ([currChannel blockOnDiscoverServices]) { + [currChannel blockOnDiscoverServices](peripheral,error); + } + + //discover characteristics + if (needDiscoverCharacteristics) { + for (CBService *service in peripheral.services) { + [peripheral discoverCharacteristics:[currChannel babyOptions].discoverWithCharacteristics forService:service]; + } + } +} + +//发现服务的Characteristics +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error { + + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidDiscoverCharacteristicsForService + object:@{@"peripheral":peripheral,@"service":service,@"error":error?error:@""}]; + + + if (error) { + BabyLog(@"error didDiscoverCharacteristicsForService for %@ with error: %@", service.UUID, [error localizedDescription]); + // return; + } + //回叫block + if ([currChannel blockOnDiscoverCharacteristics]) { + [currChannel blockOnDiscoverCharacteristics](peripheral,service,error); + } + + //如果需要更新Characteristic的值 + if (needReadValueForCharacteristic) { + for (CBCharacteristic *characteristic in service.characteristics) { + [peripheral readValueForCharacteristic:characteristic]; + //判断读写权限 + // if (characteristic.properties & CBCharacteristicPropertyRead ) { + // [peripheral readValueForCharacteristic:characteristic]; + // } + } + } + + //如果搜索Characteristic的Descriptors + if (needDiscoverDescriptorsForCharacteristic) { + for (CBCharacteristic *characteristic in service.characteristics) { + [peripheral discoverDescriptorsForCharacteristic:characteristic]; + } + } +} + +//读取Characteristics的值 +- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + + //发出通知 + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidUpdateValueForCharacteristic + object:@{@"peripheral":peripheral,@"characteristic":characteristic,@"error":error?error:@""}]; + + if (error) { + BabyLog(@"error didUpdateValueForCharacteristic %@ with error: %@", characteristic.UUID, [error localizedDescription]); + // return; + } + //查找字段订阅 + if ([babySpeaker notifyCallback:characteristic]) { + [babySpeaker notifyCallback:characteristic](peripheral,characteristic,error); + return; + } + //回叫block + if ([currChannel blockOnReadValueForCharacteristic]) { + [currChannel blockOnReadValueForCharacteristic](peripheral,characteristic,error); + } +} + +//发现Characteristics的Descriptors +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + + if (error) { + BabyLog(@"error Discovered DescriptorsForCharacteristic for %@ with error: %@", characteristic.UUID, [error localizedDescription]); + // return; + } + //回叫block + if ([currChannel blockOnDiscoverDescriptorsForCharacteristic]) { + [currChannel blockOnDiscoverDescriptorsForCharacteristic](peripheral,characteristic,error); + } + //如果需要更新Characteristic的Descriptors + if (needReadValueForDescriptors) { + for (CBDescriptor *d in characteristic.descriptors) { + [peripheral readValueForDescriptor:d]; + } + } + + //执行一次的方法 + if (oneReadValueForDescriptors) { + for (CBDescriptor *d in characteristic.descriptors) { + [peripheral readValueForDescriptor:d]; + } + oneReadValueForDescriptors = NO; + } +} + +//读取Characteristics的Descriptors的值 +- (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error { + + + if (error) { + BabyLog(@"error didUpdateValueForDescriptor for %@ with error: %@", descriptor.UUID, [error localizedDescription]); + // return; + } + //回叫block + if ([currChannel blockOnReadValueForDescriptors]) { + [currChannel blockOnReadValueForDescriptors](peripheral,descriptor,error); + } +} + +- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidWriteValueForCharacteristic object:@{@"characteristic":characteristic,@"error":error?error:@""}]; + + // BabyLog(@">>>didWriteValueForCharacteristic"); + // BabyLog(@">>>uuid:%@,new value:%@",characteristic.UUID,characteristic.value); + if ([currChannel blockOnDidWriteValueForCharacteristic]) { + [currChannel blockOnDidWriteValueForCharacteristic](characteristic,error); + } +} + +- (void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error { + // BabyLog(@">>>didWriteValueForCharacteristic"); + // BabyLog(@">>>uuid:%@,new value:%@",descriptor.UUID,descriptor.value); + if ([currChannel blockOnDidWriteValueForDescriptor]) { + [currChannel blockOnDidWriteValueForDescriptor](descriptor,error); + } +} + +//characteristic.isNotifying 状态改变 +- (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error { + + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidUpdateNotificationStateForCharacteristic object:@{@"characteristic":characteristic,@"error":error?error:@""}]; + + BabyLog(@">>>didUpdateNotificationStateForCharacteristic"); + BabyLog(@">>>uuid:%@,isNotifying:%@",characteristic.UUID,characteristic.isNotifying?@"isNotifying":@"Notifying"); + if ([currChannel blockOnDidUpdateNotificationStateForCharacteristic]) { + [currChannel blockOnDidUpdateNotificationStateForCharacteristic](characteristic,error); + } +} + +- (void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error { + if ([currChannel blockOnDidDiscoverIncludedServicesForService]) { + [currChannel blockOnDidDiscoverIncludedServicesForService](service,error); + } +} + +# if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 +- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error { + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidReadRSSI object:@{@"peripheral":peripheral,@"RSSI":peripheral.RSSI,@"error":error?error:@""}]; + + BabyLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",peripheral.RSSI); + if ([currChannel blockOnDidReadRSSI]) { + [currChannel blockOnDidReadRSSI](peripheral.RSSI,error); + } +} +#else +- (void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error { + [[NSNotificationCenter defaultCenter]postNotificationName:BabyNotificationAtDidReadRSSI object:@{@"peripheral":peripheral,@"RSSI":RSSI?RSSI:@100,@"error":error?error:@""}]; + + BabyLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",RSSI); + if ([currChannel blockOnDidReadRSSI]) { + [currChannel blockOnDidReadRSSI](RSSI,error); + } +} +#endif + +- (void)peripheralDidUpdateName:(CBPeripheral *)peripheral { + if ([currChannel blockOnDidUpdateName]) { + [currChannel blockOnDidUpdateName](peripheral); + } +} + +- (void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)invalidatedServices { + if ([currChannel blockOnDidModifyServices]) { + [currChannel blockOnDidModifyServices](peripheral,invalidatedServices); + } +} + +/** + sometimes ever,sometimes never. 相聚有时,后会无期 + + this is center with peripheral's story + **/ + +//sometimes ever:添加断开重连接的设备 +- (void)sometimes_ever:(CBPeripheral *)peripheral { + if (![reConnectPeripherals containsObject:peripheral]) { + [reConnectPeripherals addObject:peripheral]; + } +} +//sometimes never:删除需要重连接的设备 +- (void)sometimes_never:(CBPeripheral *)peripheral { + [reConnectPeripherals removeObject:peripheral]; +} + +#pragma mark - 私有方法 + + +#pragma mark - 设备list管理 + +- (void)addDiscoverPeripheral:(CBPeripheral *)peripheral{ + if (![discoverPeripherals containsObject:peripheral]) { + [discoverPeripherals addObject:peripheral]; + } +} + +- (void)addPeripheral:(CBPeripheral *)peripheral { + if (![connectedPeripherals containsObject:peripheral]) { + [connectedPeripherals addObject:peripheral]; + } +} + +- (void)deletePeripheral:(CBPeripheral *)peripheral{ + [connectedPeripherals removeObject:peripheral]; +} + +- (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName { + for (CBPeripheral *p in connectedPeripherals) { + if ([p.name isEqualToString:peripheralName]) { + return p; + } + } + return nil; +} + +- (NSArray *)findConnectedPeripherals{ + return connectedPeripherals; +} + + +@end diff --git a/Classes/objc/BabyDefine.h b/Classes/objc/BabyDefine.h new file mode 100644 index 0000000..d9af1a5 --- /dev/null +++ b/Classes/objc/BabyDefine.h @@ -0,0 +1,77 @@ +/* + BabyBluetooth + 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 + https://github.com/coolnameismy/BabyBluetooth + +@brief 预定义一些库的执行行为和配置 + + */ + +// Created by 刘彦玮 on 6/4/19. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + + +# pragma mark - baby 行为定义 + +//Baby if show log 是否打印日志,默认1:打印 ,0:不打印 +#define KBABY_IS_SHOW_LOG 1 + +//CBcentralManager等待设备打开次数 +# define KBABY_CENTRAL_MANAGER_INIT_WAIT_TIMES 5 + +//CBcentralManager等待设备打开间隔时间 +# define KBABY_CENTRAL_MANAGER_INIT_WAIT_SECOND 2.0 + +//BabyRhythm默认心跳时间间隔 +#define KBABYRHYTHM_BEATS_DEFAULT_INTERVAL 3; + +//Baby默认链式方法channel名称 +#define KBABY_DETAULT_CHANNEL @"babyDefault" + +# pragma mark - baby通知 + +//蓝牙系统通知 +//centralManager status did change notification +#define BabyNotificationAtCentralManagerDidUpdateState @"BabyNotificationAtCentralManagerDidUpdateState" +//did discover peripheral notification +#define BabyNotificationAtDidDiscoverPeripheral @"BabyNotificationAtDidDiscoverPeripheral" +//did connection peripheral notification +#define BabyNotificationAtDidConnectPeripheral @"BabyNotificationAtDidConnectPeripheral" +//did filed connect peripheral notification +#define BabyNotificationAtDidFailToConnectPeripheral @"BabyNotificationAtDidFailToConnectPeripheral" +//did disconnect peripheral notification +#define BabyNotificationAtDidDisconnectPeripheral @"BabyNotificationAtDidDisconnectPeripheral" +//did discover service notification +#define BabyNotificationAtDidDiscoverServices @"BabyNotificationAtDidDiscoverServices" +//did discover characteristics notification +#define BabyNotificationAtDidDiscoverCharacteristicsForService @"BabyNotificationAtDidDiscoverCharacteristicsForService" +//did read or notify characteristic when received value notification +#define BabyNotificationAtDidUpdateValueForCharacteristic @"BabyNotificationAtDidUpdateValueForCharacteristic" +//did write characteristic and response value notification +#define BabyNotificationAtDidWriteValueForCharacteristic @"BabyNotificationAtDidWriteValueForCharacteristic" +//did change characteristis notify status notification +#define BabyNotificationAtDidUpdateNotificationStateForCharacteristic @"BabyNotificationAtDidUpdateNotificationStateForCharacteristic" +//did read rssi and receiced value notification +#define BabyNotificationAtDidReadRSSI @"BabyNotificationAtDidReadRSSI" + +//蓝牙扩展通知 +// did centralManager enable notification +#define BabyNotificationAtCentralManagerEnable @"BabyNotificationAtCentralManagerEnable" + + + +# pragma mark - baby 定义的方法 + +//Baby log +#define BabyLog(fmt, ...) if(KBABY_IS_SHOW_LOG) { NSLog(fmt,##__VA_ARGS__); } + + + + + +@interface BabyDefine : NSObject + +@end diff --git a/Classes/objc/BabyDefine.m b/Classes/objc/BabyDefine.m new file mode 100644 index 0000000..2e2eb41 --- /dev/null +++ b/Classes/objc/BabyDefine.m @@ -0,0 +1,16 @@ +// +// BabyBlueDefine.m +// BabyTestProject +// +// Created by xuanyan.lyw on 16/4/19. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "BabyDefine.h" + + + + +@implementation BabyDefine + +@end diff --git a/Classes/objc/BabyOptions.h b/Classes/objc/BabyOptions.h index a937766..b0899e7 100644 --- a/Classes/objc/BabyOptions.h +++ b/Classes/objc/BabyOptions.h @@ -1,7 +1,12 @@ -// -// BabyClothes.h -// BabyBluetoothAppDemo -// +/* + BabyBluetooth + 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 + https://github.com/coolnameismy/BabyBluetooth + + @brief babybluetooth 封装蓝牙外设模式的运行时参数,可以实现后台模式,重复接收广播,查找service参数,查找characteristic参数 + + */ + // Created by 刘彦玮 on 15/9/27. // Copyright © 2015年 刘彦玮. All rights reserved. // @@ -11,7 +16,7 @@ @interface BabyOptions : NSObject -#pragma mark -属性 +#pragma mark - 属性 /*! * 扫描参数,centralManager:scanForPeripheralsWithServices:self.scanForPeripheralsWithServices options:self.scanForPeripheralsWithOptions * @param An optional dictionary specifying options for the scan. @@ -19,7 +24,7 @@ * @seealso CBCentralManagerScanOptionAllowDuplicatesKey :忽略同一个Peripheral端的多个发现事件被聚合成一个发现事件 * @seealso CBCentralManagerScanOptionSolicitedServiceUUIDsKey */ -@property(nonatomic,strong) NSDictionary *scanForPeripheralsWithOptions; +@property (nonatomic, copy) NSDictionary *scanForPeripheralsWithOptions; /*! * 连接设备的参数 @@ -31,7 +36,7 @@ * @seealso CBConnectPeripheralOptionNotifyOnDisconnectionKey * @seealso CBConnectPeripheralOptionNotifyOnNotificationKey */ -@property(nonatomic,strong) NSDictionary *connectPeripheralWithOptions; +@property (nonatomic, copy) NSDictionary *connectPeripheralWithOptions; @@ -40,20 +45,20 @@ *@param serviceUUIDs A list of CBUUID objects representing the service(s) to scan for. *@see centralManager:scanForPeripheralsWithServices */ -@property(nonatomic,strong) NSArray *scanForPeripheralsWithServices; +@property (nonatomic, copy) NSArray *scanForPeripheralsWithServices; // [peripheral discoverServices:self.discoverWithServices]; -@property(nonatomic,strong) NSArray *discoverWithServices; +@property (nonatomic, copy) NSArray *discoverWithServices; // [peripheral discoverCharacteristics:self.discoverWithCharacteristics forService:service]; -@property(nonatomic,strong) NSArray *discoverWithCharacteristics; +@property (nonatomic, copy) NSArray *discoverWithCharacteristics; -#pragma mark -构造方法 --(instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions +#pragma mark - 构造方法 +- (instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *)connectPeripheralWithOptions; --(instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions +- (instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *)connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithServices:(NSArray *)discoverWithServices diff --git a/Classes/objc/BabyOptions.m b/Classes/objc/BabyOptions.m index bb87c62..7739f20 100644 --- a/Classes/objc/BabyOptions.m +++ b/Classes/objc/BabyOptions.m @@ -10,37 +10,37 @@ @implementation BabyOptions --(instancetype)init{ +- (instancetype)init { self = [super init]; - if(self){ - [self setScanForPeripheralsWithOptions:nil]; - [self setConnectPeripheralWithOptions:nil]; - [self setScanForPeripheralsWithServices:nil]; - [self setDiscoverWithCharacteristics:nil]; - [self setDiscoverWithServices:nil]; + if (self) { + _scanForPeripheralsWithOptions = nil; + _connectPeripheralWithOptions = nil; + _scanForPeripheralsWithServices = nil; + _discoverWithServices = nil; + _discoverWithCharacteristics = nil; } return self; } --(instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions +- (instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *)connectPeripheralWithOptions { self = [super init]; - if(self){ + if (self) { [self setScanForPeripheralsWithOptions:scanForPeripheralsWithOptions]; [self setConnectPeripheralWithOptions:connectPeripheralWithOptions]; } return self; } --(instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions +- (instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeripheralsWithOptions connectPeripheralWithOptions:(NSDictionary *)connectPeripheralWithOptions scanForPeripheralsWithServices:(NSArray *)scanForPeripheralsWithServices discoverWithServices:(NSArray *)discoverWithServices discoverWithCharacteristics:(NSArray *)discoverWithCharacteristics { self = [self initWithscanForPeripheralsWithOptions:scanForPeripheralsWithOptions connectPeripheralWithOptions:connectPeripheralWithOptions]; - if(self){ + if (self) { [self setScanForPeripheralsWithServices:scanForPeripheralsWithServices]; [self setDiscoverWithServices:discoverWithServices]; [self setDiscoverWithCharacteristics:discoverWithCharacteristics]; @@ -48,6 +48,4 @@ -(instancetype)initWithscanForPeripheralsWithOptions:(NSDictionary *)scanForPeri return self; } - - @end diff --git a/Classes/objc/BabyPeripheralManager.h b/Classes/objc/BabyPeripheralManager.h new file mode 100644 index 0000000..12579f3 --- /dev/null +++ b/Classes/objc/BabyPeripheralManager.h @@ -0,0 +1,93 @@ +/* + BabyBluetooth + 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 + https://github.com/coolnameismy/BabyBluetooth + + @brief 蓝牙外设模式实现类 + + */ + + +// Created by 刘彦玮 on 15/12/12. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import +#import +#import "BabyToy.h" +#import "BabySpeaker.h" + + +@interface BabyPeripheralManager : NSObject { + +@public + //回叫方法 + BabySpeaker *babySpeaker; +} + +/** + 添加服务 + */ +- (BabyPeripheralManager *(^)(NSArray *array))addServices; + +/** + 添加广播包数据 + */ +- (BabyPeripheralManager *(^)(NSData *data))addManufacturerData; +/** + 移除广播包数据 + */ +- (BabyPeripheralManager *(^)())removeAllServices; + +/** +启动广播 + */ +- (BabyPeripheralManager *(^)())startAdvertising; + +/** + 停止广播 + */ +- (BabyPeripheralManager *(^)())stopAdvertising; + +//外设管理器 +@property (nonatomic, strong) CBPeripheralManager *peripheralManager; +//设备名称 +@property (nonatomic, copy) NSString *localName; +//设备服务 +@property (nonatomic, strong) NSMutableArray *services; +//设备广播包数据 +@property (nonatomic, strong) NSData *manufacturerData; + + +@end + + +/** + * 构造Characteristic,并加入service + * service:CBService + + * param`ter for properties :option 'r' | 'w' | 'n' or combination + * r CBCharacteristicPropertyRead + * w CBCharacteristicPropertyWrite + * n CBCharacteristicPropertyNotify + * default value is rw Read-Write + + * paramter for descriptor:be uesd descriptor for characteristic + */ + +void makeCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *properties,NSString *descriptor); + +/** + * 构造一个包含初始值的Characteristic,并加入service,包含了初值的characteristic必须设置permissions和properties都为只读 + * make characteristic then add to service, a static characteristic mean it has a initial value .according apple rule, it must set properties and permissions to CBCharacteristicPropertyRead and CBAttributePermissionsReadable +*/ +void makeStaticCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *descriptor,NSData *data); +/** + 生成CBService + */ +CBMutableService* makeCBService(NSString *UUID); + +/** + 生成UUID + */ +NSString* genUUID(); diff --git a/Classes/objc/BabyPeripheralManager.m b/Classes/objc/BabyPeripheralManager.m new file mode 100644 index 0000000..cea011e --- /dev/null +++ b/Classes/objc/BabyPeripheralManager.m @@ -0,0 +1,270 @@ +// +// BabyPeripheralManager.m +// BluetoothStubOnIOS +// +// Created by 刘彦玮 on 15/12/12. +// Copyright © 2015年 刘彦玮. All rights reserved. +// + +#import "BabyPeripheralManager.h" +#import "BabyDefine.h" + +#define callbackBlock(...) if ([[babySpeaker callback] __VA_ARGS__]) [[babySpeaker callback] __VA_ARGS__ ] + +@implementation BabyPeripheralManager { + int PERIPHERAL_MANAGER_INIT_WAIT_TIMES; + int didAddServices; + NSTimer *addServiceTask; +} + +- (instancetype)init { + self = [super init]; + if (self) { + _localName = @"baby-default-name"; + _peripheralManager = [[CBPeripheralManager alloc]initWithDelegate:self queue:nil options:nil]; + } + return self; +} + + +- (BabyPeripheralManager *(^)())startAdvertising { + return ^BabyPeripheralManager *() { + + if ([self canStartAdvertising]) { + PERIPHERAL_MANAGER_INIT_WAIT_TIMES = 0; + NSMutableArray *UUIDS = [NSMutableArray array]; + for (CBMutableService *s in _services) { + [UUIDS addObject:s.UUID]; + } + //启动广播 + if (_manufacturerData) { + [_peripheralManager startAdvertising: + @{ + CBAdvertisementDataServiceUUIDsKey : UUIDS + ,CBAdvertisementDataLocalNameKey : _localName, + CBAdvertisementDataManufacturerDataKey:_manufacturerData + }]; + } else { + [_peripheralManager startAdvertising: + @{ + CBAdvertisementDataServiceUUIDsKey : UUIDS + ,CBAdvertisementDataLocalNameKey : _localName + }]; + } + + } + else { + PERIPHERAL_MANAGER_INIT_WAIT_TIMES++; + if (PERIPHERAL_MANAGER_INIT_WAIT_TIMES > 5) { + BabyLog(@">>>error: 第%d次等待peripheralManager打开任然失败,请检查蓝牙设备是否可用",PERIPHERAL_MANAGER_INIT_WAIT_TIMES); + } + dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, 3.0 * NSEC_PER_SEC); + dispatch_after(popTime, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ + self.startAdvertising(); + }); + BabyLog(@">>> 第%d次等待peripheralManager打开",PERIPHERAL_MANAGER_INIT_WAIT_TIMES); + } + + return self; + }; +} + +- (BabyPeripheralManager *(^)())stopAdvertising { + return ^BabyPeripheralManager*() { + [_peripheralManager stopAdvertising]; + return self; + }; +} + +- (BOOL)canStartAdvertising { + if (_peripheralManager.state != CBPeripheralManagerStatePoweredOn) { + return NO; + } + if (didAddServices != _services.count) { + return NO; + } + return YES; +} + +- (BOOL)isPoweredOn { + if (_peripheralManager.state != CBPeripheralManagerStatePoweredOn) { + return NO; + } + return YES; +} + +- (BabyPeripheralManager *(^)(NSArray *array))addServices { + return ^BabyPeripheralManager*(NSArray *array) { + _services = [NSMutableArray arrayWithArray:array]; + [self addServicesToPeripheral]; + return self; + }; +} + +- (BabyPeripheralManager *(^)())removeAllServices { + return ^BabyPeripheralManager*() { + didAddServices = 0; + [_peripheralManager removeAllServices]; + return self; + }; +} + +- (BabyPeripheralManager *(^)(NSData *data))addManufacturerData { + return ^BabyPeripheralManager*(NSData *data) { + _manufacturerData = data; + return self; + }; +} + +- (void)addServicesToPeripheral { + if ([self isPoweredOn]) { + for (CBMutableService *s in _services) { + [_peripheralManager addService:s]; + } + } + else { + [addServiceTask setFireDate:[NSDate distantPast]]; + addServiceTask = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(addServicesToPeripheral) userInfo:nil repeats:NO]; + } +} + +#pragma mark - peripheralManager delegate + +- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { + switch (peripheral.state) { + case CBPeripheralManagerStateUnknown: + BabyLog(@">>>CBPeripheralManagerStateUnknown"); + break; + case CBPeripheralManagerStateResetting: + BabyLog(@">>>CBPeripheralManagerStateResetting"); + break; + case CBPeripheralManagerStateUnsupported: + BabyLog(@">>>CBPeripheralManagerStateUnsupported"); + break; + case CBPeripheralManagerStateUnauthorized: + BabyLog(@">>>CBPeripheralManagerStateUnauthorized"); + break; + case CBPeripheralManagerStatePoweredOff: + BabyLog(@">>>CBPeripheralManagerStatePoweredOff"); + break; + case CBPeripheralManagerStatePoweredOn: + BabyLog(@">>>CBPeripheralManagerStatePoweredOn"); + //发送centralManagerDidUpdateState通知 + [[NSNotificationCenter defaultCenter]postNotificationName:@"CBPeripheralManagerStatePoweredOn" object:nil]; + break; + default: + break; + } + +// if([babySpeaker callback] blockOnPeripheralModelDidUpdateState) { +// [currChannel blockOnCancelScan](centralManager); +// } + callbackBlock(blockOnPeripheralModelDidUpdateState)(peripheral); +} + + +- (void)peripheralManager:(CBPeripheralManager *)peripheral didAddService:(CBService *)service error:(NSError *)error { + didAddServices++; + callbackBlock(blockOnPeripheralModelDidAddService)(peripheral,service,error); +} + +- (void)peripheralManagerDidStartAdvertising:(CBPeripheralManager *)peripheral error:(NSError *)error { + callbackBlock(blockOnPeripheralModelDidStartAdvertising)(peripheral,error); +} + +- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveReadRequest:(CBATTRequest *)request { + callbackBlock(blockOnPeripheralModelDidReceiveReadRequest)(peripheral, request); +} + +- (void)peripheralManager:(CBPeripheralManager *)peripheral didReceiveWriteRequests:(NSArray *)requests { + callbackBlock(blockOnPeripheralModelDidReceiveWriteRequests)(peripheral,requests); +} + +- (void)peripheralManagerIsReadyToUpdateSubscribers:(CBPeripheralManager *)peripheral { + callbackBlock(blockOnPeripheralModelIsReadyToUpdateSubscribers)(peripheral); +} + +- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didSubscribeToCharacteristic:(CBCharacteristic *)characteristic { + callbackBlock(blockOnPeripheralModelDidSubscribeToCharacteristic)(peripheral,central,characteristic); +} + +- (void)peripheralManager:(CBPeripheralManager *)peripheral central:(CBCentral *)central didUnsubscribeFromCharacteristic:(CBCharacteristic *)characteristic { + callbackBlock(blockOnPeripheralModelDidUnSubscribeToCharacteristic)(peripheral,central,characteristic); +} + + +@end + +void makeCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *properties,NSString *descriptor) { + + //paramter for properties + CBCharacteristicProperties prop = 0x00; + if ([properties containsString:@"r"]) { + prop = prop | CBCharacteristicPropertyRead; + } + if ([properties containsString:@"w"]) { + prop = prop | CBCharacteristicPropertyWrite; + } + if ([properties containsString:@"n"]) { + prop = prop | CBCharacteristicPropertyNotify; + } + if (properties == nil || [properties isEqualToString:@""]) { + prop = CBCharacteristicPropertyRead | CBCharacteristicPropertyWrite; + } + + CBMutableCharacteristic *c = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:UUID] properties:prop value:nil permissions:CBAttributePermissionsReadable | CBAttributePermissionsWriteable]; + + //paramter for descriptor + if (!(descriptor == nil || [descriptor isEqualToString:@""])) { + //c设置description对应的haracteristics字段描述 + CBUUID *CBUUIDCharacteristicUserDescriptionStringUUID = [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString]; + CBMutableDescriptor *desc = [[CBMutableDescriptor alloc]initWithType: CBUUIDCharacteristicUserDescriptionStringUUID value:descriptor]; + [c setDescriptors:@[desc]]; + } + + if (!service.characteristics) { + service.characteristics = @[]; + } + NSMutableArray *cs = [service.characteristics mutableCopy]; + [cs addObject:c]; + service.characteristics = [cs copy]; +} +void makeStaticCharacteristicToService(CBMutableService *service,NSString *UUID,NSString *descriptor,NSData *data) { + + CBMutableCharacteristic *c = [[CBMutableCharacteristic alloc]initWithType:[CBUUID UUIDWithString:UUID] properties:CBCharacteristicPropertyRead value:data permissions:CBAttributePermissionsReadable]; + + //paramter for descriptor + if (!(descriptor == nil || [descriptor isEqualToString:@""])) { + //c设置description对应的haracteristics字段描述 + CBUUID *CBUUIDCharacteristicUserDescriptionStringUUID = [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString]; + CBMutableDescriptor *desc = [[CBMutableDescriptor alloc]initWithType: CBUUIDCharacteristicUserDescriptionStringUUID value:descriptor]; + [c setDescriptors:@[desc]]; + } + + if (!service.characteristics) { + service.characteristics = @[]; + } + NSMutableArray *cs = [service.characteristics mutableCopy]; + [cs addObject:c]; + service.characteristics = [cs copy]; +} + + +CBMutableService* makeCBService(NSString *UUID) +{ + CBMutableService *s = [[CBMutableService alloc]initWithType:[CBUUID UUIDWithString:UUID] primary:YES]; + return s; +} + +NSString * genUUID() +{ + CFUUIDRef uuid_ref = CFUUIDCreate(NULL); + CFStringRef uuid_string_ref= CFUUIDCreateString(NULL, uuid_ref); + + CFRelease(uuid_ref); + NSString *uuid = [NSString stringWithString:(__bridge NSString*)uuid_string_ref]; + + CFRelease(uuid_string_ref); + return uuid; +} + diff --git a/Classes/objc/BabyRhythm.h b/Classes/objc/BabyRhythm.h index 59205d3..6a6dd72 100644 --- a/Classes/objc/BabyRhythm.h +++ b/Classes/objc/BabyRhythm.h @@ -1,13 +1,18 @@ -// -// BabyBeats.h -// BabyBluetoothAppDemo +/* + BabyBluetooth + 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 + https://github.com/coolnameismy/BabyBluetooth + + @brief babybluetooth Rhythm用于检测蓝牙的任务执行情况,处理复杂的蓝牙流程操作 + + */ // // Created by ZTELiuyw on 15/9/15. // Copyright (c) 2015年 刘彦玮. All rights reserved. // #import - +#import "BabyDefine.h" @interface BabyRhythm : NSObject @@ -17,32 +22,26 @@ typedef void (^BBBeatsBreakBlock)(BabyRhythm *bry); typedef void (^BBBeatsOverBlock)(BabyRhythm *bry); //timer for beats -@property(nonatomic,strong) NSTimer *beatsTimer; +@property (nonatomic, strong) NSTimer *beatsTimer; //beat interval -@property int beatsInterval; - -//心跳时间间隔,默认是3 -//defalut interval -#define beatsDefaultInterval 3; +@property NSInteger beatsInterval; #pragma mark beats //心跳 --(void)beats; +- (void)beats; //主动中断心跳 --(void)beatsBreak; +- (void)beatsBreak; //结束心跳,结束后会进入BlockOnBeatOver,并且结束后再不会在触发BlockOnBeatBreak --(void)beatsOver; +- (void)beatsOver; //恢复心跳,beatsOver操作后可以使用beatsRestart恢复心跳,恢复后又可以进入BlockOnBeatBreak方法 --(void)beatsRestart; +- (void)beatsRestart; //心跳中断的委托 --(void)setBlockOnBeatsBreak:(void(^)(BabyRhythm *bry))block; +- (void)setBlockOnBeatsBreak:(void(^)(BabyRhythm *bry))block; //心跳结束的委托 --(void)setBlockOnBeatsOver:(void(^)(BabyRhythm *bry))block; - - +- (void)setBlockOnBeatsOver:(void(^)(BabyRhythm *bry))block; @end diff --git a/Classes/objc/BabyRhythm.m b/Classes/objc/BabyRhythm.m index da3a8c1..328d213 100644 --- a/Classes/objc/BabyRhythm.m +++ b/Classes/objc/BabyRhythm.m @@ -7,51 +7,51 @@ // #import "BabyRhythm.h" +#import "BabyDefine.h" -@implementation BabyRhythm{ +@implementation BabyRhythm { BOOL isOver; BBBeatsBreakBlock blockOnBeatBreak; BBBeatsOverBlock blockOnBeatOver; } - - - --(instancetype)init{ +- (instancetype)init { self = [super init]; - if(self){ + if (self) { //beatsInterval - self.beatsInterval = beatsDefaultInterval; + _beatsInterval = KBABYRHYTHM_BEATS_DEFAULT_INTERVAL; } return self; } --(void)beats{ +- (void)beats { if (isOver) { - NSLog(@">>>beats isOver"); + BabyLog(@">>>beats isOver"); return; } - NSLog(@">>>beats at :%@",[NSDate date]); + BabyLog(@">>>beats at :%@",[NSDate date]); if (self.beatsTimer) { [self.beatsTimer setFireDate: [[NSDate date]dateByAddingTimeInterval:self.beatsInterval]]; - }else{ + } + else { self.beatsTimer = [NSTimer timerWithTimeInterval:self.beatsInterval target:self selector:@selector(beatsBreak) userInfo:nil repeats:YES]; [self.beatsTimer setFireDate: [[NSDate date]dateByAddingTimeInterval:self.beatsInterval]]; [[NSRunLoop currentRunLoop] addTimer:self.beatsTimer forMode:NSRunLoopCommonModes]; } } --(void)beatsBreak{ - NSLog(@">>>beatsBreak :%@",[NSDate date]); +- (void)beatsBreak { + BabyLog(@">>>beatsBreak :%@",[NSDate date]); [self.beatsTimer setFireDate:[NSDate distantFuture]]; if (blockOnBeatBreak) { blockOnBeatBreak(self); } } --(void)beatsOver{ - NSLog(@">>>beatsOver :%@",[NSDate date]); + +- (void)beatsOver { + BabyLog(@">>>beatsOver :%@",[NSDate date]); [self.beatsTimer setFireDate:[NSDate distantFuture]]; isOver = YES; if (blockOnBeatOver) { @@ -59,17 +59,18 @@ -(void)beatsOver{ } } --(void)beatsRestart{ - NSLog(@">>>beatsRestart :%@",[NSDate date]); + +- (void)beatsRestart { + BabyLog(@">>>beatsRestart :%@",[NSDate date]); isOver = NO; [self beats]; } --(void)setBlockOnBeatsBreak:(void(^)(BabyRhythm *bry))block{ +- (void)setBlockOnBeatsBreak:(void(^)(BabyRhythm *bry))block { blockOnBeatBreak = block; } --(void)setBlockOnBeatsOver:(void(^)(BabyRhythm *bry))block{ +- (void)setBlockOnBeatsOver:(void(^)(BabyRhythm *bry))block { blockOnBeatOver = block; } diff --git a/Classes/objc/BabySpeaker.h b/Classes/objc/BabySpeaker.h index 2c61a5e..0f51603 100644 --- a/Classes/objc/BabySpeaker.h +++ b/Classes/objc/BabySpeaker.h @@ -2,6 +2,9 @@ BabyBluetooth 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 https://github.com/coolnameismy/BabyBluetooth + + @brief babybluetooth block查找和channel切换 + */ // Created by 刘彦玮 on 15/9/2. @@ -14,26 +17,26 @@ @interface BabySpeaker : NSObject --(BabyCallback *)callback; --(BabyCallback *)callbackOnCurrChannel; --(BabyCallback *)callbackOnChnnel:(NSString *)channel; --(BabyCallback *)callbackOnChnnel:(NSString *)channel +- (BabyCallback *)callback; +- (BabyCallback *)callbackOnCurrChannel; +- (BabyCallback *)callbackOnChnnel:(NSString *)channel; +- (BabyCallback *)callbackOnChnnel:(NSString *)channel createWhenNotExist:(BOOL)createWhenNotExist; //切换频道 --(void)switchChannel:(NSString *)channel; +- (void)switchChannel:(NSString *)channel; //添加到notify list --(void)addNotifyCallback:(CBCharacteristic *)c +- (void)addNotifyCallback:(CBCharacteristic *)c withBlock:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block; //添加到notify list --(void)removeNotifyCallback:(CBCharacteristic *)c; +- (void)removeNotifyCallback:(CBCharacteristic *)c; //获取notify list --(NSMutableDictionary *)notifyCallBackList; +- (NSMutableDictionary *)notifyCallBackList; //获取notityBlock --(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))notifyCallback:(CBCharacteristic *)c; +- (void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))notifyCallback:(CBCharacteristic *)c; @end diff --git a/Classes/objc/BabySpeaker.m b/Classes/objc/BabySpeaker.m index c449978..8850b34 100644 --- a/Classes/objc/BabySpeaker.m +++ b/Classes/objc/BabySpeaker.m @@ -9,7 +9,7 @@ // #import "BabySpeaker.h" - +#import "BabyDefine.h" typedef NS_ENUM(NSUInteger, BabySpeakerType) { @@ -24,10 +24,8 @@ typedef NS_ENUM(NSUInteger, BabySpeakerType) { BabySpeakerTypeDiscoverPeripheralsReadValueForDescriptorsBlock }; -//默认channel名称 -#define defaultChannel @"babyDefault" -@implementation BabySpeaker{ +@implementation BabySpeaker { //所有委托频道 NSMutableDictionary *channels; //当前委托频道 @@ -36,38 +34,38 @@ @implementation BabySpeaker{ NSMutableDictionary *notifyList; } --(instancetype)init{ +- (instancetype)init { self = [super init]; if (self) { BabyCallback *defaultCallback = [[BabyCallback alloc]init]; notifyList = [[NSMutableDictionary alloc]init]; channels = [[NSMutableDictionary alloc]init]; - currChannel = defaultChannel; - [channels setObject:defaultCallback forKey:defaultChannel]; + currChannel = KBABY_DETAULT_CHANNEL; + [channels setObject:defaultCallback forKey:KBABY_DETAULT_CHANNEL]; } return self; } --(BabyCallback *)callback{ - return [channels objectForKey:defaultChannel]; +- (BabyCallback *)callback { + return [channels objectForKey:KBABY_DETAULT_CHANNEL]; } --(BabyCallback *)callbackOnCurrChannel { +- (BabyCallback *)callbackOnCurrChannel { return [self callbackOnChnnel:currChannel]; } --(BabyCallback *)callbackOnChnnel:(NSString *)channel{ +- (BabyCallback *)callbackOnChnnel:(NSString *)channel { if (!channel) { [self callback]; } return [channels objectForKey:channel]; } --(BabyCallback *)callbackOnChnnel:(NSString *)channel - createWhenNotExist:(BOOL)createWhenNotExist{ +- (BabyCallback *)callbackOnChnnel:(NSString *)channel + createWhenNotExist:(BOOL)createWhenNotExist { BabyCallback *callback = [channels objectForKey:channel]; - if (!callback && createWhenNotExist){ + if (!callback && createWhenNotExist) { callback = [[BabyCallback alloc]init]; [channels setObject:callback forKey:channel]; } @@ -75,42 +73,40 @@ -(BabyCallback *)callbackOnChnnel:(NSString *)channel return callback; } - --(void)switchChannel:(NSString *)channel{ +- (void)switchChannel:(NSString *)channel { if (channel) { if ([self callbackOnChnnel:channel]) { currChannel = channel; - NSLog(@">>>已切换到%@",channel); + BabyLog(@">>>已切换到%@",channel); } - else{ - NSLog(@">>>所要切换的channel不存在"); + else { + BabyLog(@">>>所要切换的channel不存在"); } - }else{ - currChannel = defaultChannel; - NSLog(@">>>已切换到默认频道"); } - - + else { + currChannel = KBABY_DETAULT_CHANNEL; + BabyLog(@">>>已切换到默认频道"); + } } //添加到notify list --(void)addNotifyCallback:(CBCharacteristic *)c - withBlock:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block{ +- (void)addNotifyCallback:(CBCharacteristic *)c + withBlock:(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))block { [notifyList setObject:block forKey:c.UUID.description]; } //添加到notify list --(void)removeNotifyCallback:(CBCharacteristic *)c{ +- (void)removeNotifyCallback:(CBCharacteristic *)c { [notifyList removeObjectForKey:c.UUID.description]; } //获取notify list --(NSMutableDictionary *)notifyCallBackList{ +- (NSMutableDictionary *)notifyCallBackList { return notifyList; } //获取notityBlock --(void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))notifyCallback:(CBCharacteristic *)c{ +- (void(^)(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error))notifyCallback:(CBCharacteristic *)c { return [notifyList objectForKey:c.UUID.description]; } @end diff --git a/Classes/objc/BabyToy.h b/Classes/objc/BabyToy.h index 3f5a12e..bf2c92c 100644 --- a/Classes/objc/BabyToy.h +++ b/Classes/objc/BabyToy.h @@ -2,6 +2,9 @@ BabyBluetooth 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 https://github.com/coolnameismy/BabyBluetooth + + @brief babybluetooth 工具类 + */ // Created by 刘彦玮 on 15/8/1. @@ -15,8 +18,6 @@ @interface BabyToy : NSObject - - //十六进制转换为普通字符串的。 + (NSString *)ConvertHexStringToString:(NSString *)hexString; //普通字符串转换为十六进制 @@ -30,5 +31,4 @@ //根据UUIDString查找CBCharacteristic +(CBCharacteristic *)findCharacteristicFormServices:(NSMutableArray *)services UUIDString:(NSString *)UUIDString; - @end diff --git a/Classes/objc/BabyToy.m b/Classes/objc/BabyToy.m index 38e9002..7a3ce0e 100644 --- a/Classes/objc/BabyToy.m +++ b/Classes/objc/BabyToy.m @@ -26,39 +26,40 @@ + (NSString *)ConvertHexStringToString:(NSString *)hexString { myBuffer[i / 2] = (char)anInt; } NSString *unicodeString = [NSString stringWithCString:myBuffer encoding:4]; -// NSLog(@"===字符串===%@",unicodeString); +// BabyLog(@"===字符串===%@",unicodeString); return unicodeString; } //普通字符串转换为十六进制 -+ (NSString *)ConvertStringToHexString:(NSString *)string{ ++ (NSString *)ConvertStringToHexString:(NSString *)string { NSData *myD = [string dataUsingEncoding:NSUTF8StringEncoding]; Byte *bytes = (Byte *)[myD bytes]; //下面是Byte 转换为16进制。 NSString *hexStr=@""; - for(int i=0;i<[myD length];i++) - { + for (int i=0;i<[myD length];i++) { NSString *newHexStr = [NSString stringWithFormat:@"%x",bytes[i]&0xff];///16进制数 - if([newHexStr length]==1) - hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; - else - hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; + if ([newHexStr length]==1) { + hexStr = [NSString stringWithFormat:@"%@0%@",hexStr,newHexStr]; + } + else{ + hexStr = [NSString stringWithFormat:@"%@%@",hexStr,newHexStr]; + } + } return hexStr; } //int转data -+(NSData *)ConvertIntToData:(int)i -{ ++ (NSData *)ConvertIntToData:(int)i { NSData *data = [NSData dataWithBytes: &i length: sizeof(i)]; return data; } //data转int -+(int)ConvertDataToInt:(NSData *)data{ ++ (int)ConvertDataToInt:(NSData *)data { int i; [data getBytes:&i length:sizeof(i)]; return i; @@ -73,8 +74,8 @@ + (NSData *)ConvertHexStringToData:(NSString *)hexString { //根据UUIDString查找CBCharacteristic -+(CBCharacteristic *)findCharacteristicFormServices:(NSMutableArray *)services - UUIDString:(NSString *)UUIDString{ ++ (CBCharacteristic *)findCharacteristicFormServices:(NSMutableArray *)services + UUIDString:(NSString *)UUIDString { for (CBService *s in services) { for (CBCharacteristic *c in s.characteristics) { if ([c.UUID.UUIDString isEqualToString:UUIDString]) { @@ -85,8 +86,6 @@ +(CBCharacteristic *)findCharacteristicFormServices:(NSMutableArray *)services return nil; } - - @end diff --git a/Classes/objc/Babysister.m b/Classes/objc/Babysister.m deleted file mode 100644 index bbae89e..0000000 --- a/Classes/objc/Babysister.m +++ /dev/null @@ -1,472 +0,0 @@ -/* - BabyBluetooth - 简单易用的蓝牙ble库,基于CoreBluetooth 作者:刘彦玮 - https://github.com/coolnameismy/BabyBluetooth - */ - -// Created by 刘彦玮 on 15/7/30. -// Copyright (c) 2015年 刘彦玮. All rights reserved. -// - -#import "Babysister.h" -#import "BabyCallback.h" - -@implementation Babysister - -#define currChannel [babySpeaker callbackOnCurrChannel] - - --(instancetype)init{ - self = [super init]; - if(self){ - - -#if __IPHONE_OS_VERSION_MIN_REQUIRED > __IPHONE_6_0 - NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: - //蓝牙power没打开时alert提示框 - [NSNumber numberWithBool:YES],CBCentralManagerOptionShowPowerAlertKey, - //重设centralManager恢复的IdentifierKey - @"babyBluetoothRestore",CBCentralManagerOptionRestoreIdentifierKey, - nil]; - -#else - NSDictionary *options = [NSDictionary dictionaryWithObjectsAndKeys: - //蓝牙power没打开时alert提示框 - [NSNumber numberWithBool:YES],CBCentralManagerOptionShowPowerAlertKey, - nil]; -#endif - - NSArray *backgroundModes = [[[NSBundle mainBundle] infoDictionary]objectForKey:@"UIBackgroundModes"]; - if ([backgroundModes containsObject:@"bluetooth-central"]) { - //后台模式 - bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil options:options]; - }else{ - //非后台模式 - bleManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil]; - } - - //pocket - pocket = [[NSMutableDictionary alloc]init]; - connectedPeripherals = [[NSMutableArray alloc]init]; - - - //监听通知 - [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(scanForPeripheralNotifyReceived:) name:@"scanForPeripherals" object:nil]; - [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(didDiscoverPeripheralNotifyReceived:) name:@"didDiscoverPeripheral" object:nil]; - [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(connectToPeripheralNotifyReceived:) name:@"connectToPeripheral" object:nil]; - - } - return self; - -} - - - -#pragma mark -接收到通知 - -//开始扫描 --(void)scanForPeripheralNotifyReceived:(NSNotification *)notify{ -// NSLog(@">>>scanForPeripheralsNotifyReceived"); -} - -//扫描到设备 --(void)didDiscoverPeripheralNotifyReceived:(NSNotification *)notify{ -// CBPeripheral *peripheral =[notify.userInfo objectForKey:@"peripheral"]; -// NSLog(@">>>didDiscoverPeripheralNotifyReceived:%@",peripheral.name); -} - -//开始连接设备 --(void)connectToPeripheralNotifyReceived:(NSNotification *)notify{ -// NSLog(@">>>connectToPeripheralNotifyReceived"); -} - -//扫描Peripherals --(void)scanPeripherals{ - [bleManager scanForPeripheralsWithServices:[currChannel babyOptions].scanForPeripheralsWithServices options:[currChannel babyOptions].scanForPeripheralsWithOptions]; -} -//连接Peripherals --(void)connectToPeripheral:(CBPeripheral *)peripheral{ - [bleManager connectPeripheral:peripheral options:[currChannel babyOptions].connectPeripheralWithOptions]; -} - - -//断开设备连接 --(void)cancelPeripheralConnection:(CBPeripheral *)peripheral{ - [bleManager cancelPeripheralConnection:peripheral]; -} - -//断开所有已连接的设备 --(void)cancelAllPeripheralsConnection{ - for (int i=0;i>>CBCentralManagerStateUnknown"); - break; - case CBCentralManagerStateResetting: - NSLog(@">>>CBCentralManagerStateResetting"); - break; - case CBCentralManagerStateUnsupported: - NSLog(@">>>CBCentralManagerStateUnsupported"); - break; - case CBCentralManagerStateUnauthorized: - NSLog(@">>>CBCentralManagerStateUnauthorized"); - break; - case CBCentralManagerStatePoweredOff: - NSLog(@">>>CBCentralManagerStatePoweredOff"); - break; - case CBCentralManagerStatePoweredOn: - NSLog(@">>>CBCentralManagerStatePoweredOn"); - //发送centralManagerDidUpdateState通知 - [[NSNotificationCenter defaultCenter]postNotificationName:@"CBCentralManagerStatePoweredOn" object:nil]; - break; - default: - break; - } - //状态改变callback - if([currChannel blockOnCentralManagerDidUpdateState]){ - [currChannel blockOnCentralManagerDidUpdateState](central); - } -} - -- (void)centralManager:(CBCentralManager *)central willRestoreState:(NSDictionary *)dict{ - -} - -//扫描到Peripherals -- (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI -{ - - - //日志 - //NSLog(@"当扫描到设备:%@",peripheral.name); - - //发出通知 - [[NSNotificationCenter defaultCenter]postNotificationName:@"didDiscoverPeripheral" - object:nil - userInfo:@{@"central":central,@"peripheral":peripheral,@"advertisementData":advertisementData,@"RSSI":RSSI}]; - - //扫描到设备callback - if([currChannel blockOnDiscoverPeripherals]){ - if ([currChannel filterOnDiscoverPeripherals](peripheral.name)) { - [[babySpeaker callbackOnCurrChannel] blockOnDiscoverPeripherals](central,peripheral,advertisementData,RSSI); - } - } - - //处理连接设备 - if(needConnectPeripheral){ - if ([currChannel filterOnConnetToPeripherals](peripheral.name)) { - [bleManager connectPeripheral:peripheral options:[currChannel babyOptions].connectPeripheralWithOptions]; - //开一个定时器监控连接超时的情况 - connectTimer = [NSTimer scheduledTimerWithTimeInterval:5.0f target:self selector:@selector(disconnect:) userInfo:peripheral repeats:NO]; - } - } -} - -//停止扫描 --(void)disconnect:(id)sender{ - [bleManager stopScan]; -} - -//连接到Peripherals-成功 -- (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral *)peripheral -{ - - //设置委托 - [peripheral setDelegate:self]; - - //NSLog(@">>>连接到名称为(%@)的设备-成功",peripheral.name); - [connectTimer invalidate];//停止时钟 - [self addPeripheral:peripheral]; - - //执行回叫 - //扫描到设备callback - if([currChannel blockOnConnectedPeripheral]){ - [currChannel blockOnConnectedPeripheral](central,peripheral); - } - - //扫描外设的服务 - if (needDiscoverServices) { - [peripheral discoverServices:[currChannel babyOptions].discoverWithServices]; - //discoverIncludedServices - } - -} - -//连接到Peripherals-失败 --(void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error -{ -// NSLog(@">>>连接到名称为(%@)的设备-失败,原因:%@",[peripheral name],[error localizedDescription]); - if ([currChannel blockOnFailToConnect]) { - [currChannel blockOnFailToConnect](central,peripheral,error); - } -} - -//Peripherals断开连接 -- (void)centralManager:(CBCentralManager *)central didDisconnectPeripheral:(CBPeripheral *)peripheral error:(NSError *)error{ -// NSLog(@">>>外设连接断开连接 %@: %@\n", [peripheral name], [error localizedDescription]); - if (error) - { - NSLog(@">>> didDisconnectPeripheral for %@ with error: %@", peripheral.name, [error localizedDescription]); - } - - [self deletePeripheral:peripheral]; - if ([currChannel blockOnDisconnect]) { - [currChannel blockOnDisconnect](central,peripheral,error); - } - - //判断是否全部链接都已经段开 - if ([self findConnectedPeripherals].count == 0) { - //停止扫描callback - if([currChannel blockOnCancelAllPeripheralsConnection]){ - [currChannel blockOnCancelAllPeripheralsConnection](bleManager); - } - // NSLog(@">>> stopConnectAllPerihperals"); - } -} - -//扫描到服务 -- (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error -{ - - -// NSLog(@">>>扫描到服务:%@",peripheral.services); - if (error) - { - NSLog(@">>>didDiscoverServices for %@ with error: %@", peripheral.name, [error localizedDescription]); -// return; - } - //回叫block - if ([currChannel blockOnDiscoverServices]) { - [currChannel blockOnDiscoverServices](peripheral,error); - } - - //discover characteristics - if (needDiscoverCharacteristics) { - for (CBService *service in peripheral.services) { - [peripheral discoverCharacteristics:[currChannel babyOptions].discoverWithCharacteristics forService:service]; - } - } -} - - -//发现服务的Characteristics --(void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService *)service error:(NSError *)error{ - - if (error) - { - NSLog(@"error didDiscoverCharacteristicsForService for %@ with error: %@", service.UUID, [error localizedDescription]); -// return; - } - //回叫block - if ([currChannel blockOnDiscoverCharacteristics]) { - [currChannel blockOnDiscoverCharacteristics](peripheral,service,error); - } - - //如果需要更新Characteristic的值 - if (needReadValueForCharacteristic) { - for (CBCharacteristic *characteristic in service.characteristics) - { - [peripheral readValueForCharacteristic:characteristic]; - } - } - - //如果搜索Characteristic的Descriptors - if (needDiscoverDescriptorsForCharacteristic) { - for (CBCharacteristic *characteristic in service.characteristics) - { - [peripheral discoverDescriptorsForCharacteristic:characteristic]; - } - } -} - -//读取Characteristics的值 --(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ - - - if (error) - { - NSLog(@"error didUpdateValueForCharacteristic %@ with error: %@", characteristic.UUID, [error localizedDescription]); -// return; - } - //查找字段订阅 - if([babySpeaker notifyCallback:characteristic]){ - [babySpeaker notifyCallback:characteristic](peripheral,characteristic,error); - return; - } - //回叫block - if ([currChannel blockOnReadValueForCharacteristic]) { - [currChannel blockOnReadValueForCharacteristic](peripheral,characteristic,error); - } - -} -//发现Characteristics的Descriptors --(void)peripheral:(CBPeripheral *)peripheral didDiscoverDescriptorsForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ - - - if (error) - { - NSLog(@"error Discovered DescriptorsForCharacteristic for %@ with error: %@", characteristic.UUID, [error localizedDescription]); -// return; - } - //回叫block - if ([currChannel blockOnDiscoverDescriptorsForCharacteristic]) { - [currChannel blockOnDiscoverDescriptorsForCharacteristic](peripheral,characteristic,error); - } - //如果需要更新Characteristic的Descriptors - if (needReadValueForDescriptors) { - for (CBDescriptor *d in characteristic.descriptors) - { - [peripheral readValueForDescriptor:d]; - } - } - - //执行一次的方法 - if (oneReadValueForDescriptors) { - for (CBDescriptor *d in characteristic.descriptors) - { - [peripheral readValueForDescriptor:d]; - } - oneReadValueForDescriptors = NO; - } -} - -//读取Characteristics的Descriptors的值 --(void)peripheral:(CBPeripheral *)peripheral didUpdateValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{ - - - if (error) - { - NSLog(@"error didUpdateValueForDescriptor for %@ with error: %@", descriptor.UUID, [error localizedDescription]); -// return; - } - //回叫block - if ([currChannel blockOnReadValueForDescriptors]) { - [currChannel blockOnReadValueForDescriptors](peripheral,descriptor,error); - } - -} - --(void)peripheral:(CBPeripheral *)peripheral didWriteValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ -// NSLog(@">>>didWriteValueForCharacteristic"); -// NSLog(@">>>uuid:%@,new value:%@",characteristic.UUID,characteristic.value); - if ([currChannel blockOnDidWriteValueForCharacteristic]) { - [currChannel blockOnDidWriteValueForCharacteristic](characteristic,error); - } -} - --(void)peripheral:(CBPeripheral *)peripheral didWriteValueForDescriptor:(CBDescriptor *)descriptor error:(NSError *)error{ -// NSLog(@">>>didWriteValueForCharacteristic"); -// NSLog(@">>>uuid:%@,new value:%@",descriptor.UUID,descriptor.value); - if ([currChannel blockOnDidWriteValueForDescriptor]) { - [currChannel blockOnDidWriteValueForDescriptor](descriptor,error); - } -} - -//characteristic.isNotifying 状态改变 --(void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error{ - NSLog(@">>>didUpdateNotificationStateForCharacteristic"); - NSLog(@">>>uuid:%@,isNotifying:%@",characteristic.UUID,characteristic.isNotifying?@"isNotifying":@"Notifying"); - if ([currChannel blockOnDidUpdateNotificationStateForCharacteristic]) { - [currChannel blockOnDidUpdateNotificationStateForCharacteristic](characteristic,error); - } -} - --(void)peripheral:(CBPeripheral *)peripheral didDiscoverIncludedServicesForService:(CBService *)service error:(NSError *)error{ - if ([currChannel blockOnDidDiscoverIncludedServicesForService]) { - [currChannel blockOnDidDiscoverIncludedServicesForService](service,error); - } -} - -//-(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error{ -// NSLog(@"peripheralDidUpdateRSSI -> RSSI:%@",RSSI); -// if ([currChannel blockOnDidReadRSSI]) { -// [currChannel blockOnDidReadRSSI](RSSI,error); -// } -//} - - - -//- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(NSError *)error{ -// NSLog(@"peripheralDidUpdateRSSI -> RSSI:%@",peripheral.RSSI); -// if ([currChannel blockOnDidReadRSSI]) { -// [currChannel blockOnDidReadRSSI](peripheral.RSSI,error); -// } -//} - -#if __IPHONE_OS_VERSION_MIN_REQUIRED < __IPHONE_8_0 -- (void)peripheralDidUpdateRSSI:(CBPeripheral *)peripheral error:(nullable NSError *)error{ - NSLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",peripheral.RSSI); - if ([currChannel blockOnDidReadRSSI]) { - [currChannel blockOnDidReadRSSI](peripheral.RSSI,error); - } -} -#else --(void)peripheral:(CBPeripheral *)peripheral didReadRSSI:(NSNumber *)RSSI error:(NSError *)error{ - NSLog(@">>>peripheralDidUpdateRSSI -> RSSI:%@",RSSI); - if ([currChannel blockOnDidReadRSSI]) { - [currChannel blockOnDidReadRSSI](RSSI,error); - } -} -#endif - --(void)peripheralDidUpdateName:(CBPeripheral *)peripheral{ - if ([currChannel blockOnDidUpdateName]) { - [currChannel blockOnDidUpdateName](peripheral); - } -} - --(void)peripheral:(CBPeripheral *)peripheral didModifyServices:(NSArray *)invalidatedServices{ - if ([currChannel blockOnDidModifyServices]) { - [currChannel blockOnDidModifyServices](peripheral,invalidatedServices); - } -} - - - - -#pragma mark -私有方法 - -#pragma mark -设备list管理 - --(void)addPeripheral:(CBPeripheral *)peripheral{ - if (![connectedPeripherals containsObject:peripheral]) { - [connectedPeripherals addObject:peripheral]; - } -} - --(void)deletePeripheral:(CBPeripheral *)peripheral{ - [connectedPeripherals removeObject:peripheral]; -} - --(CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName{ - for (CBPeripheral *p in connectedPeripherals) { - if (p.name == peripheralName) { - return p; - } - } - return nil; -} - --(NSArray *)findConnectedPeripherals{ - return connectedPeripherals; -} - - -@end diff --git a/README.md b/README.md index cb113c3..f790058 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ -![](http://images.jumppo.com/uploads/BabyBluetooth_logo.png) +![](logo.png) The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的蓝牙库,基于CoreBluetooth的封装,并兼容ios和mac osx. @@ -11,10 +11,14 @@ The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的 - 4:通过channel切换区分委托调用,并方便切换 - 5:便利的工具方法 - 6:完善的文档,且项目处于活跃状态,不断的更新中 -- 7:github上star最多的纯Bluetooch类库(非PhoneGap和SensorTag项目) +- 7:github上star最多的纯Bluetooth类库 - 8:包含多种类型的demo和ios蓝牙开发教程 +- 9:同时支持蓝牙设备中心模式和外设模式(central model and peripheral model) + +当前版本 0.7.0 + +[更新说明](https://github.com/coolnameismy/BabyBluetooth/wiki/%E6%9B%B4%E6%96%B0%E8%AF%B4%E6%98%8E) -当前版本 v0.4.0 详细文档请参考wiki The full documentation of the project is available on its wiki. # [english readme link,please click it!](https://github.com/coolnameismy/BabyBluetooth/blob/master/README_en.md) @@ -29,8 +33,13 @@ The easiest way to use Bluetooth (BLE )in ios,even bady can use. 简单易用的 * [后期更新](#后期更新) * [蓝牙学习资源](#蓝牙学习资源) * [期待](#期待) +* [常见问题](https://github.com/coolnameismy/BabyBluetooth/issues) # QuickExample + +## 中心模式 central model +> app作为中心,连接其他BLE4.0外设 + ```objc //导入.h文件和系统蓝牙库的头文件 @@ -59,16 +68,90 @@ BabyBluetooth *baby; //过滤器 //设置查找设备的过滤器 - [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) { + [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + //最常用的场景是查找某一个前缀开头的设备 most common usage is discover for peripheral that name has common prefix + //if ([peripheralName hasPrefix:@"Pxxxx"] ) { + // return YES; + //} + //return NO; //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1 if (peripheralName.length >1) { return YES; } return NO; }]; + + //....... } - + ``` + +更多蓝牙操作方法和委托请参考[wiki](https://github.com/coolnameismy/BabyBluetooth/wiki) + +中心模式使用示例请参考:[BabyBluetoothAppDemo](https://github.com/coolnameismy//BabyBluetoothExamples/BabyBluetoothAppDemo) + +## 外设模式 peripheral model +> app模拟一个,BLE4.0外设,可以被其他设备连接和使用 + + +模拟一个有2个service和6个characteristic的外设 + +````objc +//导入.h文件和系统蓝牙库的头文件 +#import "BabyBluetooth.h" +//定义变量 +BabyBluetooth *baby; + +-(void)viewDidLoad { + [super viewDidLoad]; + + //配置第一个服务s1 + CBMutableService *s1 = makeCBService(@"FFF0"); + //配置s1的3个characteristic + makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//读 + makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//写 + makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//可读写,uuid自动生成 + makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段 + makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段 + //配置第一个服务s2 + CBMutableService *s2 = makeCBService(@"FFE0"); + makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读 + + //实例化baby + baby = [BabyBluetooth shareBabyBluetooth]; + //配置委托 + [self babyDelegate]; + //启动外设 + baby.bePeripheral().addServices(@[s1,s2]).startAdvertising(); +} + +//设置蓝牙外设模式的委托 +-(void)babyDelegate{ + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) { + NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) { + NSLog(@"didStartAdvertising !!!"); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) { + NSLog(@"Did Add Service uuid: %@ ",service.UUID); + }]; + + //..... +} + +```` + +更多蓝牙外设模式委托请参考[wiki](https://github.com/coolnameismy/BabyBluetooth/wiki) + +中心模式使用示例请参考:[BluetoothStubOnIOS](https://github.com/coolnameismy//BabyBluetoothExamples/BluetoothStubOnIOS) + # 如何安装 ##1 手动安装 @@ -83,7 +166,7 @@ step2:导入.h文件 ##2 cocoapods step1:add the following line to your Podfile: ```` -pod 'BabyBluetooth','~> 0.4.0' +pod 'BabyBluetooth','~> 0.7.0' ```` step2:导入.h文件 @@ -96,7 +179,7 @@ step2:导入.h文件 # 示例程序说明 -**BabyBluetoothExamples/BabyBluetoothAppDemo** :一个类似lightblue的程序,蓝牙操作全部使用BabyBluetooch完成。 +**BabyBluetoothExamples/BabyBluetoothAppDemo** :一个类似lightblue的程序,蓝牙操作全部使用BabyBluetooth完成。 功能: - 1:扫描周围设备 - 2:连接设备,扫描设备的全部services和characteristic @@ -104,6 +187,9 @@ step2:导入.h文件 - 4:写0x01到characteristic - 5:订阅/取消订阅 characteristic的notify +**BabyBluetoothExamples/BluetoothStubOnIOS** : 一个iOS程序,启动后会用手机模拟一个外设,提供2个服务和若干characteristic。 +该程序作为Babybluetooth 外设模式使用的示例程序 + **BabyBluetoothExamples/BabyBluetoothOSDemo** :一个mac os程序,因为os和ios的蓝牙底层方法都一样,所以BabyBluetooth可以ios/os通用。但是os程序有个好处就是直接可以在mac上跑蓝牙设备,不像ios,必须要真机才能跑蓝牙设备。所以不能真机调试时可以使用os尝试蓝牙库的使用。 功能: @@ -111,29 +197,30 @@ step2:导入.h文件 **BabyBluetoothExamples/BluetoothStubOnOSX** :一个mac os程序,该程序可以作为蓝牙外设使用,解决学习蓝牙时没有外设可用的囧境,并且可以作为peripheral model模式的学习示例。改程序用swift编码。 + + + 功能: - 1:作为蓝牙外设使用,可以被发现,连接,读写,订阅 - 2:提供1个service,包含了3个characteristic,分别具有读、读写、订阅功能 # 兼容性 -- 蓝牙4.0,也叫做ble,ios6以上可以自由使用。 +- 蓝牙4.0,也叫做ble,ios6以上和iPhone4s以上可以自由使用 - os和ios通用 - 蓝牙设备相关程序必须使用真机才能运行。如果不能使用真机调试的情况,可以使用os程序调试蓝牙。可以参考示例程序中的BabyBluetoothOSDemo - 本项目和示例程序是使用ios 8.3开发,使用者可以自行降版本,但必须大于6.0 - # 后期更新 -- 增加babytooth对NSNotification事件的支持 -- 完善代码的中英文注释 -- 增加对外设模式使用的支持(app作为蓝牙设备提供服务) -- 优化babyBluetooch的子类类名 + - 增加对Carthage Install的支持 -- swift版本开发 +- [swift版本开发,目前项目已经兼容swift,详细见这里](https://github.com/coolnameismy/BabyBluetooth-swift) 已经更新的版本说明,请在wiki中查看 # 蓝牙学习资源 +> 温馨提示:零基础做蓝牙开发很困难,所以就算使用babybluetooth降低了代码量,仍然任很有必要花上几天时间把蓝牙的基础概念弄明白后才开始动手~ + - [ios蓝牙开发(一)蓝牙相关基础知识](http://liuyanwei.jumppo.com/2015/07/17/ios-BLE-1.html) - [ios蓝牙开发(二)蓝牙中心模式的ios代码实现](http://liuyanwei.jumppo.com/2015/08/14/ios-BLE-2.html) - [ios蓝牙开发(三)app作为外设被连接的实现](http://liuyanwei.jumppo.com/2015/09/07/ios-BLE-3.html) @@ -141,13 +228,30 @@ step2:导入.h文件 - 暂未完成-ios蓝牙开发(五)BabyBluetooth实现原理 - 待定... - [官方CoreBuetooth支持页](https://developer.apple.com/bluetooth) +- [Bluetooth Accessory Design Guidelines for Apple Products](https://developer.apple.com/hardwaredrivers/BluetoothDesignGuidelines.pdf) +- [知乎live:一小时蓝牙应用开发科普 文字整理](http://liuyanwei.jumppo.com/2017/01/23/zhihu-live-a-hour-for-bluetooth-0.html) + +# 组织和交流 +- qq交流群6: 284341984(满) +- qq交流群5: 426082944(满) +- qq交流群4: 313084771(满) +- qq交流群3:530142592(满) +- qq交流群2:168756967(满) +- qq交流群1:426603940(满) +- 个人微信公众号 -qq交流群:426603940 +![个人微信公众号](qrcode.jpg) + +# 招聘 + +  阿里云-iot事业部招人,p6 p7 前端,android,iOS,风光无限 一起造作,有兴趣的投简历到 coolnameismy@gmail.com # 期待 - - 蓝牙库写起来很辛苦,希望大家可以多多支持,多多star + + - 蓝牙库写起来很辛苦,不要忘记点击右上角小星星star和[follow](https://github.com/coolnameismy)支持一下~ - 如果在使用过程中遇到BUG,或发现功能不够用,希望你能Issues我,谢谢 + - 如果你的app用到了Babybluetooth,希望你能发邮件([coolnameismy@hotmail.com](mailto:coolnameismy@hotmail.com?subject=Babybluetooth用在的了我们的app中))告诉我,我会在readme中展现出来。 - 期待大家也能一起为BabyBluetooth输出代码,这里我只是给BabyBluetooth开了个头,他可以增加和优化的地方还是非常多。也期待和大家在Pull Requests一起学习,交流,成长。 - - 谁知道ios使用蓝牙进行固件升级的解决方法,请联系我。 + + - diff --git a/README_en.md b/README_en.md index 9137585..573aa3f 100644 --- a/README_en.md +++ b/README_en.md @@ -1,24 +1,28 @@ ![](http://images.jumppo.com/uploads/BabyBluetooth_logo.png) -The easiest way to use Bluetooth (BLE )in ios,even bady can use . CoreBluetooth wrap. +The easiest way to use Bluetooth (BLE )in ios,even baby can use . CoreBluetooth wrap. #feature - 1:CoreBluetooth wrap,simple and eary for use. - 2:CoreBluetooth is dependency on delegate ,and most times,call method at delegate then go into delegate,and over and over,it's messy.BabyBluetooth favor to using block. -- 3:call methor in a serial,it's simple and graceful. +- 3:call methor in a serial,it's simple and elegant syntax. - 4:using channel switch blcoks in a group. - 5:convenience tools class - 6:comprehensive documentation and active project - 7:more star library for ios bluetooch in github(not PhoneGap and SensorTag) - 8:include demo and tutorial +- 9:works with both central model and peripheral model -current verison v0.4.0 +current verison 0.7.0 + +[update histroy](https://github.com/coolnameismy/BabyBluetooth/wiki/verision) + +![](.res/qrcode.jpg) # Contents - * [QuickExample](#user-content-QuickExample) * [how to install](#user-content-how-to-install) * [how to use](#user-content-how-to-use) @@ -29,6 +33,10 @@ current verison v0.4.0 * [wish](#user-content-wish) # QuickExample + +## central model +> let you apps be Central and cotact other BLE4.0 peripheral + ```objc //import head files @@ -57,7 +65,12 @@ BabyBluetooth *baby; //filter //discover peripherals filter - [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName) { + [baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + //最常用的场景是查找某一个前缀开头的设备 most common usage is discover for peripheral that name has common prefix + //if ([peripheralName hasPrefix:@"Pxxxx"] ) { + // return YES; + //} + //return NO; //设置查找规则是名称大于1 , the search rule is peripheral.name length > 1 if (peripheralName.length >1) { return YES; @@ -67,6 +80,73 @@ BabyBluetooth *baby; } ``` +more method and delegate please see : [wiki](https://github.com/coolnameismy/BabyBluetooth/wiki) + +Central model deme see:[BabyBluetoothAppDemo](https://github.com/coolnameismy//BabyBluetoothExamples/BabyBluetoothAppDemo) + + +## peripheral model +> let you apps be a BLE4.0 peripheral + + +mock a peripheral which has 2 service and 6 characteristic together + +````objc +//import head files +#import "BabyBluetooth.h" +//define var +BabyBluetooth *baby; + +-(void)viewDidLoad { + [super viewDidLoad]; + + //config first service + CBMutableService *s1 = makeCBService(@"FFF0"); + //config s1‘s characteristic + makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//can read + makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//can write + makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//can read,write ,uuid be automatically generate + makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//default property is rw + makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//can notiy + //config seconed service s2 + CBMutableService *s2 = makeCBService(@"FFE0"); + //a static characteristic and has cached vuale ,it must be only can read. + makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]); + + //init BabyBluetooth + baby = [BabyBluetooth shareBabyBluetooth]; + //config delegate + [self babyDelegate]; + //let peripheral add services and start advertising + baby.bePeripheral().addServices(@[s1,s2]).startAdvertising(); +} + +//set baby peripheral model delegate +-(void)babyDelegate{ + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) { + NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) { + NSLog(@"didStartAdvertising !!!"); + }]; + + //设置添加service委托 | set didAddService block + [baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) { + NSLog(@"Did Add Service uuid: %@ ",service.UUID); + }]; + + //..... +} + +```` + +more method and delegate please see : [wiki](https://github.com/coolnameismy/BabyBluetooth/wiki) + +peripheral model demo see :[BluetoothStubOnIOS](https://github.com/coolnameismy//BabyBluetoothExamples/BluetoothStubOnIOS) # how to install @@ -82,7 +162,8 @@ step2:import .h ##2 cocoapods step1:add the following line to your Podfile: ```` -pod 'BabyBluetooth','~> 0.4.0' +pod 'BabyBluetooth','~> 0.6.0' + ```` step2:import header files @@ -104,6 +185,9 @@ functionality - 4:write 0x01 to characteristic - 5:subscription/unsubscription characteristic +**BabyBluetoothExamples/BluetoothStubOnIOS** : a iOS apps, when they launch, they will start a BLE4,0 peripheral and privide two services and together six charactistic . it's Babybluetooth peripheral model develope example + + **BabyBluetoothExamples/BabyBluetoothOSDemo** :mac osx app,osx and ios is not diffent on CoreBluetooth,so BabyBluetooth can use in both ios and osx 。 functionality - 1:scanfor peripheral, conncet peripheral 、read characteristic,read characteristic 's value,discover descriptors and descriptors's value,the message all in nslog,this app none UI @@ -121,9 +205,7 @@ functionality # plan for update -- add support for NSNotification event in babyBluetooth - improve englist code note -- add support for peripheralManager(let app be a peripheral!) - babybluetooth test application - swift babybluetooth develop @@ -134,6 +216,8 @@ QQ Group Number:168756967. [QQ is a IM from china](http://im.qq.com/) Or Mail me:coolnameismy@hotmail.com +*nearst update:* be support for BLE peripheral model + # wish - coding BabyBluetooth is work hard , wish audience star BabyBluetooch for support diff --git a/Test/BabyTestProject/BabyTestProject.xcodeproj/project.pbxproj b/Test/BabyTestProject/BabyTestProject.xcodeproj/project.pbxproj new file mode 100644 index 0000000..0c08fb8 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject.xcodeproj/project.pbxproj @@ -0,0 +1,509 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + 79B996211CC7C0820016358F /* BabybluetoothNotificationTest.m in Sources */ = {isa = PBXBuildFile; fileRef = 79B996201CC7C0820016358F /* BabybluetoothNotificationTest.m */; }; + 79B996251CC86CEF0016358F /* BabyDefine.m in Sources */ = {isa = PBXBuildFile; fileRef = 79B996231CC7C13D0016358F /* BabyDefine.m */; }; + E1FAF0521C96F76900BD7054 /* BabyTestExpretaion.m in Sources */ = {isa = PBXBuildFile; fileRef = E1FAF0511C96F76900BD7054 /* BabyTestExpretaion.m */; }; + F0AF1DC41C92A4D100FD1717 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1DC31C92A4D100FD1717 /* main.m */; }; + F0AF1DC71C92A4D100FD1717 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1DC61C92A4D100FD1717 /* AppDelegate.m */; }; + F0AF1DCA1C92A4D100FD1717 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1DC91C92A4D100FD1717 /* ViewController.m */; }; + F0AF1DCD1C92A4D100FD1717 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F0AF1DCB1C92A4D100FD1717 /* Main.storyboard */; }; + F0AF1DCF1C92A4D100FD1717 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F0AF1DCE1C92A4D100FD1717 /* Assets.xcassets */; }; + F0AF1DD21C92A4D100FD1717 /* LaunchScreen.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F0AF1DD01C92A4D100FD1717 /* LaunchScreen.storyboard */; }; + F0AF1DDD1C92A4D100FD1717 /* BabyTestProjectTests.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1DDC1C92A4D100FD1717 /* BabyTestProjectTests.m */; }; + F0AF1E1F1C92B03A00FD1717 /* BabyBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E101C92B03A00FD1717 /* BabyBluetooth.m */; }; + F0AF1E201C92B03A00FD1717 /* BabyCallback.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E121C92B03A00FD1717 /* BabyCallback.m */; }; + F0AF1E211C92B03A00FD1717 /* BabyCentralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E141C92B03A00FD1717 /* BabyCentralManager.m */; }; + F0AF1E221C92B03A00FD1717 /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E161C92B03A00FD1717 /* BabyOptions.m */; }; + F0AF1E231C92B03A00FD1717 /* BabyPeripheralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E181C92B03A00FD1717 /* BabyPeripheralManager.m */; }; + F0AF1E241C92B03A00FD1717 /* BabyRhythm.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E1A1C92B03A00FD1717 /* BabyRhythm.m */; }; + F0AF1E251C92B03A00FD1717 /* BabySpeaker.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E1C1C92B03A00FD1717 /* BabySpeaker.m */; }; + F0AF1E261C92B03A00FD1717 /* BabyToy.m in Sources */ = {isa = PBXBuildFile; fileRef = F0AF1E1E1C92B03A00FD1717 /* BabyToy.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXContainerItemProxy section */ + F0AF1DD91C92A4D100FD1717 /* PBXContainerItemProxy */ = { + isa = PBXContainerItemProxy; + containerPortal = F0AF1DB71C92A4D100FD1717 /* Project object */; + proxyType = 1; + remoteGlobalIDString = F0AF1DBE1C92A4D100FD1717; + remoteInfo = BabyTestProject; + }; +/* End PBXContainerItemProxy section */ + +/* Begin PBXFileReference section */ + 79B996201CC7C0820016358F /* BabybluetoothNotificationTest.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabybluetoothNotificationTest.m; sourceTree = ""; }; + 79B996221CC7C13D0016358F /* BabyDefine.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyDefine.h; sourceTree = ""; }; + 79B996231CC7C13D0016358F /* BabyDefine.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyDefine.m; sourceTree = ""; }; + E1FAF0501C96F76900BD7054 /* BabyTestExpretaion.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyTestExpretaion.h; sourceTree = ""; }; + E1FAF0511C96F76900BD7054 /* BabyTestExpretaion.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyTestExpretaion.m; sourceTree = ""; }; + F0AF1DBF1C92A4D100FD1717 /* BabyTestProject.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BabyTestProject.app; sourceTree = BUILT_PRODUCTS_DIR; }; + F0AF1DC31C92A4D100FD1717 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + F0AF1DC51C92A4D100FD1717 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + F0AF1DC61C92A4D100FD1717 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + F0AF1DC81C92A4D100FD1717 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; + F0AF1DC91C92A4D100FD1717 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; + F0AF1DCC1C92A4D100FD1717 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + F0AF1DCE1C92A4D100FD1717 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + F0AF1DD11C92A4D100FD1717 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/LaunchScreen.storyboard; sourceTree = ""; }; + F0AF1DD31C92A4D100FD1717 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + F0AF1DD81C92A4D100FD1717 /* BabyTestProjectTests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = BabyTestProjectTests.xctest; sourceTree = BUILT_PRODUCTS_DIR; }; + F0AF1DDC1C92A4D100FD1717 /* BabyTestProjectTests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BabyTestProjectTests.m; sourceTree = ""; }; + F0AF1DDE1C92A4D100FD1717 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + F0AF1DE71C92A4D100FD1717 /* BabyTestProjectUITests.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = BabyTestProjectUITests.m; sourceTree = ""; }; + F0AF1DE91C92A4D100FD1717 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + F0AF1E0F1C92B03A00FD1717 /* BabyBluetooth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyBluetooth.h; sourceTree = ""; }; + F0AF1E101C92B03A00FD1717 /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyBluetooth.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0AF1E111C92B03A00FD1717 /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCallback.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + F0AF1E121C92B03A00FD1717 /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCallback.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0AF1E131C92B03A00FD1717 /* BabyCentralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCentralManager.h; sourceTree = ""; }; + F0AF1E141C92B03A00FD1717 /* BabyCentralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCentralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0AF1E151C92B03A00FD1717 /* BabyOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyOptions.h; sourceTree = ""; }; + F0AF1E161C92B03A00FD1717 /* BabyOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyOptions.m; sourceTree = ""; }; + F0AF1E171C92B03A00FD1717 /* BabyPeripheralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyPeripheralManager.h; sourceTree = ""; }; + F0AF1E181C92B03A00FD1717 /* BabyPeripheralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyPeripheralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0AF1E191C92B03A00FD1717 /* BabyRhythm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyRhythm.h; sourceTree = ""; }; + F0AF1E1A1C92B03A00FD1717 /* BabyRhythm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyRhythm.m; sourceTree = ""; }; + F0AF1E1B1C92B03A00FD1717 /* BabySpeaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabySpeaker.h; sourceTree = ""; }; + F0AF1E1C1C92B03A00FD1717 /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabySpeaker.m; sourceTree = ""; }; + F0AF1E1D1C92B03A00FD1717 /* BabyToy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyToy.h; sourceTree = ""; }; + F0AF1E1E1C92B03A00FD1717 /* BabyToy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyToy.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F0AF1DBC1C92A4D100FD1717 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F0AF1DD51C92A4D100FD1717 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F0AF1DB61C92A4D100FD1717 = { + isa = PBXGroup; + children = ( + F0AF1E0E1C92B03A00FD1717 /* BabyBluetooth */, + F0AF1DC11C92A4D100FD1717 /* BabyTestProject */, + F0AF1DDB1C92A4D100FD1717 /* BabyTestProjectTests */, + F0AF1DE61C92A4D100FD1717 /* BabyTestProjectUITests */, + F0AF1DC01C92A4D100FD1717 /* Products */, + ); + sourceTree = ""; + }; + F0AF1DC01C92A4D100FD1717 /* Products */ = { + isa = PBXGroup; + children = ( + F0AF1DBF1C92A4D100FD1717 /* BabyTestProject.app */, + F0AF1DD81C92A4D100FD1717 /* BabyTestProjectTests.xctest */, + ); + name = Products; + sourceTree = ""; + }; + F0AF1DC11C92A4D100FD1717 /* BabyTestProject */ = { + isa = PBXGroup; + children = ( + F0AF1DC51C92A4D100FD1717 /* AppDelegate.h */, + F0AF1DC61C92A4D100FD1717 /* AppDelegate.m */, + F0AF1DC81C92A4D100FD1717 /* ViewController.h */, + F0AF1DC91C92A4D100FD1717 /* ViewController.m */, + F0AF1DCB1C92A4D100FD1717 /* Main.storyboard */, + F0AF1DCE1C92A4D100FD1717 /* Assets.xcassets */, + F0AF1DD01C92A4D100FD1717 /* LaunchScreen.storyboard */, + F0AF1DD31C92A4D100FD1717 /* Info.plist */, + F0AF1DC21C92A4D100FD1717 /* Supporting Files */, + ); + path = BabyTestProject; + sourceTree = ""; + }; + F0AF1DC21C92A4D100FD1717 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + F0AF1DC31C92A4D100FD1717 /* main.m */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + F0AF1DDB1C92A4D100FD1717 /* BabyTestProjectTests */ = { + isa = PBXGroup; + children = ( + 79B996201CC7C0820016358F /* BabybluetoothNotificationTest.m */, + F0AF1DDC1C92A4D100FD1717 /* BabyTestProjectTests.m */, + F0AF1DDE1C92A4D100FD1717 /* Info.plist */, + E1FAF0501C96F76900BD7054 /* BabyTestExpretaion.h */, + E1FAF0511C96F76900BD7054 /* BabyTestExpretaion.m */, + ); + path = BabyTestProjectTests; + sourceTree = ""; + }; + F0AF1DE61C92A4D100FD1717 /* BabyTestProjectUITests */ = { + isa = PBXGroup; + children = ( + F0AF1DE71C92A4D100FD1717 /* BabyTestProjectUITests.m */, + F0AF1DE91C92A4D100FD1717 /* Info.plist */, + ); + path = BabyTestProjectUITests; + sourceTree = ""; + }; + F0AF1E0E1C92B03A00FD1717 /* BabyBluetooth */ = { + isa = PBXGroup; + children = ( + 79B996221CC7C13D0016358F /* BabyDefine.h */, + 79B996231CC7C13D0016358F /* BabyDefine.m */, + F0AF1E0F1C92B03A00FD1717 /* BabyBluetooth.h */, + F0AF1E101C92B03A00FD1717 /* BabyBluetooth.m */, + F0AF1E111C92B03A00FD1717 /* BabyCallback.h */, + F0AF1E121C92B03A00FD1717 /* BabyCallback.m */, + F0AF1E131C92B03A00FD1717 /* BabyCentralManager.h */, + F0AF1E141C92B03A00FD1717 /* BabyCentralManager.m */, + F0AF1E151C92B03A00FD1717 /* BabyOptions.h */, + F0AF1E161C92B03A00FD1717 /* BabyOptions.m */, + F0AF1E171C92B03A00FD1717 /* BabyPeripheralManager.h */, + F0AF1E181C92B03A00FD1717 /* BabyPeripheralManager.m */, + F0AF1E191C92B03A00FD1717 /* BabyRhythm.h */, + F0AF1E1A1C92B03A00FD1717 /* BabyRhythm.m */, + F0AF1E1B1C92B03A00FD1717 /* BabySpeaker.h */, + F0AF1E1C1C92B03A00FD1717 /* BabySpeaker.m */, + F0AF1E1D1C92B03A00FD1717 /* BabyToy.h */, + F0AF1E1E1C92B03A00FD1717 /* BabyToy.m */, + ); + name = BabyBluetooth; + path = ../../Classes/objc; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + F0AF1DBE1C92A4D100FD1717 /* BabyTestProject */ = { + isa = PBXNativeTarget; + buildConfigurationList = F0AF1DEC1C92A4D100FD1717 /* Build configuration list for PBXNativeTarget "BabyTestProject" */; + buildPhases = ( + F0AF1DBB1C92A4D100FD1717 /* Sources */, + F0AF1DBC1C92A4D100FD1717 /* Frameworks */, + F0AF1DBD1C92A4D100FD1717 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = BabyTestProject; + productName = BabyTestProject; + productReference = F0AF1DBF1C92A4D100FD1717 /* BabyTestProject.app */; + productType = "com.apple.product-type.application"; + }; + F0AF1DD71C92A4D100FD1717 /* BabyTestProjectTests */ = { + isa = PBXNativeTarget; + buildConfigurationList = F0AF1DEF1C92A4D100FD1717 /* Build configuration list for PBXNativeTarget "BabyTestProjectTests" */; + buildPhases = ( + F0AF1DD41C92A4D100FD1717 /* Sources */, + F0AF1DD51C92A4D100FD1717 /* Frameworks */, + F0AF1DD61C92A4D100FD1717 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + F0AF1DDA1C92A4D100FD1717 /* PBXTargetDependency */, + ); + name = BabyTestProjectTests; + productName = BabyTestProjectTests; + productReference = F0AF1DD81C92A4D100FD1717 /* BabyTestProjectTests.xctest */; + productType = "com.apple.product-type.bundle.unit-test"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F0AF1DB71C92A4D100FD1717 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = liuyanwei; + TargetAttributes = { + F0AF1DBE1C92A4D100FD1717 = { + CreatedOnToolsVersion = 7.2.1; + DevelopmentTeam = QGDUF2E9QW; + }; + F0AF1DD71C92A4D100FD1717 = { + CreatedOnToolsVersion = 7.2.1; + TestTargetID = F0AF1DBE1C92A4D100FD1717; + }; + }; + }; + buildConfigurationList = F0AF1DBA1C92A4D100FD1717 /* Build configuration list for PBXProject "BabyTestProject" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = F0AF1DB61C92A4D100FD1717; + productRefGroup = F0AF1DC01C92A4D100FD1717 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F0AF1DBE1C92A4D100FD1717 /* BabyTestProject */, + F0AF1DD71C92A4D100FD1717 /* BabyTestProjectTests */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + F0AF1DBD1C92A4D100FD1717 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F0AF1DD21C92A4D100FD1717 /* LaunchScreen.storyboard in Resources */, + F0AF1DCF1C92A4D100FD1717 /* Assets.xcassets in Resources */, + F0AF1DCD1C92A4D100FD1717 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F0AF1DD61C92A4D100FD1717 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + F0AF1DBB1C92A4D100FD1717 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + 79B996251CC86CEF0016358F /* BabyDefine.m in Sources */, + F0AF1DCA1C92A4D100FD1717 /* ViewController.m in Sources */, + F0AF1E261C92B03A00FD1717 /* BabyToy.m in Sources */, + F0AF1DC71C92A4D100FD1717 /* AppDelegate.m in Sources */, + F0AF1E221C92B03A00FD1717 /* BabyOptions.m in Sources */, + F0AF1E1F1C92B03A00FD1717 /* BabyBluetooth.m in Sources */, + F0AF1E211C92B03A00FD1717 /* BabyCentralManager.m in Sources */, + F0AF1E201C92B03A00FD1717 /* BabyCallback.m in Sources */, + F0AF1DC41C92A4D100FD1717 /* main.m in Sources */, + F0AF1E251C92B03A00FD1717 /* BabySpeaker.m in Sources */, + F0AF1E241C92B03A00FD1717 /* BabyRhythm.m in Sources */, + F0AF1E231C92B03A00FD1717 /* BabyPeripheralManager.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; + F0AF1DD41C92A4D100FD1717 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + E1FAF0521C96F76900BD7054 /* BabyTestExpretaion.m in Sources */, + F0AF1DDD1C92A4D100FD1717 /* BabyTestProjectTests.m in Sources */, + 79B996211CC7C0820016358F /* BabybluetoothNotificationTest.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXTargetDependency section */ + F0AF1DDA1C92A4D100FD1717 /* PBXTargetDependency */ = { + isa = PBXTargetDependency; + target = F0AF1DBE1C92A4D100FD1717 /* BabyTestProject */; + targetProxy = F0AF1DD91C92A4D100FD1717 /* PBXContainerItemProxy */; + }; +/* End PBXTargetDependency section */ + +/* Begin PBXVariantGroup section */ + F0AF1DCB1C92A4D100FD1717 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + F0AF1DCC1C92A4D100FD1717 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; + F0AF1DD01C92A4D100FD1717 /* LaunchScreen.storyboard */ = { + isa = PBXVariantGroup; + children = ( + F0AF1DD11C92A4D100FD1717 /* Base */, + ); + name = LaunchScreen.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + F0AF1DEA1C92A4D100FD1717 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer: liu yanwei (M6RAJ5X9QT)"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = iphoneos; + }; + name = Debug; + }; + F0AF1DEB1C92A4D100FD1717 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "iPhone Developer: liu yanwei (M6RAJ5X9QT)"; + "CODE_SIGN_IDENTITY[sdk=iphoneos*]" = "iPhone Developer"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + IPHONEOS_DEPLOYMENT_TARGET = 9.0; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = iphoneos; + VALIDATE_PRODUCT = YES; + }; + name = Release; + }; + F0AF1DED1C92A4D100FD1717 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + INFOPLIST_FILE = BabyTestProject/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestProject.BabyTestProject; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F0AF1DEE1C92A4D100FD1717 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + CODE_SIGN_IDENTITY = "iPhone Developer"; + INFOPLIST_FILE = BabyTestProject/Info.plist; + IPHONEOS_DEPLOYMENT_TARGET = 8.0; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestProject.BabyTestProject; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; + F0AF1DF01C92A4D100FD1717 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + INFOPLIST_FILE = BabyTestProjectTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestProject.BabyTestProjectTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/BabyTestProject.app/BabyTestProject"; + }; + name = Debug; + }; + F0AF1DF11C92A4D100FD1717 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + BUNDLE_LOADER = "$(TEST_HOST)"; + INFOPLIST_FILE = BabyTestProjectTests/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/Frameworks @loader_path/Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestProject.BabyTestProjectTests; + PRODUCT_NAME = "$(TARGET_NAME)"; + TEST_HOST = "$(BUILT_PRODUCTS_DIR)/BabyTestProject.app/BabyTestProject"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F0AF1DBA1C92A4D100FD1717 /* Build configuration list for PBXProject "BabyTestProject" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0AF1DEA1C92A4D100FD1717 /* Debug */, + F0AF1DEB1C92A4D100FD1717 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F0AF1DEC1C92A4D100FD1717 /* Build configuration list for PBXNativeTarget "BabyTestProject" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0AF1DED1C92A4D100FD1717 /* Debug */, + F0AF1DEE1C92A4D100FD1717 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F0AF1DEF1C92A4D100FD1717 /* Build configuration list for PBXNativeTarget "BabyTestProjectTests" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0AF1DF01C92A4D100FD1717 /* Debug */, + F0AF1DF11C92A4D100FD1717 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; +/* End XCConfigurationList section */ + }; + rootObject = F0AF1DB71C92A4D100FD1717 /* Project object */; +} diff --git a/Test/BabyTestProject/BabyTestProject.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Test/BabyTestProject/BabyTestProject.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..3b88561 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Test/BabyTestProject/BabyTestProject/AppDelegate.h b/Test/BabyTestProject/BabyTestProject/AppDelegate.h new file mode 100644 index 0000000..23d6d17 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/AppDelegate.h @@ -0,0 +1,17 @@ +// +// AppDelegate.h +// BabyTestProject +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@interface AppDelegate : UIResponder + +@property (strong, nonatomic) UIWindow *window; + + +@end + diff --git a/Test/BabyTestProject/BabyTestProject/AppDelegate.m b/Test/BabyTestProject/BabyTestProject/AppDelegate.m new file mode 100644 index 0000000..b18b31d --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/AppDelegate.m @@ -0,0 +1,45 @@ +// +// AppDelegate.m +// BabyTestProject +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "AppDelegate.h" + +@interface AppDelegate () + +@end + +@implementation AppDelegate + + +- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { + // Override point for customization after application launch. + return YES; +} + +- (void)applicationWillResignActive:(UIApplication *)application { + // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. + // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game. +} + +- (void)applicationDidEnterBackground:(UIApplication *)application { + // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later. + // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits. +} + +- (void)applicationWillEnterForeground:(UIApplication *)application { + // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background. +} + +- (void)applicationDidBecomeActive:(UIApplication *)application { + // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface. +} + +- (void)applicationWillTerminate:(UIApplication *)application { + // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:. +} + +@end diff --git a/Test/BabyTestProject/BabyTestProject/Assets.xcassets/AppIcon.appiconset/Contents.json b/Test/BabyTestProject/BabyTestProject/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..118c98f --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,38 @@ +{ + "images" : [ + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "29x29", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "40x40", + "scale" : "3x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "2x" + }, + { + "idiom" : "iphone", + "size" : "60x60", + "scale" : "3x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Test/BabyTestProject/BabyTestProject/Base.lproj/LaunchScreen.storyboard b/Test/BabyTestProject/BabyTestProject/Base.lproj/LaunchScreen.storyboard new file mode 100644 index 0000000..2e721e1 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/Base.lproj/LaunchScreen.storyboard @@ -0,0 +1,27 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Test/BabyTestProject/BabyTestProject/Base.lproj/Main.storyboard b/Test/BabyTestProject/BabyTestProject/Base.lproj/Main.storyboard new file mode 100644 index 0000000..ff3d372 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/Base.lproj/Main.storyboard @@ -0,0 +1,26 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Test/BabyTestProject/BabyTestProject/Info.plist b/Test/BabyTestProject/BabyTestProject/Info.plist new file mode 100644 index 0000000..6905cc6 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/Info.plist @@ -0,0 +1,40 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSRequiresIPhoneOS + + UILaunchStoryboardName + LaunchScreen + UIMainStoryboardFile + Main + UIRequiredDeviceCapabilities + + armv7 + + UISupportedInterfaceOrientations + + UIInterfaceOrientationPortrait + UIInterfaceOrientationLandscapeLeft + UIInterfaceOrientationLandscapeRight + + + diff --git a/Test/BabyTestProject/BabyTestProject/ViewController.h b/Test/BabyTestProject/BabyTestProject/ViewController.h new file mode 100644 index 0000000..f870387 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/ViewController.h @@ -0,0 +1,15 @@ +// +// ViewController.h +// BabyTestProject +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@interface ViewController : UIViewController + + +@end + diff --git a/Test/BabyTestProject/BabyTestProject/ViewController.m b/Test/BabyTestProject/BabyTestProject/ViewController.m new file mode 100644 index 0000000..54afd02 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/ViewController.m @@ -0,0 +1,27 @@ +// +// ViewController.m +// BabyTestProject +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "ViewController.h" + +@interface ViewController () + +@end + +@implementation ViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + // Do any additional setup after loading the view, typically from a nib. +} + +- (void)didReceiveMemoryWarning { + [super didReceiveMemoryWarning]; + // Dispose of any resources that can be recreated. +} + +@end diff --git a/Test/BabyTestProject/BabyTestProject/main.m b/Test/BabyTestProject/BabyTestProject/main.m new file mode 100644 index 0000000..5df5971 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProject/main.m @@ -0,0 +1,16 @@ +// +// main.m +// BabyTestProject +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import +#import "AppDelegate.h" + +int main(int argc, char * argv[]) { + @autoreleasepool { + return UIApplicationMain(argc, argv, nil, NSStringFromClass([AppDelegate class])); + } +} diff --git a/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.h b/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.h new file mode 100644 index 0000000..73fd2fa --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.h @@ -0,0 +1,19 @@ +// +// BabyTestExpretaion.h +// BabyTestProject +// +// Created by 刘彦玮 on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@interface BabyTestExpretaion : NSObject + +@property (nonatomic, assign) BOOL hasFulfill; + +- (instancetype) initWithExp:(XCTestExpectation *)exp; + +- (void) fulfill; + +@end diff --git a/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.m b/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.m new file mode 100644 index 0000000..68fc6ca --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectTests/BabyTestExpretaion.m @@ -0,0 +1,35 @@ +// +// BabyTestExpretaion.m +// BabyTestProject +// +// Created by 刘彦玮 on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "BabyTestExpretaion.h" + +@interface BabyTestExpretaion() + + +@property (nonatomic, strong) XCTestExpectation *exp; + +@end + +@implementation BabyTestExpretaion + +- (instancetype)initWithExp:(XCTestExpectation *)exp { + self = [super init]; + if (self) { + _exp = exp; + _hasFulfill = NO; + } + return self; +} + +- (void)fulfill { + if (!self.hasFulfill) { + [self.exp fulfill]; + } + self.hasFulfill = YES; +} +@end diff --git a/Test/BabyTestProject/BabyTestProjectTests/BabyTestProjectTests.m b/Test/BabyTestProject/BabyTestProjectTests/BabyTestProjectTests.m new file mode 100644 index 0000000..fe637ee --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectTests/BabyTestProjectTests.m @@ -0,0 +1,493 @@ +// +// BabyTestProjectTests.m +// BabyTestProjectTests +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import +#import "BabyBluetooth.h" +#import "BabyTestExpretaion.h" + +@interface BabyTestProjectTests : XCTestCase + +@property (nonatomic, strong) BabyBluetooth *baby; +@property (nonatomic, strong) CBPeripheral *testPeripheral; + +@end + + +//NSString * const testPeripleralName = @"BabyBluetoothTestStub"; +NSString * const testPeripleralName = @"baby-default-name"; + +# warning testPeripleralUUIDString这个值会根据不同设备变化的,同一个设备也可能用为重启会产生变化,可以通过打印 [[peripheral identifier] UUIDString]] 取得 +NSString * const testPeripleralUUIDString = @"FD9C47C0-B6A8-2D91-BD7D-C91810654EE8"; + +@implementation BabyTestProjectTests + +- (void)setUp { + [super setUp]; + self.baby = [BabyBluetooth shareBabyBluetooth]; +} + +- (void)tearDown { + [super tearDown]; +} + +#pragma mark - unit test + +/** + 测试蓝牙设备状态委托 + test centralManager and peripheralManager can power on + + @method: peripheralModelBlockOnPeripheralManagerDidUpdateState +*/ +- (void)testCentralManagerAndPeripheralManagerCanPowerOn { + + XCTestExpectation *cmExprect = [self expectationWithDescription:@"centralManager can't power on"]; + XCTestExpectation *pmExprect = [self expectationWithDescription:@"peripheralManager can't power on"]; + + if (self.baby.centralManager.state == CBPeripheralManagerStatePoweredOn) { + [cmExprect fulfill]; + } + + [self.baby setBlockOnCentralManagerDidUpdateState:^(CBCentralManager *central) { + if (central.state == CBCentralManagerStatePoweredOn) { + [cmExprect fulfill]; + } + }]; + + if (self.baby.peripheralManager.state == CBPeripheralManagerStatePoweredOn) { + [pmExprect fulfill]; + } + [self.baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) { + if (peripheral.state == CBPeripheralManagerStatePoweredOn) { + [pmExprect fulfill]; + } + }]; + + [self waitForExpectationsWithTimeout:10 handler:nil]; + +} + +/** + 测试链式方法中心模式主要的委托和过滤器,enjoy()方法 + !!测试前必须先启动BabyTestStub项目 + + 执行顺序:启动->过滤扫描->扫描->过滤连接->连接->发现服务->发现特征->读取特征->读取特征的描述->读取Rssi->取消扫描->断开连接->结束 + */ +- (void)testCentralModelMainOfDelegateAndFilter { + + __weak __typeof(self) weakSelf = self; + + BabyTestExpretaion *filterOnDiscoverPeripheralsExp = [self expWithDescription:@"filterOnDiscoverPeripherals not execute"]; + BabyTestExpretaion *blockOnDiscoverToPeripheralsExp = [self expWithDescription:@"blockOnDiscoverToPeripheralsExp not execute"]; + + BabyTestExpretaion *filterOnConnectToPeripheralsExp = [self expWithDescription:@"filterOnConnectToPeripherals not execute"]; + BabyTestExpretaion *blockOnConnectedExp = [self expWithDescription:@"blockOnConnectedExp not execute"]; + + BabyTestExpretaion *blockOnDiscoverServicesExp = [self expWithDescription:@"blockOnDiscoverServicesExp not execute"]; + BabyTestExpretaion *blockOnDiscoverCharacteristicsExp = [self expWithDescription:@"blockOnDiscoverCharacteristics not execute"]; + BabyTestExpretaion *blockOnReadValueForCharacteristicExp = [self expWithDescription:@"blockOnReadValueForCharacteristic not execute"]; + + BabyTestExpretaion *blockOnDiscoverDescriptorsForCharacteristicExp = [self expWithDescription:@"blockOnDiscoverDescriptorsForCharacteristic not execute"]; + BabyTestExpretaion *blockOnReadValueForDescriptorsExp = [self expWithDescription:@"blockOnReadValueForDescriptors not execute"]; + + BabyTestExpretaion *blockOnReadRSSIExp = [self expWithDescription:@"blockOnReadRSSI not execute"]; + + BabyTestExpretaion *blockOnDisconnectExp = [self expWithDescription:@"blockOnDisconnect block not execute"]; + BabyTestExpretaion *blockOnCancelScanExp = [self expWithDescription:@"blockOnCancelScan block not execute"]; + BabyTestExpretaion *blockOnCancelAllPeripheralsConnectionExp = [self expWithDescription:@"blockOnCancelAllPeripheralsConnection block not execute"]; + + //设置查找设备的过滤器 + //只放过测试peripheral名称相等的设备 + [self.baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"搜索到了设备:%@ | %@",peripheralName, localName); + if ([localName isEqualToString:testPeripleralName]) { + [filterOnDiscoverPeripheralsExp fulfill]; + return YES; + } + return NO; + }]; + + //设置扫描到设备的委托 + [self.baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) { + + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"搜索到了设备:%@ | %@",peripheral.name, localName); + if ([localName isEqualToString:testPeripleralName]) { + [blockOnDiscoverToPeripheralsExp fulfill]; + weakSelf.testPeripheral = peripheral; + }else { + //如果出现非测试程序的设备则出错 + [weakSelf failOnTest:@"filterOnDiscoverPeripherals 方法未进行有效的过滤"]; + } + }]; + + //设置连接设备的过滤器 + [self.baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"连接设备的过滤器,设备:%@",localName); + if ([localName isEqualToString:testPeripleralName]) { + [filterOnConnectToPeripheralsExp fulfill]; + return YES; + } + return NO; + }]; + + //设置连接设备的委托 + [self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { + NSLog(@"搜索到了设备:%@",peripheral.name); + if (self.testPeripheral == peripheral) { + [blockOnConnectedExp fulfill]; + //读取RSSI测试 + [weakSelf.testPeripheral readRSSI]; + } else { + //如果出现非测试程序的设备则出错 + [weakSelf failOnTest:@"setBlockOnConnected 方法未进行有效的过滤"]; + } + }]; + + //设置发现设备的Services的委托 + [self.baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) { + [blockOnDiscoverServicesExp fulfill]; + }]; + + //设置发现设service的Characteristics的委托 + [self.baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { + [blockOnDiscoverCharacteristicsExp fulfill]; + }]; + + //设置读取characteristics的委托 + [self.baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) { + NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value); + [blockOnReadValueForCharacteristicExp fulfill]; + }]; + + //设置发现characteristics的descriptors的委托 + [self.baby setBlockOnDiscoverDescriptorsForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error) { + NSLog(@"===characteristic name:%@",characteristic.service.UUID); + for (CBDescriptor *d in characteristic.descriptors) { + NSLog(@"CBDescriptor name is :%@",d.UUID); + } + [blockOnDiscoverDescriptorsForCharacteristicExp fulfill]; + }]; + + //设置读取Descriptor的委托 + [self.baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) { + NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value); + [blockOnReadValueForDescriptorsExp fulfill]; + }]; + + //读取rssi的委托 + [self.baby setBlockOnDidReadRSSI:^(NSNumber *RSSI, NSError *error) { + NSLog(@"setBlockOnDidReadRSSI:RSSI:%@",RSSI); + [blockOnReadRSSIExp fulfill]; + }]; + + //断开设备测试,读取rssi测试 + [self.baby setBlockOnCancelScanBlock:^(CBCentralManager *centralManager) { + NSLog(@"setBlockOnCancelScanBlock"); + [blockOnCancelScanExp fulfill]; + }]; + + //断开连接委托 + [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) { + NSLog(@"设备:%@--断开连接",peripheral.name); + [blockOnDisconnectExp fulfill]; + }]; + + [self.baby setBlockOnCancelAllPeripheralsConnectionBlock:^(CBCentralManager *centralManager) { + NSLog(@"setBlockOnCancelAllPeripheralsConnectionBlock"); + [blockOnCancelAllPeripheralsConnectionExp fulfill]; + }]; + + //启动中心设备 +// self.baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin().stop(15); + self.baby.scanForPeripherals().enjoy().stop(15); + + //预期 + [self waitForExpectationsWithTimeout:20 handler:nil]; + + //无法模拟测试 + // + // //设置设备连接失败的委托 + // [self.baby setBlockOnFailToConnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) { + // NSLog(@"设备:%@--连接失败",peripheral.name); + // [blockOnFailToConnectExp fulfill]; + // + // }]; + // +} + + +/** + 测试Peripheral断开后自动重连方法 + + @method: (void)AutoReconnect:(CBPeripheral *)peripheral;//添加断开自动重连的外设 +*/ + +- (void)testAutoReConnect { + + __weak __typeof(self) weakSelf = self; + BabyTestExpretaion *blockOnDisconnectExp = [self expWithDescription:@"first disconnect block not execute"]; + BabyTestExpretaion *blockOnReConnectExp = [self expWithDescription:@"secend reconnect block not execute"]; + + //设置扫描到设备的委托 + [self.baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) { + NSLog(@"%@",peripheral.identifier); + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + if ([localName isEqualToString:testPeripleralName]) { + NSLog(@"搜索到了设备:%@",peripheral.name); + } + + }]; + //设置连接设备的过滤器 + [self.baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"连接设备的过滤器,设备:%@",localName); + if ([localName isEqualToString:testPeripleralName]) { + return YES; + } + return NO; + }]; + + //设置连接的委托 + [self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { + if (blockOnDisconnectExp.hasFulfill) { + NSLog(@"设备:%@--已重新连接,测试成功",peripheral.name); + [blockOnReConnectExp fulfill]; + //清除自动重连接的状态 + [weakSelf.baby AutoReconnectCancel:peripheral]; + } else { + NSLog(@"设备:%@--已连接",peripheral.name); + //设置重新连接的设备 + [weakSelf.baby AutoReconnect:peripheral]; + NSLog(@"设备:--开始断开连接,测试重连功能"); + [weakSelf.baby cancelAllPeripheralsConnection]; + } + }]; + + //断开连接的委托 + [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) { + NSLog(@"设备:%@--断开连接",peripheral.name); + [blockOnDisconnectExp fulfill]; +// [weakSelf.baby.centralManager connectPeripheral:peripheral options:nil]; +// NSArray *array = [weakSelf.baby.centralManager retrievePeripheralsWithIdentifiers:@[peripheral.identifier]]; +// [weakSelf.baby.centralManager retrieveConnectedPeripheralsWithServices:nil]; + }]; + + //启动中心设备 + self.baby.scanForPeripherals().connectToPeripherals().begin(); + + [self waitForExpectationsWithTimeout:20 handler:nil]; +} + + +/** + 测试获取已连接的外设相关功能 + + @method: + //获取当前连接的peripherals + - (NSArray *)findConnectedPeripherals; + //获取当前连接的peripheral + - (CBPeripheral *)findConnectedPeripheral:(NSString *)peripheralName; + + */ + +- (void)testConnectedPeripherals { + + __weak __typeof(self) weakSelf = self; + BabyTestExpretaion *fetchPeripheralExp = [self expWithDescription:@"fbug about findConnectedPeripheral instance!"]; + BabyTestExpretaion *notFoundExp = [self expWithDescription:@"bug about number of connectPeripheral"]; + + //设置委托 + + //设置连接设备的过滤器 + [self.baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"连接设备的过滤器,设备:%@",localName); + if ([localName isEqualToString:testPeripleralName]) { + return YES; + } + return NO; + }]; + + //设置连接的委托 + [self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { + NSLog(@"设备:%@--已连接",peripheral.name); + + //保证peripheralName和peripheral.name的指针也不相同 + NSString *peripheralName = [peripheral.name mutableCopy]; + CBPeripheral *finded = [weakSelf.baby findConnectedPeripheral:peripheralName]; + if (finded == peripheral) { + [fetchPeripheralExp fulfill]; + weakSelf.testPeripheral = peripheral; + //断开设备连接 + [central cancelPeripheralConnection:peripheral]; + } + }]; + + [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) { + if (peripheral == weakSelf.testPeripheral && [weakSelf.baby findConnectedPeripheral:[peripheral.name mutableCopy]] == nil) { + [notFoundExp fulfill]; + } + }]; + + //启动中心设备 + self.baby.scanForPeripherals().connectToPeripherals().begin(); + [self waitForExpectationsWithTimeout:20 handler:nil]; +} + + +/** + 测试根据UUIDString获取外设并快速连接 + + @method: [baby retrievePeripheralWithUUIDString:testPeripleralUUIDString] + + */ +-(void)testRetrievePeripheralWithUUIDString { + BabyTestExpretaion *connectExp = [self expWithDescription:@"testRetrievePeripheralsWithIdentifiers falied "]; + + CBPeripheral *p = [self.baby retrievePeripheralWithUUIDString:testPeripleralUUIDString]; + //设置连接的委托 + [self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { + if (p == peripheral) { + NSLog(@"设备:%@--已连接>>>>>>>",peripheral.name); + [connectExp fulfill]; + } + }]; + self.baby.having(p).connectToPeripherals().begin(); + + [self waitForExpectationsWithTimeout:10 handler:nil]; +} + +/** + 测试蓝牙未连上是否可以读写操作 + */ +- (void)testPeripheralOperationOfDelegate { + + BabyTestExpretaion *connectExp = [self expWithDescription:@"testRetrievePeripheralsWithIdentifiers falied "]; + + //设置查找设备的过滤器 + //只放过测试peripheral名称相等的设备 + [self.baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"搜索到了设备:%@ | %@",peripheralName, localName); + if ([localName isEqualToString:testPeripleralName]) { + return YES; + } + return NO; + }]; + + //设置扫描到设备的委托 + [self.baby setBlockOnDiscoverToPeripherals:^(CBCentralManager *central, CBPeripheral *peripheral, NSDictionary *advertisementData, NSNumber *RSSI) { + + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"搜索到了设备:%@ | %@",peripheral.name, localName); + + }]; + + //设置连接设备的过滤器 + [self.baby setFilterOnConnectToPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"连接设备的过滤器,设备:%@",localName); + if ([localName isEqualToString:testPeripleralName]) { + return YES; + } + return NO; + }]; + + //设置连接设备的委托 + [self.baby setBlockOnConnected:^(CBCentralManager *central, CBPeripheral *peripheral) { + NSLog(@"搜索到了设备:%@",peripheral.name); + if (self.testPeripheral == peripheral) { + + } else { + + } + }]; + + //设置发现设备的Services的委托 + [self.baby setBlockOnDiscoverServices:^(CBPeripheral *peripheral, NSError *error) { + + }]; + + //设置发现设service的Characteristics的委托 + [self.baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { + + }]; + + //设置读取characteristics的委托 + [self.baby setBlockOnReadValueForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristics, NSError *error) { + NSLog(@"characteristic name:%@ value is:%@",characteristics.UUID,characteristics.value); + peripheral readValueForCharacteristic:<#(nonnull CBCharacteristic *)#> + }]; + + //设置发现characteristics的descriptors的委托 + [self.baby setBlockOnDiscoverDescriptorsForCharacteristic:^(CBPeripheral *peripheral, CBCharacteristic *characteristic, NSError *error) { + NSLog(@"===characteristic name:%@",characteristic.service.UUID); + for (CBDescriptor *d in characteristic.descriptors) { + NSLog(@"CBDescriptor name is :%@",d.UUID); + } + + }]; + + //设置读取Descriptor的委托 + [self.baby setBlockOnReadValueForDescriptors:^(CBPeripheral *peripheral, CBDescriptor *descriptor, NSError *error) { + NSLog(@"Descriptor name:%@ value is:%@",descriptor.characteristic.UUID, descriptor.value); + + }]; + + //读取rssi的委托 + [self.baby setBlockOnDidReadRSSI:^(NSNumber *RSSI, NSError *error) { + NSLog(@"setBlockOnDidReadRSSI:RSSI:%@",RSSI); + + }]; + + //断开设备测试,读取rssi测试 + [self.baby setBlockOnCancelScanBlock:^(CBCentralManager *centralManager) { + NSLog(@"setBlockOnCancelScanBlock"); + + }]; + + //断开连接委托 + [self.baby setBlockOnDisconnect:^(CBCentralManager *central, CBPeripheral *peripheral, NSError *error) { + NSLog(@"设备:%@--断开连接",peripheral.name); + + }]; + + [self.baby setBlockOnCancelAllPeripheralsConnectionBlock:^(CBCentralManager *centralManager) { + NSLog(@"setBlockOnCancelAllPeripheralsConnectionBlock"); + + }]; + + //启动中心设备 + self.baby.scanForPeripherals().connectToPeripherals().discoverServices().discoverCharacteristics().readValueForCharacteristic().discoverDescriptorsForCharacteristic().readValueForDescriptors().begin().stop(15); +// self.baby.scanForPeripherals().enjoy().stop(15); + + + [self waitForExpectationsWithTimeout:10 handler:nil]; +} + + +//- (void)testPerformanceExample { +// // This is an example of a performance test case. +// [self measureBlock:^{ +// // Put the code you want to measure the time of here. +// }]; +//} + + +- (void)failOnTest:(NSString *)msg { + XCTFail(@"%@",msg); +} + +- (BabyTestExpretaion *)expWithDescription:(NSString *)description { + BabyTestExpretaion *babyExp = [[BabyTestExpretaion alloc]initWithExp:[self expectationWithDescription:description]]; + return babyExp; +} + +@end diff --git a/Test/BabyTestProject/BabyTestProjectTests/BabybluetoothNotificationTest.m b/Test/BabyTestProject/BabyTestProjectTests/BabybluetoothNotificationTest.m new file mode 100644 index 0000000..c3ca6ad --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectTests/BabybluetoothNotificationTest.m @@ -0,0 +1,89 @@ +// +// BabybluetoothNotificationTest.m +// BabyTestProject +// +// Created by xuanyan.lyw on 16/4/20. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import +//#import "BabyTestExpretaion.h" +#import "BabyBluetooth.h" + + +@interface BabybluetoothNotificationTest : XCTestCase + +@property (nonatomic, strong) BabyBluetooth *baby; + +@end + +@implementation BabybluetoothNotificationTest + +NSString * const testPeripleralName1 = @"BabyBluetoothTestStub"; + +- (void)setUp { + [super setUp]; + self.baby = [BabyBluetooth shareBabyBluetooth]; +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +/** + + + BabyNotificationAtDidFailToConnectPeripheral 不测试 + + */ +- (void)testExample { + //蓝牙系统通知 + //[self expectationForNotification:(BabyNotificationAtCentralManagerDidUpdateState) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidDiscoverPeripheral) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidConnectPeripheral) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidDisconnectPeripheral) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidDiscoverServices) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidDiscoverCharacteristicsForService) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidWriteValueForCharacteristic) object:nil handler:nil]; + [self expectationForNotification:(BabyNotificationAtDidUpdateNotificationStateForCharacteristic) object:nil handler:nil]; + + //无法测试的类型 + // [self expectationForNotification:(BabyNotificationAtCentralManagerEnable) object:nil handler:nil]; + // [self expectationForNotification:(BabyNotificationAtDidFailToConnectPeripheral) object:nil handler:nil]; + + //设置扫描到设备的委托 + [self.baby setFilterOnDiscoverPeripherals:^BOOL(NSString *peripheralName, NSDictionary *advertisementData, NSNumber *RSSI) { + NSString *localName = [NSString stringWithFormat:@"%@",[advertisementData objectForKey:@"kCBAdvDataLocalName"]]; + NSLog(@"搜索到了设备:%@",localName); + if ([localName isEqualToString:testPeripleralName1]) { + return YES; + } + return NO; + }]; + + [self.baby setBlockOnDiscoverCharacteristics:^(CBPeripheral *peripheral, CBService *service, NSError *error) { + for (CBCharacteristic *c in service.characteristics) { + if (c.properties & CBCharacteristicPropertyWrite) { + Byte b = 0X01; + NSData *data = [NSData dataWithBytes:&b length:sizeof(b)]; + [peripheral writeValue:data forCharacteristic:c type:CBCharacteristicWriteWithResponse]; + } + if (c.properties & CBCharacteristicPropertyNotify) { + [peripheral setNotifyValue:YES forCharacteristic:c]; + } + } + }]; + + self.baby.scanForPeripherals().enjoy().stop(10); + [self waitForExpectationsWithTimeout:15 handler:nil]; +} + +//- (void)testPerformanceExample { +// // This is an example of a performance test case. +// [self measureBlock:^{ +// // Put the code you want to measure the time of here. +// }]; +//} + +@end diff --git a/Test/BabyTestProject/BabyTestProjectTests/Info.plist b/Test/BabyTestProject/BabyTestProjectTests/Info.plist new file mode 100644 index 0000000..ba72822 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectTests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + + diff --git a/Test/BabyTestProject/BabyTestProjectUITests/BabyTestProjectUITests.m b/Test/BabyTestProject/BabyTestProjectUITests/BabyTestProjectUITests.m new file mode 100644 index 0000000..e5e5cca --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectUITests/BabyTestProjectUITests.m @@ -0,0 +1,40 @@ +// +// BabyTestProjectUITests.m +// BabyTestProjectUITests +// +// Created by ZTELiuyw on 16/3/11. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@interface BabyTestProjectUITests : XCTestCase + +@end + +@implementation BabyTestProjectUITests + +- (void)setUp { + [super setUp]; + + // Put setup code here. This method is called before the invocation of each test method in the class. + + // In UI tests it is usually best to stop immediately when a failure occurs. + self.continueAfterFailure = NO; + // UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method. + [[[XCUIApplication alloc] init] launch]; + + // In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this. +} + +- (void)tearDown { + // Put teardown code here. This method is called after the invocation of each test method in the class. + [super tearDown]; +} + +- (void)testExample { + // Use recording to get started writing UI tests. + // Use XCTAssert and related functions to verify your tests produce the correct results. +} + +@end diff --git a/Test/BabyTestProject/BabyTestProjectUITests/Info.plist b/Test/BabyTestProject/BabyTestProjectUITests/Info.plist new file mode 100644 index 0000000..ba72822 --- /dev/null +++ b/Test/BabyTestProject/BabyTestProjectUITests/Info.plist @@ -0,0 +1,24 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + BNDL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + + diff --git a/Test/BabyTestStub/BabyTestStub.xcodeproj/project.pbxproj b/Test/BabyTestStub/BabyTestStub.xcodeproj/project.pbxproj new file mode 100644 index 0000000..8eefab3 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub.xcodeproj/project.pbxproj @@ -0,0 +1,351 @@ +// !$*UTF8*$! +{ + archiveVersion = 1; + classes = { + }; + objectVersion = 46; + objects = { + +/* Begin PBXBuildFile section */ + F0D05E171C965017000EFD22 /* AppDelegate.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E161C965017000EFD22 /* AppDelegate.m */; }; + F0D05E1A1C965017000EFD22 /* main.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E191C965017000EFD22 /* main.m */; }; + F0D05E1D1C965017000EFD22 /* ViewController.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E1C1C965017000EFD22 /* ViewController.m */; }; + F0D05E1F1C965017000EFD22 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = F0D05E1E1C965017000EFD22 /* Assets.xcassets */; }; + F0D05E221C965017000EFD22 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = F0D05E201C965017000EFD22 /* Main.storyboard */; }; + F0D05E3A1C965025000EFD22 /* BabyBluetooth.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E2B1C965025000EFD22 /* BabyBluetooth.m */; }; + F0D05E3B1C965025000EFD22 /* BabyCallback.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E2D1C965025000EFD22 /* BabyCallback.m */; }; + F0D05E3C1C965025000EFD22 /* BabyCentralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E2F1C965025000EFD22 /* BabyCentralManager.m */; }; + F0D05E3D1C965025000EFD22 /* BabyOptions.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E311C965025000EFD22 /* BabyOptions.m */; }; + F0D05E3E1C965025000EFD22 /* BabyPeripheralManager.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E331C965025000EFD22 /* BabyPeripheralManager.m */; }; + F0D05E3F1C965025000EFD22 /* BabyRhythm.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E351C965025000EFD22 /* BabyRhythm.m */; }; + F0D05E401C965025000EFD22 /* BabySpeaker.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E371C965025000EFD22 /* BabySpeaker.m */; }; + F0D05E411C965025000EFD22 /* BabyToy.m in Sources */ = {isa = PBXBuildFile; fileRef = F0D05E391C965025000EFD22 /* BabyToy.m */; }; +/* End PBXBuildFile section */ + +/* Begin PBXFileReference section */ + F0D05E121C965017000EFD22 /* BabyTestStub.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = BabyTestStub.app; sourceTree = BUILT_PRODUCTS_DIR; }; + F0D05E151C965017000EFD22 /* AppDelegate.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = AppDelegate.h; sourceTree = ""; }; + F0D05E161C965017000EFD22 /* AppDelegate.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = AppDelegate.m; sourceTree = ""; }; + F0D05E191C965017000EFD22 /* main.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = main.m; sourceTree = ""; }; + F0D05E1B1C965017000EFD22 /* ViewController.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = ViewController.h; sourceTree = ""; }; + F0D05E1C1C965017000EFD22 /* ViewController.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = ViewController.m; sourceTree = ""; }; + F0D05E1E1C965017000EFD22 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; + F0D05E211C965017000EFD22 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; + F0D05E231C965017000EFD22 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; + F0D05E2A1C965025000EFD22 /* BabyBluetooth.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyBluetooth.h; sourceTree = ""; }; + F0D05E2B1C965025000EFD22 /* BabyBluetooth.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyBluetooth.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0D05E2C1C965025000EFD22 /* BabyCallback.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCallback.h; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objcpp; }; + F0D05E2D1C965025000EFD22 /* BabyCallback.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCallback.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0D05E2E1C965025000EFD22 /* BabyCentralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyCentralManager.h; sourceTree = ""; }; + F0D05E2F1C965025000EFD22 /* BabyCentralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyCentralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0D05E301C965025000EFD22 /* BabyOptions.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyOptions.h; sourceTree = ""; }; + F0D05E311C965025000EFD22 /* BabyOptions.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyOptions.m; sourceTree = ""; }; + F0D05E321C965025000EFD22 /* BabyPeripheralManager.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyPeripheralManager.h; sourceTree = ""; }; + F0D05E331C965025000EFD22 /* BabyPeripheralManager.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyPeripheralManager.m; sourceTree = ""; xcLanguageSpecificationIdentifier = xcode.lang.objc; }; + F0D05E341C965025000EFD22 /* BabyRhythm.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyRhythm.h; sourceTree = ""; }; + F0D05E351C965025000EFD22 /* BabyRhythm.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyRhythm.m; sourceTree = ""; }; + F0D05E361C965025000EFD22 /* BabySpeaker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabySpeaker.h; sourceTree = ""; }; + F0D05E371C965025000EFD22 /* BabySpeaker.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabySpeaker.m; sourceTree = ""; }; + F0D05E381C965025000EFD22 /* BabyToy.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = BabyToy.h; sourceTree = ""; }; + F0D05E391C965025000EFD22 /* BabyToy.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = BabyToy.m; sourceTree = ""; }; +/* End PBXFileReference section */ + +/* Begin PBXFrameworksBuildPhase section */ + F0D05E0F1C965017000EFD22 /* Frameworks */ = { + isa = PBXFrameworksBuildPhase; + buildActionMask = 2147483647; + files = ( + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXFrameworksBuildPhase section */ + +/* Begin PBXGroup section */ + F0D05E091C965017000EFD22 = { + isa = PBXGroup; + children = ( + F0D05E291C965025000EFD22 /* Baby */, + F0D05E141C965017000EFD22 /* BabyTestStub */, + F0D05E131C965017000EFD22 /* Products */, + ); + sourceTree = ""; + }; + F0D05E131C965017000EFD22 /* Products */ = { + isa = PBXGroup; + children = ( + F0D05E121C965017000EFD22 /* BabyTestStub.app */, + ); + name = Products; + sourceTree = ""; + }; + F0D05E141C965017000EFD22 /* BabyTestStub */ = { + isa = PBXGroup; + children = ( + F0D05E151C965017000EFD22 /* AppDelegate.h */, + F0D05E161C965017000EFD22 /* AppDelegate.m */, + F0D05E1B1C965017000EFD22 /* ViewController.h */, + F0D05E1C1C965017000EFD22 /* ViewController.m */, + F0D05E1E1C965017000EFD22 /* Assets.xcassets */, + F0D05E201C965017000EFD22 /* Main.storyboard */, + F0D05E231C965017000EFD22 /* Info.plist */, + F0D05E181C965017000EFD22 /* Supporting Files */, + ); + path = BabyTestStub; + sourceTree = ""; + }; + F0D05E181C965017000EFD22 /* Supporting Files */ = { + isa = PBXGroup; + children = ( + F0D05E191C965017000EFD22 /* main.m */, + ); + name = "Supporting Files"; + sourceTree = ""; + }; + F0D05E291C965025000EFD22 /* Baby */ = { + isa = PBXGroup; + children = ( + F0D05E2A1C965025000EFD22 /* BabyBluetooth.h */, + F0D05E2B1C965025000EFD22 /* BabyBluetooth.m */, + F0D05E2C1C965025000EFD22 /* BabyCallback.h */, + F0D05E2D1C965025000EFD22 /* BabyCallback.m */, + F0D05E2E1C965025000EFD22 /* BabyCentralManager.h */, + F0D05E2F1C965025000EFD22 /* BabyCentralManager.m */, + F0D05E301C965025000EFD22 /* BabyOptions.h */, + F0D05E311C965025000EFD22 /* BabyOptions.m */, + F0D05E321C965025000EFD22 /* BabyPeripheralManager.h */, + F0D05E331C965025000EFD22 /* BabyPeripheralManager.m */, + F0D05E341C965025000EFD22 /* BabyRhythm.h */, + F0D05E351C965025000EFD22 /* BabyRhythm.m */, + F0D05E361C965025000EFD22 /* BabySpeaker.h */, + F0D05E371C965025000EFD22 /* BabySpeaker.m */, + F0D05E381C965025000EFD22 /* BabyToy.h */, + F0D05E391C965025000EFD22 /* BabyToy.m */, + ); + name = Baby; + path = ../../Classes/objc; + sourceTree = ""; + }; +/* End PBXGroup section */ + +/* Begin PBXNativeTarget section */ + F0D05E111C965017000EFD22 /* BabyTestStub */ = { + isa = PBXNativeTarget; + buildConfigurationList = F0D05E261C965017000EFD22 /* Build configuration list for PBXNativeTarget "BabyTestStub" */; + buildPhases = ( + F0D05E0E1C965017000EFD22 /* Sources */, + F0D05E0F1C965017000EFD22 /* Frameworks */, + F0D05E101C965017000EFD22 /* Resources */, + ); + buildRules = ( + ); + dependencies = ( + ); + name = BabyTestStub; + productName = BabyTestStub; + productReference = F0D05E121C965017000EFD22 /* BabyTestStub.app */; + productType = "com.apple.product-type.application"; + }; +/* End PBXNativeTarget section */ + +/* Begin PBXProject section */ + F0D05E0A1C965017000EFD22 /* Project object */ = { + isa = PBXProject; + attributes = { + LastUpgradeCheck = 0720; + ORGANIZATIONNAME = liuyanwei; + TargetAttributes = { + F0D05E111C965017000EFD22 = { + CreatedOnToolsVersion = 7.2.1; + }; + }; + }; + buildConfigurationList = F0D05E0D1C965017000EFD22 /* Build configuration list for PBXProject "BabyTestStub" */; + compatibilityVersion = "Xcode 3.2"; + developmentRegion = English; + hasScannedForEncodings = 0; + knownRegions = ( + en, + Base, + ); + mainGroup = F0D05E091C965017000EFD22; + productRefGroup = F0D05E131C965017000EFD22 /* Products */; + projectDirPath = ""; + projectRoot = ""; + targets = ( + F0D05E111C965017000EFD22 /* BabyTestStub */, + ); + }; +/* End PBXProject section */ + +/* Begin PBXResourcesBuildPhase section */ + F0D05E101C965017000EFD22 /* Resources */ = { + isa = PBXResourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F0D05E1F1C965017000EFD22 /* Assets.xcassets in Resources */, + F0D05E221C965017000EFD22 /* Main.storyboard in Resources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXResourcesBuildPhase section */ + +/* Begin PBXSourcesBuildPhase section */ + F0D05E0E1C965017000EFD22 /* Sources */ = { + isa = PBXSourcesBuildPhase; + buildActionMask = 2147483647; + files = ( + F0D05E1D1C965017000EFD22 /* ViewController.m in Sources */, + F0D05E411C965025000EFD22 /* BabyToy.m in Sources */, + F0D05E1A1C965017000EFD22 /* main.m in Sources */, + F0D05E3D1C965025000EFD22 /* BabyOptions.m in Sources */, + F0D05E3A1C965025000EFD22 /* BabyBluetooth.m in Sources */, + F0D05E3C1C965025000EFD22 /* BabyCentralManager.m in Sources */, + F0D05E3B1C965025000EFD22 /* BabyCallback.m in Sources */, + F0D05E171C965017000EFD22 /* AppDelegate.m in Sources */, + F0D05E401C965025000EFD22 /* BabySpeaker.m in Sources */, + F0D05E3F1C965025000EFD22 /* BabyRhythm.m in Sources */, + F0D05E3E1C965025000EFD22 /* BabyPeripheralManager.m in Sources */, + ); + runOnlyForDeploymentPostprocessing = 0; + }; +/* End PBXSourcesBuildPhase section */ + +/* Begin PBXVariantGroup section */ + F0D05E201C965017000EFD22 /* Main.storyboard */ = { + isa = PBXVariantGroup; + children = ( + F0D05E211C965017000EFD22 /* Base */, + ); + name = Main.storyboard; + sourceTree = ""; + }; +/* End PBXVariantGroup section */ + +/* Begin XCBuildConfiguration section */ + F0D05E241C965017000EFD22 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = dwarf; + ENABLE_STRICT_OBJC_MSGSEND = YES; + ENABLE_TESTABILITY = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_DYNAMIC_NO_PIC = NO; + GCC_NO_COMMON_BLOCKS = YES; + GCC_OPTIMIZATION_LEVEL = 0; + GCC_PREPROCESSOR_DEFINITIONS = ( + "DEBUG=1", + "$(inherited)", + ); + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = YES; + ONLY_ACTIVE_ARCH = YES; + SDKROOT = macosx; + }; + name = Debug; + }; + F0D05E251C965017000EFD22 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ALWAYS_SEARCH_USER_PATHS = NO; + CLANG_CXX_LANGUAGE_STANDARD = "gnu++0x"; + CLANG_CXX_LIBRARY = "libc++"; + CLANG_ENABLE_MODULES = YES; + CLANG_ENABLE_OBJC_ARC = YES; + CLANG_WARN_BOOL_CONVERSION = YES; + CLANG_WARN_CONSTANT_CONVERSION = YES; + CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; + CLANG_WARN_EMPTY_BODY = YES; + CLANG_WARN_ENUM_CONVERSION = YES; + CLANG_WARN_INT_CONVERSION = YES; + CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; + CLANG_WARN_UNREACHABLE_CODE = YES; + CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; + CODE_SIGN_IDENTITY = "-"; + COPY_PHASE_STRIP = NO; + DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; + ENABLE_NS_ASSERTIONS = NO; + ENABLE_STRICT_OBJC_MSGSEND = YES; + GCC_C_LANGUAGE_STANDARD = gnu99; + GCC_NO_COMMON_BLOCKS = YES; + GCC_WARN_64_TO_32_BIT_CONVERSION = YES; + GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; + GCC_WARN_UNDECLARED_SELECTOR = YES; + GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; + GCC_WARN_UNUSED_FUNCTION = YES; + GCC_WARN_UNUSED_VARIABLE = YES; + MACOSX_DEPLOYMENT_TARGET = 10.10; + MTL_ENABLE_DEBUG_INFO = NO; + SDKROOT = macosx; + }; + name = Release; + }; + F0D05E271C965017000EFD22 /* Debug */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = BabyTestStub/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestStub.BabyTestStub; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Debug; + }; + F0D05E281C965017000EFD22 /* Release */ = { + isa = XCBuildConfiguration; + buildSettings = { + ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; + COMBINE_HIDPI_IMAGES = YES; + INFOPLIST_FILE = BabyTestStub/Info.plist; + LD_RUNPATH_SEARCH_PATHS = "$(inherited) @executable_path/../Frameworks"; + PRODUCT_BUNDLE_IDENTIFIER = com.jumppo.app.BabyTestStub.BabyTestStub; + PRODUCT_NAME = "$(TARGET_NAME)"; + }; + name = Release; + }; +/* End XCBuildConfiguration section */ + +/* Begin XCConfigurationList section */ + F0D05E0D1C965017000EFD22 /* Build configuration list for PBXProject "BabyTestStub" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0D05E241C965017000EFD22 /* Debug */, + F0D05E251C965017000EFD22 /* Release */, + ); + defaultConfigurationIsVisible = 0; + defaultConfigurationName = Release; + }; + F0D05E261C965017000EFD22 /* Build configuration list for PBXNativeTarget "BabyTestStub" */ = { + isa = XCConfigurationList; + buildConfigurations = ( + F0D05E271C965017000EFD22 /* Debug */, + F0D05E281C965017000EFD22 /* Release */, + ); + defaultConfigurationIsVisible = 0; + }; +/* End XCConfigurationList section */ + }; + rootObject = F0D05E0A1C965017000EFD22 /* Project object */; +} diff --git a/Test/BabyTestStub/BabyTestStub.xcodeproj/project.xcworkspace/contents.xcworkspacedata b/Test/BabyTestStub/BabyTestStub.xcodeproj/project.xcworkspace/contents.xcworkspacedata new file mode 100644 index 0000000..5d6690d --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub.xcodeproj/project.xcworkspace/contents.xcworkspacedata @@ -0,0 +1,7 @@ + + + + + diff --git a/Test/BabyTestStub/BabyTestStub/AppDelegate.h b/Test/BabyTestStub/BabyTestStub/AppDelegate.h new file mode 100644 index 0000000..907bad5 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/AppDelegate.h @@ -0,0 +1,15 @@ +// +// AppDelegate.h +// BabyTestStub +// +// Created by ZTELiuyw on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@interface AppDelegate : NSObject + + +@end + diff --git a/Test/BabyTestStub/BabyTestStub/AppDelegate.m b/Test/BabyTestStub/BabyTestStub/AppDelegate.m new file mode 100644 index 0000000..9a31651 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/AppDelegate.m @@ -0,0 +1,25 @@ +// +// AppDelegate.m +// BabyTestStub +// +// Created by ZTELiuyw on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "AppDelegate.h" + +@interface AppDelegate () + +@end + +@implementation AppDelegate + +- (void)applicationDidFinishLaunching:(NSNotification *)aNotification { + // Insert code here to initialize your application +} + +- (void)applicationWillTerminate:(NSNotification *)aNotification { + // Insert code here to tear down your application +} + +@end diff --git a/Test/BabyTestStub/BabyTestStub/Assets.xcassets/AppIcon.appiconset/Contents.json b/Test/BabyTestStub/BabyTestStub/Assets.xcassets/AppIcon.appiconset/Contents.json new file mode 100644 index 0000000..2db2b1c --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/Assets.xcassets/AppIcon.appiconset/Contents.json @@ -0,0 +1,58 @@ +{ + "images" : [ + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "16x16", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "32x32", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "128x128", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "256x256", + "scale" : "2x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "1x" + }, + { + "idiom" : "mac", + "size" : "512x512", + "scale" : "2x" + } + ], + "info" : { + "version" : 1, + "author" : "xcode" + } +} \ No newline at end of file diff --git a/Test/BabyTestStub/BabyTestStub/Base.lproj/Main.storyboard b/Test/BabyTestStub/BabyTestStub/Base.lproj/Main.storyboard new file mode 100644 index 0000000..c6dddf0 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/Base.lproj/Main.storyboard @@ -0,0 +1,681 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + Default + + + + + + + Left to Right + + + + + + + Right to Left + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/Test/BabyTestStub/BabyTestStub/Info.plist b/Test/BabyTestStub/BabyTestStub/Info.plist new file mode 100644 index 0000000..03a4f6a --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/Info.plist @@ -0,0 +1,34 @@ + + + + + CFBundleDevelopmentRegion + en + CFBundleExecutable + $(EXECUTABLE_NAME) + CFBundleIconFile + + CFBundleIdentifier + $(PRODUCT_BUNDLE_IDENTIFIER) + CFBundleInfoDictionaryVersion + 6.0 + CFBundleName + $(PRODUCT_NAME) + CFBundlePackageType + APPL + CFBundleShortVersionString + 1.0 + CFBundleSignature + ???? + CFBundleVersion + 1 + LSMinimumSystemVersion + $(MACOSX_DEPLOYMENT_TARGET) + NSHumanReadableCopyright + Copyright © 2016年 liuyanwei. All rights reserved. + NSMainStoryboardFile + Main + NSPrincipalClass + NSApplication + + diff --git a/Test/BabyTestStub/BabyTestStub/ViewController.h b/Test/BabyTestStub/BabyTestStub/ViewController.h new file mode 100644 index 0000000..cc6d6f0 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/ViewController.h @@ -0,0 +1,19 @@ +// +// ViewController.h +// BabyTestStub +// +// Created by ZTELiuyw on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +@class BabyBluetooth; + +@interface ViewController : NSViewController + + +@property (nonatomic,strong) BabyBluetooth *baby; + +@end + diff --git a/Test/BabyTestStub/BabyTestStub/ViewController.m b/Test/BabyTestStub/BabyTestStub/ViewController.m new file mode 100644 index 0000000..d7ed085 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/ViewController.m @@ -0,0 +1,153 @@ +// +// ViewController.m +// BabyTestStub +// +// Created by ZTELiuyw on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import "ViewController.h" +#import "BabyBluetooth.h" + + + +@implementation ViewController + +- (void)viewDidLoad { + [super viewDidLoad]; + [self configAndStartAd]; + +} + +-(void) configAndStartAd { + //配置第一个服务s1 + CBMutableService *s1 = makeCBService(@"FFF0"); + //配置s1的3个characteristic + makeCharacteristicToService(s1, @"FFF1", @"r", @"hello1");//读 + makeCharacteristicToService(s1, @"FFF2", @"w", @"hello2");//写 + makeCharacteristicToService(s1, genUUID(), @"rw", @"hello3");//读写,自动生成uuid + makeCharacteristicToService(s1, @"FFF4", nil, @"hello4");//默认读写字段 + makeCharacteristicToService(s1, @"FFF5", @"n", @"hello5");//notify字段 + //配置第一个服务s2 + CBMutableService *s2 = makeCBService(@"FFE0"); + makeStaticCharacteristicToService(s2, genUUID(), @"hello6", [@"a" dataUsingEncoding:NSUTF8StringEncoding]);//一个含初值的字段,该字段权限只能是只读 + //实例化baby + self.baby = [BabyBluetooth shareBabyBluetooth]; + //配置委托 + [self babyDelegate]; + //添加服务和启动外设 + // self.baby.bePeripheral().addServices(@[s1,s2]).startAdvertising(); + + //设置广播包数据 + NSData *data = [[NSData alloc]initWithBase64EncodedString:@"qAEgECCVXQuEVSHedFvlQc5g5ivB" options:0]; + + self.baby.bePeripheralWithName(@"BabyBluetoothTestStub").addServices(@[s1,s2]).addManufacturerData(data).startAdvertising(); + + + /** + 模拟外设信息 + + localName = BabyBluetoothTestStub + uuid: 不固定 + alink协议隐藏的mac地址:C1:2B:E6:60:CE:41 + + services and characteristics + FFF0 + - FFF1 , FFF2 ,FFF3 + - 一个随机的uuid + - FFF5 是notiy字段,返回当前时间 + FFE0 + - 一个随机的只读uuid + **/ + + + +} +//配置委托 +- (void)babyDelegate{ + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnPeripheralManagerDidUpdateState:^(CBPeripheralManager *peripheral) { + NSLog(@"PeripheralManager trun status code: %ld",(long)peripheral.state); + if (peripheral.state == CBPeripheralManagerStatePoweredOn) { +// [self configAndStartAd]; + } + }]; + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidStartAdvertising:^(CBPeripheralManager *peripheral, NSError *error) { + NSLog(@"didStartAdvertising !!!"); + }]; + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidAddService:^(CBPeripheralManager *peripheral, CBService *service, NSError *error) { + NSLog(@"Did Add Service uuid: %@ ",service.UUID); + }]; + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidReceiveReadRequest:^(CBPeripheralManager *peripheral,CBATTRequest *request) { + NSLog(@"request characteristic uuid:%@",request.characteristic.UUID); + //判断是否有读数据的权限 + if (request.characteristic.properties & CBCharacteristicPropertyRead) { + NSData *data = request.characteristic.value; + [request setValue:data]; + //对请求作出成功响应 + [peripheral respondToRequest:request withResult:CBATTErrorSuccess]; + }else{ + //错误的响应 + [peripheral respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; + } + }]; + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidReceiveWriteRequests:^(CBPeripheralManager *peripheral,NSArray *requests) { + CBATTRequest *request = requests[0]; + NSLog(@"didReceiveWriteRequests: value:%@",request.value); + //判断是否有写数据的权限 + if (request.characteristic.properties & CBCharacteristicPropertyWrite) { + //需要转换成CBMutableCharacteristic对象才能进行写值 + CBMutableCharacteristic *c =(CBMutableCharacteristic *)request.characteristic; + c.value = request.value; + [peripheral respondToRequest:request withResult:CBATTErrorSuccess]; + }else{ + [peripheral respondToRequest:request withResult:CBATTErrorWriteNotPermitted]; + } + + }]; + + __block NSTimer *timer; + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidSubscribeToCharacteristic:^(CBPeripheralManager *peripheral, CBCentral *central, CBCharacteristic *characteristic) { + NSLog(@"订阅了 %@的数据",characteristic.UUID); + if (characteristic.isNotifying) { + return; + } + //每秒执行一次给主设备发送一个当前时间的秒数 + timer = [NSTimer scheduledTimerWithTimeInterval:3 target:self selector:@selector(sendData:) userInfo:characteristic repeats:YES]; + }]; + + //设置添加service委托 | set didAddService block + [self.baby peripheralModelBlockOnDidUnSubscribeToCharacteristic:^(CBPeripheralManager *peripheral, CBCentral *central, CBCharacteristic *characteristic) { + NSLog(@"peripheralManagerIsReadyToUpdateSubscribers"); + [timer fireDate]; + }]; + +} + +//发送数据,发送当前时间的秒数 +-(BOOL)sendData:(NSTimer *)t { + CBMutableCharacteristic *characteristic = t.userInfo; + NSDateFormatter *dft = [[NSDateFormatter alloc]init]; + [dft setDateFormat:@"ss"]; + // NSLog(@"%@",[dft stringFromDate:[NSDate date]]); + //执行回应Central通知数据 + return [self.baby.peripheralManager updateValue:[[dft stringFromDate:[NSDate date]] dataUsingEncoding:NSUTF8StringEncoding] forCharacteristic:(CBMutableCharacteristic *)characteristic onSubscribedCentrals:nil]; +} + +- (void)setRepresentedObject:(id)representedObject { + [super setRepresentedObject:representedObject]; + + // Update the view, if already loaded. +} + +@end diff --git a/Test/BabyTestStub/BabyTestStub/main.m b/Test/BabyTestStub/BabyTestStub/main.m new file mode 100644 index 0000000..f1a05e8 --- /dev/null +++ b/Test/BabyTestStub/BabyTestStub/main.m @@ -0,0 +1,13 @@ +// +// main.m +// BabyTestStub +// +// Created by ZTELiuyw on 16/3/14. +// Copyright © 2016年 liuyanwei. All rights reserved. +// + +#import + +int main(int argc, const char * argv[]) { + return NSApplicationMain(argc, argv); +} diff --git a/logo.png b/logo.png new file mode 100644 index 0000000..9d82e7d Binary files /dev/null and b/logo.png differ diff --git a/qrcode.jpg b/qrcode.jpg new file mode 100644 index 0000000..f45207f Binary files /dev/null and b/qrcode.jpg differ