0

我们是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

4

0 回答 0