这里主要参考这个项目:iOS-nRF-Toolbox,它是Nordic公司开发的测试工程,包含一整套nRF设备的测试解决方案。git
项目是用Swift写的,不过以前仍是有OC版本的,可是后来因为一些**(不可描述的问题),才变成了如今的纯Swift版本。对于使用Swift开发的人员,直接仿照Demo操做便可。若是你是用Swift开发的,那下面的内容你能够不用看了。接下来我就讲一下针对OC引用DFU升级的操做步骤和我遇到的问题。github
nRF-Toolbox项目包含BGM,HRM,HTM,DFU等多个模块,咱们今天只关注其中的DFU升级模块。打开项目,在对应的NORDFUViewController.swift
中咱们可以看到有三个引用库 import UIKit
,import CoreBluetooth
,import iOSDFULibrary
,这里的iOSDFULibrary就是DFU升级的库,也是解决DFU升级最重要的组件。咱们只要把这个库集成到咱们的项目中,就可以完成nRF设备的DFU升级了。swift
有两种方案集成:ruby
第一种方案是做者推荐的,可是我试了好久,引入DFULibrary会出现头文件找不到等一系列问题,无奈只能放弃,若是有人经过这种方式成功,还望告知。下面讲的是经过第二种方案的集成。bash
第一步:导出iOSDFULibraryapp
这一步是最关键也是最容易出问题的,这个库也是由Swift写成的,咱们将这个库clone到本地,而后选择iOSDFULibrary进行编译curl
最后生成两个framework:工具
这时库内的代码已经变成了咱们熟悉的OC语言。理论上这个库应该是没问题的了,可是事实仍是有问题的,见issues#39。做者给出的解决方法是:oop
一、On your mac please install carthage (instructions)测试
二、Create a file named cartfile anywhere on your computer
三、add the following content to the file:
github "NordicSemiconductor/IOS-Pods-DFU-Library" ~> 2.1.2 github "marmelroy/Zip" ~> 0.6 复制代码
一、Open a new terminal and cd to the directory where the file is
二、Enter the command carthage update --platform iOS
三、Carthage will now take care of building your frameworks, the produced .framework files will be found in a newly created directory called Carthage/Build/iOS,copy over iOSDFULibrary.framework and Zip.framework to your project and you are good to go.
carthage是一种和cocoapods类似的的类库管理工具,若是不会使用的话能够参照Demo,将framework文件导入到本身的项目。
第二步、导入framework Target->General
直接拖入项目默认只会导入到Linked Frameworks and Libraries
,咱们还须要在Embeded Binaries中引入。
第三步、使用iOSDFULibrary
//create a DFUFirmware object using a NSURL to a Distribution Packer(ZIP)
DFUFirmware *selectedFirmware = [[DFUFirmware alloc] initWithUrlToZipFile:url];// or
//Use the DFUServiceInitializer to initialize the DFU process.
DFUServiceInitiator *initiator = [[DFUServiceInitiator alloc] initWithCentralManager: centralManager target:selectedPeripheral];
[initiator withFirmware:selectedFirmware];
// Optional:
// initiator.forceDfu = YES/NO; // default NO
// initiator.packetReceiptNotificationParameter = N; // default is 12
initiator.logger = self; // - to get log info
initiator.delegate = self; // - to be informed about current state and errors
initiator.progressDelegate = self; // - to show progress bar
// initiator.peripheralSelector = ... // the default selector is used
DFUServiceController *controller = [initiator start];
复制代码
库中有三个代理方法DFUProgressDelegate
,DFUServiceDelegate
,LoggerDelegate
,它们的做用分别为监视DFU升级进度,DFU升级及蓝牙链接状态,打印状态日志。
一、selectedFirmware返回nil
DFUFirmware *selectedFirmware = [[DFUFirmware alloc] initWithUrlToZipFile:url];
复制代码
须要在General
的Embeded Binaries
选项卡里导入那Zip.framework
和iOSDFULibrary.framework
二、崩溃报错
dyld: Library not loaded: @rpath/libswiftCore.dylibReferenced from: /private/var/containers/Bundle/Application/02516D79-BB30-4278-81B8-3F86BF2AE2A7/XingtelBLE.app/Frameworks/iOSDFULibrary.framework/iOSDFULibraryReason: image not found
复制代码
须要改两个地方
ERROR ITMS-90087: "Unsupported Architectures. The executable for ***.app/Frameworks/SDK.framework contains unsupported architectures '[x86_64, i386]'."
ERROR ITMS-90362: "Invalid Info.plist value. The value for the key 'MinimumOSVersion' in bundle ***.app/Frameworks/SDK.framework is invalid. The minimum value is 8.0"
ERROR ITMS-90209: "Invalid Segment Alignment. The app binary at '***.app/Frameworks/SDK.framework/SDK' does not have proper segment alignment. Try rebuilding the app with the latest Xcode version."
ERROR ITMS-90125: "The binary is invalid. The encryption info in the LC_ENCRYPTION_INFO load command is either missing or invalid, or the binary is already encrypted. This binary does not seem to have been built with Apple's linker."
复制代码
解决方法,添加Run Script Phase
APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}"
# This script loops through the frameworks embedded in the application and
# removes unused architectures.
find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK
do
FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable)
FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME"
echo "Executable is $FRAMEWORK_EXECUTABLE_PATH"
EXTRACTED_ARCHS=()
for ARCH in $ARCHS
do
echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME"
lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH"
EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")
done
echo "Merging extracted architectures: ${ARCHS}"
lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}"
rm "${EXTRACTED_ARCHS[@]}"
echo "Replacing original executable with thinned version"
rm "$FRAMEWORK_EXECUTABLE_PATH"
mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH"
done
复制代码
这个是对应Swift版本用OC写的完整项目,应该是OC中止维护以前的版本。会有一些bug。在将DFUFramework更新以后,我把它搬到了个人github上,有须要的同窗能够下载研究:OC-nRFTool-box。
如下为更新内容,时间:2017.12.26 收到不少关于没法适配Xcode9.2的反馈,由于最近比较忙没时间处理,很差意思啦,今天抽出时间来把代码更新了一下。
一、dyld: Library not loaded: @rpath/libswiftCore.dylib Referenced from: /private/var/containers/Bundle/Application/02516D79-BB30-4278-81B8-3F86BF2AE2A7/XingtelBLE.app/Frameworks/iOSDFULibrary.framework/iOSDFULibrary Reason: image not found
二、DFUFirmware *selectedFirmware = [[DFUFirmware alloc] initWithUrlToZipFile:url]; 返回为空或者崩溃问题
个人测试结果是更新iOSDFULibrary. framework和Zip.framework能够解决以上问题。
由于这两个库都是经过Swift维护的,因此更新framework最好仍是要用适用Swift的方式,包括之后的更新也同样。因此我推荐用Carthage更新这俩库,下面是使用Carthage的简单介绍,详细的能够看这里Carthage的安装和使用。 另外OC-nRFTool-box也已经更新,里面的Framework能够直接拿来用。
一、安装brew
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
复制代码
二、brew更新
brew update
复制代码
三、安装Carthage
brew install carthage
复制代码
四、使用Carthage
cd ~/路径/项目文件夹 /**进入项目文件夹下*/
touch Cartfile /**建立carthage文件*/
open Cartfile /**打开carthage文件*/
/**输入如下内容*/
github "NordicSemiconductor/IOS-Pods-DFU-Library" ~> 4.1
github "marmelroy/Zip" ~> 1.1
复制代码
五、运行Carthage
carthage update --platform iOS /**编译出iOS版本*/
复制代码
六、更新framework
cd Carthage/Build/iOS /**framework输出位置,将老的framework替换掉*/
复制代码
**注意
nRF Toolbox项目方法变动
[initiator withFirmwareFile:selectedFirmware];/** 旧版本方法 */
initiator = [initiator withFirmware:selectedFirmware];/** 新版本方法 */
复制代码
DFU v4.2版本API变动兼容旧版,但标为DEPRECATED
,github项目已经更新。