dfu.dart 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. import 'dart:io';
  2. import 'package:flutter/cupertino.dart';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_reactive_ble/flutter_reactive_ble.dart';
  5. import 'package:nordic_dfu/nordic_dfu.dart';
  6. import 'package:nrf/app_subscription_state.dart';
  7. class DFU extends StatefulWidget {
  8. final DiscoveredDevice device;
  9. final File file;
  10. const DFU({Key? key, required this.device, required this.file}) : super(key: key);
  11. @override
  12. State<StatefulWidget> createState() => _State();
  13. }
  14. class _State extends State<DFU> with SubscriptionState {
  15. final ScrollController _scrollController = ScrollController();
  16. final List<String> _log = [];
  17. late int start;
  18. @override
  19. void initState() {
  20. super.initState();
  21. doDfu(widget.device.id, widget.file.path);
  22. }
  23. _addLog(String msg) {
  24. setState(() {
  25. int now = DateTime.now().millisecondsSinceEpoch;
  26. _log.add("${(now - start).toString().padLeft(8, "0")} -- $msg");
  27. _scrollController.jumpTo(_scrollController.position.maxScrollExtent);
  28. });
  29. }
  30. Future<String?> doDfu(String deviceId, String filePath) async {
  31. await Future.delayed(const Duration(seconds: 3));
  32. start = DateTime.now().millisecondsSinceEpoch;
  33. _addLog("DFU start!");
  34. return await NordicDfu().startDfu(
  35. deviceId,
  36. filePath,
  37. fileInAsset: false,
  38. onEnablingDfuMode: (deviceAddress) {
  39. _addLog("EnablingDfuMode");
  40. },
  41. onDfuProcessStarted: (deviceAddress) {
  42. _addLog("DfuProcessStarted");
  43. },
  44. onDfuProcessStarting: (deviceAddress) {
  45. _addLog("DfuProcessStarting");
  46. },
  47. onDeviceConnecting: (deviceAddress) {
  48. _addLog("DeviceConnecting");
  49. },
  50. onDeviceConnected: (deviceAddress) {
  51. _addLog("DeviceConnected");
  52. },
  53. onDfuCompleted: (deviceAddress) {
  54. _addLog("DfuCompleted");
  55. },
  56. onFirmwareValidating: (deviceAddress) {
  57. _addLog("FirmwareValidating");
  58. },
  59. onProgressChanged: (
  60. deviceAddress,
  61. percent,
  62. speed,
  63. avgSpeed,
  64. currentPart,
  65. partsTotal,
  66. ) {
  67. _addLog("ProgressChanged $percent ${speed.toStringAsFixed(2)} ${avgSpeed.toStringAsFixed(2)} $currentPart $partsTotal");
  68. },
  69. onError: (
  70. String? deviceAddress,
  71. int? error,
  72. int? errorType,
  73. String? message,
  74. ) {
  75. _addLog("Error $error $errorType $message");
  76. },
  77. );
  78. }
  79. @override
  80. Widget build(BuildContext context) {
  81. return Scaffold(
  82. appBar: AppBar(
  83. title: Text("${widget.device.name}"),
  84. ),
  85. body: ListView.builder(
  86. controller: _scrollController,
  87. itemBuilder: (context, index) {
  88. return Padding(
  89. padding: const EdgeInsets.symmetric(vertical: 4.0),
  90. child: Text(_log[index]),
  91. );
  92. },
  93. itemCount: _log.length,
  94. ),
  95. );
  96. }
  97. }