Skip to content

Commit

Permalink
Merge pull request #30 from juliansteenbakker/lint
Browse files Browse the repository at this point in the history
Upgrade Android dependency
  • Loading branch information
juliansteenbakker authored Apr 13, 2022
2 parents ba01735 + 0503cd6 commit 5649dc0
Show file tree
Hide file tree
Showing 16 changed files with 193 additions and 170 deletions.
11 changes: 9 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
## 3.3.1
* Upgraded Android Bluetooth Permissions
## 4.0.0
BREAKING CHANGES:
NordiDfu now uses a Singelton! The notation changes from NordicDfu.startDfu() to NordicDfu().startDfu()

New Features:
* Upgraded Nordic-DFU-Library to 2.0.2
* Upgraded Android Bluetooth Permissions.
* Upgraded other minor dependencies.
* Upgraded flutter_lints to lint for stricter analyzer.

## 3.3.0
* Upgraded Android Dependency to 1.12.1-beta01
Expand Down
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,15 @@
# nordic-dfu [![pub package](https://img.shields.io/pub/v/nordic_dfu.svg)](https://pub.dev/packages/nordic_dfu)
# nordic_dfu
[![style: lint](https://img.shields.io/badge/style-lint-4BC0F5.svg)](https://pub.dev/packages/lint)
[![pub package](https://img.shields.io/pub/v/nordic_dfu.svg)](https://pub.dev/packages/nordic_dfu)
[![mobile_scanner](https://github.com/juliansteenbakker/nordic_dfu/actions/workflows/flutter_format.yml/badge.svg)](https://github.com/juliansteenbakker/nordic_dfu/actions/workflows/flutter_format.yml)
[![GitHub Sponsors](https://img.shields.io/github/sponsors/juliansteenbakker?label=want%20support%3F%20sponsor%20me%20and%20I%27ll%20contact%20you%21)](https://github.com/sponsors/juliansteenbakker)

Fork from [flutter_nordic_dfu](https://pub.dev/packages/flutter_nordic_dfu).
## 4.0.0 Breaking changes!
From version 4.0.0, the NordicDfu class uses a Singleton.
If you are comming from an older version, you have to change the notation from NordicDfu.startDfu() to NordicDfu().startDfu().


Fork from [flutter_nordic_dfu](https://pub.dev/packages/flutter_nordic_dfu) and updated with latest dependencies.

This library allows you to do a Device Firmware Update (DFU) of your nrf51 or
nrf52 chip from Nordic Semiconductor. It works for Android and iOS.
Expand Down Expand Up @@ -28,7 +37,7 @@ You can pass an absolute file path or asset file to `NordicDfu`

```dart
/// You can define your ProgressListenerListener
await NordicDfu.startDfu(
await NordicDfu().startDfu(
'EB:75:AD:E3:CA:CF', '/file/to/zip/path/file.zip',
progressListener: ProgressListenerListener(),
);
Expand All @@ -45,7 +54,7 @@ class ProgressListenerListener extends DfuProgressListenerAdapter {
}
/// Or you can use DefaultDfuProgressListenerAdapter
await NordicDfu.startDfu(
await NordicDfu().startDfu(
'EB:75:AD:E3:CA:CF',
'assets/file.zip',
fileInAsset: true,
Expand All @@ -67,7 +76,7 @@ await NordicDfu.startDfu(

```dart
/// just set [fileInAsset] true
await NordicDfu.startDfu(
await NordicDfu().startDfu(
'EB:75:AD:E3:CA:CF', 'assets/file.zip',
progressListener: ProgressListenerListener(),
fileInAsset: true,
Expand Down
2 changes: 1 addition & 1 deletion analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:flutter_lints/flutter.yaml
include: package:lint/analysis_options_package.yaml
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ apply plugin: 'com.android.library'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 31
compileSdkVersion 32
sourceSets {
main.java.srcDirs += 'src/main/kotlin'
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import android.app.NotificationManager
import android.content.Context
import android.os.Build
import android.os.Handler
import android.os.Looper
import io.flutter.FlutterInjector
import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.FlutterPlugin.FlutterPluginBinding
Expand Down Expand Up @@ -72,7 +73,11 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler {
val tempFileName = (PathUtils.getExternalAppCachePath(mContext!!)
+ UUID.randomUUID().toString())
// copy asset file to temp path
ResourceUtils.copyFileFromAssets(filePath, tempFileName, mContext!!)
if (!ResourceUtils.copyFileFromAssets(filePath, tempFileName, mContext!!)) {
result.error("File Error", "File not found!", "$filePath")
return
}

// now, the path is an absolute path, and can pass it to nordic dfu libarary
filePath = tempFileName
}
Expand Down Expand Up @@ -226,7 +231,7 @@ class NordicDfuPlugin : FlutterPlugin, MethodCallHandler {

private fun cancelNotification() {
// let's wait a bit until we cancel the notification. When canceled immediately it will be recreated by service again.
Handler().postDelayed({
Handler(Looper.getMainLooper()).postDelayed({
val manager = mContext!!.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
manager.cancel(NOTIFICATION_ID)
}, 200)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@ object ResourceUtils {
} else {
res = writeFileFromIS(
destFilePath,
context.applicationContext.assets.open(assetsFilePath),
false
context.applicationContext.assets.open(assetsFilePath)
)
}
} catch (e: IOException) {
} catch (e: IOException ) {
e.printStackTrace()
res = false
} catch (f: FileNotFoundException) {
f.printStackTrace()
res = false
}
return res
}
Expand All @@ -40,18 +42,16 @@ object ResourceUtils {
// other utils methods
///////////////////////////////////////////////////////////////////////////
private fun writeFileFromIS(filePath: String,
`is`: InputStream,
append: Boolean): Boolean {
return writeFileFromIS(getFileByPath(filePath), `is`, append)
`is`: InputStream): Boolean {
return writeFileFromIS(getFileByPath(filePath), `is`)
}

private fun writeFileFromIS(file: File?,
`is`: InputStream?,
append: Boolean): Boolean {
`is`: InputStream?): Boolean {
if (!createOrExistsFile(file) || `is` == null) return false
var os: OutputStream? = null
return try {
os = BufferedOutputStream(FileOutputStream(file, append))
os = BufferedOutputStream(FileOutputStream(file, false))
val data = ByteArray(BUFFER_SIZE)
var len: Int
while (`is`.read(data, 0, BUFFER_SIZE).also { len = it } != -1) {
Expand Down
2 changes: 1 addition & 1 deletion example/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -1 +1 @@
include: package:flutter_lints/flutter.yaml
include: package:lint/analysis_options.yaml
4 changes: 2 additions & 2 deletions example/android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android'
apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle"

android {
compileSdkVersion 31
compileSdkVersion 32

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand All @@ -40,7 +40,7 @@ android {
// TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html).
applicationId "dev.steenbakker.nordicdfuexample"
minSdkVersion 19
targetSdkVersion 31
targetSdkVersion 32
versionCode flutterVersionCode.toInteger()
versionName flutterVersionName
}
Expand Down
4 changes: 0 additions & 4 deletions example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
Expand Down
4 changes: 2 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
buildscript {
ext.kotlin_version = '1.6.10'
ext.kotlin_version = '1.6.20'
repositories {
google()
mavenCentral()
}

dependencies {
classpath 'com.android.tools.build:gradle:7.0.4'
classpath 'com.android.tools.build:gradle:7.1.2'
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
}
}
Expand Down
4 changes: 2 additions & 2 deletions example/android/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri Jan 14 10:31:27 CET 2022
#Thu Apr 07 10:20:39 CEST 2022
distributionBase=GRADLE_USER_HOME
distributionUrl=https\://services.gradle.org/distributions/gradle-7.3.3-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-7.4.2-bin.zip
distributionPath=wrapper/dists
zipStorePath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
98 changes: 56 additions & 42 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import 'dart:async';

import 'package:collection/collection.dart' show IterableExtension;
import 'package:flutter/material.dart';
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
import 'package:nordic_dfu/nordic_dfu.dart';
import 'package:flutter_blue/flutter_blue.dart';
import 'package:permission_handler/permission_handler.dart';

void main() => runApp(const MyApp());
Expand All @@ -16,36 +16,32 @@ class MyApp extends StatefulWidget {
}

class _MyAppState extends State<MyApp> {
final FlutterBlue flutterBlue = FlutterBlue.instance;
final FlutterBluePlus flutterBlue = FlutterBluePlus.instance;
StreamSubscription<ScanResult>? scanSubscription;
List<ScanResult> scanResults = <ScanResult>[];
bool dfuRunning = false;
int? dfuRunningInx;

@override
void initState() {
super.initState();
}

Future<void> doDfu(String deviceId) async {
stopScan();
dfuRunning = true;
try {
var s = await NordicDfu.startDfu(
final s = await NordicDfu().startDfu(
deviceId,
'assets/file.zip',
fileInAsset: true,
progressListener:
DefaultDfuProgressListenerAdapter(onProgressChangedHandle: (
deviceAddress,
percent,
speed,
avgSpeed,
currentPart,
partsTotal,
) {
debugPrint('deviceAddress: $deviceAddress, percent: $percent');
}),
progressListener: DefaultDfuProgressListenerAdapter(
onProgressChangedHandle: (
deviceAddress,
percent,
speed,
avgSpeed,
currentPart,
partsTotal,
) {
debugPrint('deviceAddress: $deviceAddress, percent: $percent');
},
),
);
debugPrint(s);
dfuRunning = false;
Expand All @@ -70,7 +66,8 @@ class _MyAppState extends State<MyApp> {
scanSubscription = flutterBlue.scan().listen(
(scanResult) {
if (scanResults.firstWhereOrNull(
(ele) => ele.device.id == scanResult.device.id) !=
(ele) => ele.device.id == scanResult.device.id,
) !=
null) {
return;
}
Expand Down Expand Up @@ -100,15 +97,16 @@ class _MyAppState extends State<MyApp> {
appBar: AppBar(
title: const Text('Plugin example app'),
actions: <Widget>[
isScanning
? IconButton(
icon: const Icon(Icons.pause_circle_filled),
onPressed: dfuRunning ? null : stopScan,
)
: IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: dfuRunning ? null : startScan,
)
if (isScanning)
IconButton(
icon: const Icon(Icons.pause_circle_filled),
onPressed: dfuRunning ? null : stopScan,
)
else
IconButton(
icon: const Icon(Icons.play_arrow),
onPressed: dfuRunning ? null : startScan,
)
],
),
body: !hasDevice
Expand All @@ -126,13 +124,13 @@ class _MyAppState extends State<MyApp> {
}

Widget _deviceItemBuilder(BuildContext context, int index) {
var result = scanResults[index];
final result = scanResults[index];
return DeviceItem(
isRunningItem: (dfuRunningInx == null ? false : dfuRunningInx == index),
isRunningItem: dfuRunningInx == index,
scanResult: result,
onPress: dfuRunning
? () async {
await NordicDfu.abortDfu();
await NordicDfu().abortDfu();
setState(() {
dfuRunningInx = null;
});
Expand All @@ -152,10 +150,22 @@ class _MyAppState extends State<MyApp> {

class ProgressListenerListener extends DfuProgressListenerAdapter {
@override
void onProgressChanged(String? deviceAddress, int? percent, double? speed,
double? avgSpeed, int? currentPart, int? partsTotal) {
void onProgressChanged(
String? deviceAddress,
int? percent,
double? speed,
double? avgSpeed,
int? currentPart,
int? partsTotal,
) {
super.onProgressChanged(
deviceAddress, percent, speed, avgSpeed, currentPart, partsTotal);
deviceAddress,
percent,
speed,
avgSpeed,
currentPart,
partsTotal,
);
debugPrint('deviceAddress: $deviceAddress, percent: $percent');
}
}
Expand All @@ -167,9 +177,12 @@ class DeviceItem extends StatelessWidget {

final bool? isRunningItem;

const DeviceItem(
{required this.scanResult, this.onPress, this.isRunningItem, Key? key})
: super(key: key);
const DeviceItem({
required this.scanResult,
this.onPress,
this.isRunningItem,
Key? key,
}) : super(key: key);

@override
Widget build(BuildContext context) {
Expand All @@ -194,10 +207,11 @@ class DeviceItem extends StatelessWidget {
),
),
TextButton(
onPressed: onPress,
child: isRunningItem!
? const Text('Abort Dfu')
: const Text('Start Dfu'))
onPressed: onPress,
child: isRunningItem!
? const Text('Abort Dfu')
: const Text('Start Dfu'),
)
],
),
),
Expand Down
Loading

0 comments on commit 5649dc0

Please sign in to comment.