Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Starting DFU does nothing after update failed #85

Open
Torvaldi opened this issue Mar 15, 2023 · 0 comments
Open

Starting DFU does nothing after update failed #85

Torvaldi opened this issue Mar 15, 2023 · 0 comments

Comments

@Torvaldi
Copy link

Hello,

I got a problem using this library. When I am launching NordicDfu().startDfu(...) and then unplug the Nordic board, I am unable to relaunch DFU update until I completely kill and restart the application...

Here is the code used to launch DFU:

import 'package:flutter/material.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:nordic_dfu/nordic_dfu.dart';
import 'package:poc_ble/DevicePage/BottomSelectUpdate.dart';
import 'package:poc_ble/Logger/CustomLogger.dart';

class BottomLaunchUpdate extends StatefulWidget {
  const BottomLaunchUpdate(
      {Key? key, required this.deviceId, required this.firmware})
      : super(key: key);
  final DeviceIdentifier deviceId;
  final UpdateFirmwareType firmware;

  @override
  State<BottomLaunchUpdate> createState() => _BottomLaunchUpdateState();
}

class _BottomLaunchUpdateState extends State<BottomLaunchUpdate> {
  final FlutterBlue flutterBlue = FlutterBlue.instance;
  int _progress = 0;
  bool _isDfuFinished = true;
  bool _dfuHasError = false;

  String getFilePath() {
    customLogger.d("GETTING FILE PATH for ${widget.firmware}");
    switch (widget.firmware) {
      case UpdateFirmwareType.blinky1:
        return "assets/app_dfu_package_blinky1_SDK17.zip";
      case UpdateFirmwareType.blinky2:
        return "assets/app_dfu_package_blinky2_SDK17.zip";
      default:
        return "assets/app_dfu_package_blinky1_SDK17.zip";
    }
  }

  @override
  void dispose() {
    super.dispose();

    if (!_isDfuFinished) {
      NordicDfu().abortDfu();
      flutterBlue.connectedDevices.then((value) {
        value.forEach((element) {
          element.disconnect();
        });
      });
    }
  }

  void sendFirmware() async {
    customLogger.d("LAUNCHING DFU UPDATE");

    setState(() {
      _isDfuFinished = false;
    });

    try {
      final s = await NordicDfu().startDfu(
          widget.deviceId.id.toString(), getFilePath(),
          fileInAsset: true,
          onDfuCompleted: handleDfuCompleted,
          onDfuAborted: handleDfuAborted,
          onProgressChanged: handleProgressChanged,
          onDfuProcessStarted: (String deviceAddress) =>
              customLogger.d("DFU PROCESS STARTED"),
          onDeviceConnected: (String deviceAddress) =>
              customLogger.d("DEVICE CONNECTED"),
          onDeviceConnecting: (String deviceAddress) =>
              customLogger.d("DEVICE CONNECTING"),
          onDeviceDisconnected: (String deviceAddress) =>
              customLogger.d("DEVICE DISCONNECTED"),
          onDeviceDisconnecting: handleDeviceDisconnecting,
          onDfuProcessStarting: (String deviceAddress) =>
              customLogger.d("DFU PROCESS STARTING"),
          onEnablingDfuMode: (String deviceAddress) =>
              customLogger.d("ENABLING DFU MODE"),
          onFirmwareValidating: (String deviceAddress) =>
              customLogger.d("FIRMWARE VALIDATING"),
          onError: handleError);

      customLogger.d("DFU STARTED: $s");
      setState(() {
        _isDfuFinished = true;
      });
    } catch (e) {
      customLogger.e("ERROR: $e");
      setState(() {
        _dfuHasError = true;
      });
    }
  }

  void handleDfuCompleted(String deviceAddress) {
    customLogger.d("DFU COMPLETED");
    setState(() {
      _isDfuFinished = true;
    });
    Navigator.pop(context);
  }

  void handleProgressChanged(String deviceAddress, int percent, double speed,
      double avgSpeed, int currentPart, int partsTotal) {
    customLogger.d("PROGRESS CHANGED");
    setState(() {
      _progress = percent;
    });
  }

  void handleError(
      String deviceAddress, int error, int errorType, String message) {
    customLogger.e("ERROR: $message");
    setState(() {
      _dfuHasError = true;
    });
  }

  void handleDeviceDisconnecting(String deviceAddress) {
    customLogger.d("DEVICE DISCONNECTING");
    setState(() {
      _dfuHasError = true;
    });
  }

  void handleDfuAborted(String deviceAddress) {
    customLogger.d("DFU ABORTED");
    setState(() {
      _dfuHasError = true;
    });
  }

  @override
  Widget build(BuildContext context) {
    if (_dfuHasError) return const Text("Got an error during DFU");
    return Column(
      children: !_isDfuFinished
          ? [
              const Text("Updating...", style: TextStyle(fontSize: 30)),
              const SizedBox(height: 20),
              LinearProgressIndicator(value: _progress / 100),
              const SizedBox(height: 20),
              Text("$_progress %"),
            ]
          : [
              ElevatedButton(
                onPressed: sendFirmware,
                child: const Text("Update"),
              ),
            ],
    );
  }
}

Can I have some more information of what I can do to solve this? Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant