这适用于 iOS 。
1) - 将 iOSDFULibarary 添加到项目中。
pod 'iOSDFULibrary' (使用 pod)
2) - 导入库import iOSDFULibrary.
3) - 声明以下常量。
static var legacyDfuServiceUUID = CBUUID(string: "00001530-1212-EFDE-1523-785FEABCD123")
static var secureDfuServiceUUID = CBUUID(string: "FE59")
static var deviceInfoServiceUUID = CBUUID(string: "180A")
4) - 使用特定服务扫描蓝牙设备。然后设备将进入 DFU 模式。
func startDiscovery() {
if !scanningStarted {
scanningStarted = true
print("Start discovery")
// By default the scanner shows only devices advertising with one of those Service UUIDs:
centralManager!.delegate = self
centralManager!.scanForPeripherals(
withServices: [
ImpulseUpdateFirmwareVC.legacyDfuServiceUUID,
ImpulseUpdateFirmwareVC.secureDfuServiceUUID,
ImpulseUpdateFirmwareVC.deviceInfoServiceUUID
/*, customServiceUUID*/],
options: [CBCentralManagerScanOptionAllowDuplicatesKey: true])
}
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
switch central.state {
case .unsupported:
print("BLe Unsupported")
break
case .unauthorized:
print("BLe unauthorized")
break
case .poweredOff:
let alertMessgesInst = AlertMessages.sharedInstance
let alert = UIAlertController(title: alertMessgesInst.actofit_Title, message: alertMessgesInst.trun_On_blueTooth, preferredStyle: UIAlertController.Style.alert)
let okAction = UIAlertAction(title: Constants.General.OK, style: .cancel, handler: nil)
alert.addAction(okAction)
self.present(alert, animated: true, completion: nil)
DispatchQueue.main.async {
self.activityIndicator.stopAnimating()
}
break
case .poweredOn:
startDiscovery()
break
case .unknown:
print("BLe unknown")
break
default:
break
}
}
5 - 进入“CBCentralManager”的“didDiscover peripheral”委托方法,将DFU库调用到.
func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
let name = advertisementData[CBAdvertisementDataLocalNameKey] as! String?
// Ignore dupliactes.
// They will not be reported in a single scan, as we scan without CBCentralManagerScanOptionAllowDuplicatesKey flag,
// but after returning from DFU view another scan will be started.
centralManager.stopScan()
let selectedFirmware = DFUFirmware(urlToZipFile: zipFilePathULR!, type: .application)
let initiator = DFUServiceInitiator().with(firmware: selectedFirmware!)
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
let controller = initiator.start(target: peripheral)
}
这将更新 RNF52 芯片组设备的固件。