updateFirmware method

Stream<double> updateFirmware(
  1. String filePath
)

Updates the firmware of the device.

Implementation

Stream<double> updateFirmware(String filePath) async* {
  try {
    yield 0;

    _log.info("Starting firmware update");

    if (state != BrilliantConnectionState.dfuConnected) {
      throw ("DFU device is not connected");
    }

    if (_dfuControl == null || _dfuPacket == null) {
      throw ("Device is not in DFU mode");
    }

    final updateZipFile = await rootBundle.load(filePath);
    final zip = ZipDecoder().decodeBytes(updateZipFile.buffer.asUint8List());

    final initFile = zip.firstWhere((file) => file.name.endsWith(".dat"));
    final imageFile = zip.firstWhere((file) => file.name.endsWith(".bin"));

    await for (var _ in _transferDfuFile(initFile.content, true)) {}
    await Future.delayed(const Duration(milliseconds: 500));
    await for (var value in _transferDfuFile(imageFile.content, false)) {
      yield value;
    }

    _log.info("Firmware update completed");
  } catch (error) {
    _log.warning("Couldn't complete firmware update. $error");
    yield* Stream.error(BrilliantBluetoothException(error.toString()));
  }
}