0

We were DFU-Library to upload firmware to our BLE accessory earlier. But our new set of accessories doesn't support DFU library and we need to upload the firmware to an OTA service in its respective characteristics and we does the same using following code,

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()
}

But this doesn't work on iOS but the same approach working on Android. Can you please guide me what could be wrong here.

Expected behaviour: After data transfer gets completed the ble device should start the firmware update which is not occurring on iOS even though full data is transferred.

FYI: Peripheral has esp32

Bharath
  • 2,064
  • 1
  • 14
  • 39
  • We need more information please; what does not work precisely ? – Adam Sep 27 '21 at 20:54
  • Hi @Adam, Thank you for letting me know, Actually after the data transfer gets completed the ble device is supposed to start firmware update, but that doesn't happen in iOS even though full data is transferred. – Bharath Sep 27 '21 at 20:58
  • Are you sure that the data you're sending is fully written ? Then, don't you have another command to send to indicate that the upload finished and is ready to install ? Or is it handled automatically ? What does the `updateCompleteCompletion!()` do ? – Adam Sep 27 '21 at 21:02
  • Yes @Adam, the data is written in full, updateCompleteCompletion!() is for notifying that last set of data is transferred. And no additional command needs to be sent after the data is uploaded. – Bharath Sep 27 '21 at 21:07
  • @Adam: These where all set of questions which we found and rolled out. And I guess we are near to the solution, Just few more tries. Because we debugged the data sent by both the platform and compared the byteArray and there is the problem. https://stackoverflow.com/questions/69353145/why-is-ios-android-bytearray-content-of-same-file-is-different-in-each-platfor?noredirect=1#comment122580251_69353145 – Bharath Sep 27 '21 at 21:09
  • Ok I see. I hope you will find the issue. – Adam Sep 27 '21 at 21:15
  • Thank you @Adam – Bharath Sep 27 '21 at 21:18

0 Answers0