我们曾经
DFU-Library
以便更早地将固件上传到我们的BLE配件。
但我们的新配件不支持DFU库,我们需要将固件上传到OTA服务中,并根据其各自的特性,我们使用以下代码进行同样的操作,
func prepareChunksToTransfer(fromData: Data) -> [Data] {
var offset = 0
var bytesToSend = fromData.count
let packetSize = BoardInformationService.packetSize
var chunkArray: [Data] = []
repeat {
let chunkLength = min(bytesToSend, packetSize)
let chunk = fromData.subdata(in: Int(offset) ..< Int(offset + chunkLength))
chunkArray.append(chunk)
offset += chunkLength
bytesToSend -= chunkLength
} while bytesToSend > 0
print("BT: Total Chunk size:\(chunkArray.count)")
return chunkArray
}
func uploadNextChunk() {
if let firmwareCharacteristic = fileCharacteristic {
print("Found fileCharacteristic to upload")
if self.currentPacket < self.totalNumberOfPackets {
// Send next chunk
let nextPacket = self.fileByteChunkArray[self.currentPacket]
print("BT: current packet uploading: \(self.currentPacket)/\(self.totalNumberOfPackets) Data:\(nextPacket)")
self.currentPacket += 1
// Write to peripheral
BTDiscovery.sharedInstance().peripheralBLE.writeValue(nextPacket, for: firmwareCharacteristic, type: .withResponse)
} else {
updateCompleteCompletion!()
}
} else {
print("No fileCharacteristic to upload")
}
}
// This is a delegate method which gets called when we get response from our peripheral after sending each packet.
func ackReceivedFromPeripheral() {
uploadNextChunk()
}
但这在iOS上不起作用,但在Android上使用相同的方法。你能告诉我这里可能出了什么问题吗。
预期行为:数据传输完成后,ble设备应启动固件更新,即使传输了完整的数据,iOS上也不会进行固件更新。
仅供参考:外围设备有
esp32