Skip to content

Commit

Permalink
Merge pull request #147 from chan150/master
Browse files Browse the repository at this point in the history
Improve example code
  • Loading branch information
juliansteenbakker authored Aug 8, 2024
2 parents 67a11aa + 661fe44 commit 80ff886
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 53 deletions.
98 changes: 49 additions & 49 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import 'dart:async';
import 'dart:io';

import 'package:collection/collection.dart';
import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:nordic_dfu/nordic_dfu.dart';
Expand All @@ -24,25 +26,28 @@ class _MyAppState extends State<MyApp> {
Future<void> doDfu(String deviceId) async {
stopScan();
dfuRunning = true;

final result = await FilePicker.platform.pickFiles();

if (result == null) return;
try {
final s = await NordicDfu().startDfu(
deviceId,
'assets/file.zip',
fileInAsset: true,
result.files.single.path ?? '',
onDeviceDisconnecting: (string) {
debugPrint('deviceAddress: $string');
},
// onErrorHandle: (string) {
// debugPrint('deviceAddress: $string');
// },
onProgressChanged: (
deviceAddress,
percent,
speed,
avgSpeed,
currentPart,
partsTotal,
) {
deviceAddress,
percent,
speed,
avgSpeed,
currentPart,
partsTotal,
) {
debugPrint('deviceAddress: $deviceAddress, percent: $percent');
},
// androidSpecialParameter: const AndroidSpecialParameter(rebootTime: 1000),
Expand All @@ -68,24 +73,19 @@ class _MyAppState extends State<MyApp> {
}

scanSubscription?.cancel();
setState(() {
scanResults.clear();
// ignore: deprecated_member_use
scanSubscription = FlutterBluePlus.scan(allowDuplicates: true).listen(
(scanResult) {
if (scanResults.firstWhereOrNull(
(ele) => ele.device.remoteId == scanResult.device.remoteId,
) !=
null) {
return;
}
setState(() {
/// add result to results if not added
scanResults.add(scanResult);
});
},
);
});
FlutterBluePlus.startScan();
scanResults.clear();
scanSubscription = FlutterBluePlus.scanResults.expand((e) => e).listen(
(scanResult) {
if (scanResults.firstWhereOrNull((ele) => ele.device.remoteId == scanResult.device.remoteId) != null) {
return;
}
setState(() {
/// add result to results if not added
scanResults.add(scanResult);
});
},
);
}

void stopScan() {
Expand Down Expand Up @@ -119,14 +119,14 @@ class _MyAppState extends State<MyApp> {
),
body: !hasDevice
? const Center(
child: Text('No device'),
)
child: Text('No device'),
)
: ListView.separated(
padding: const EdgeInsets.all(8),
itemBuilder: _deviceItemBuilder,
separatorBuilder: (context, index) => const SizedBox(height: 5),
itemCount: scanResults.length,
),
padding: const EdgeInsets.all(8),
itemBuilder: _deviceItemBuilder,
separatorBuilder: (context, index) => const SizedBox(height: 5),
itemCount: scanResults.length,
),
),
);
}
Expand All @@ -138,20 +138,20 @@ class _MyAppState extends State<MyApp> {
scanResult: result,
onPress: dfuRunning
? () async {
await NordicDfu().abortDfu();
setState(() {
dfuRunningInx = null;
});
}
await NordicDfu().abortDfu();
setState(() {
dfuRunningInx = null;
});
}
: () async {
setState(() {
dfuRunningInx = index;
});
await doDfu(result.device.remoteId.str);
setState(() {
dfuRunningInx = null;
});
},
setState(() {
dfuRunningInx = index;
});
await doDfu(result.device.remoteId.str);
setState(() {
dfuRunningInx = null;
});
},
);
}
}
Expand Down Expand Up @@ -195,8 +195,8 @@ class DeviceItem extends StatelessWidget {
@override
Widget build(BuildContext context) {
var name = 'Unknown';
if (scanResult.device.localName.isNotEmpty) {
name = scanResult.device.localName;
if (scanResult.device.platformName.isNotEmpty) {
name = scanResult.device.platformName;
}
return Card(
child: Padding(
Expand Down
9 changes: 5 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,19 @@ environment:
sdk: '>=2.12.0 <4.0.0'

dependencies:
collection: ^1.15.0
collection: ">=1.15.0"
file_picker: ">=8.0.2"
flutter:
sdk: flutter
flutter_blue_plus: 1.14.24
flutter_blue_plus: ">=1.32.4 <1.40.0"
nordic_dfu:
path: ../
permission_handler: ^10.4.3
permission_handler: ">=10.4.3"

dev_dependencies:
flutter_test:
sdk: flutter
lint: ^2.1.2
lint: ">=2.1.2"

flutter:
uses-material-design: true
Expand Down

0 comments on commit 80ff886

Please sign in to comment.