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

Example with changes to support Android 12 #45

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@ android {

dependencies {
implementation 'androidx.core:core:1.0.2'
implementation 'no.nordicsemi.android:dfu:1.11.0'
implementation 'no.nordicsemi.android:dfu:1.12.0'
}
10 changes: 8 additions & 2 deletions android/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.timeyaa.flutter_nordic_dfu">

<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />

<uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_ADVERTISE" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

<application>
Expand Down
2 changes: 1 addition & 1 deletion 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 30
compileSdkVersion 31

sourceSets {
main.java.srcDirs += 'src/main/kotlin'
Expand Down
8 changes: 7 additions & 1 deletion example/android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,20 @@
to allow setting breakpoints, to provide hot reload, etc.
-->
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.QUERY_ALL_PACKAGES" />
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" />

<!-- io.flutter.app.FlutterApplication is an android.app.Application that
calls FlutterMain.startInitialization(this); in its onCreate method.
In most cases you can leave this as-is, but you if you want to provide
additional functionality it is fine to subclass or reimplement
FlutterApplication and put your custom class here. -->
<application
android:name="io.flutter.app.FlutterApplication"
android:name="${applicationName}"
android:icon="@mipmap/ic_launcher"
android:label="flutter_nordic_dfu_example">
<activity
Expand Down
5 changes: 3 additions & 2 deletions example/android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
buildscript {
ext.kotlin_version = '1.3.50'
ext.kotlin_version = '1.6.20'
repositories {
google()
jcenter()
Expand All @@ -15,7 +15,8 @@ buildscript {
resolutionStrategy.eachDependency { details ->
if (details.requested.group == 'androidx.core'
&& !details.requested.name.contains('androidx') ) {
details.useVersion "1.3.0-alpha01"
//details.useVersion "1.3.0-alpha01"
details.useVersion "1.5.0"
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions example/ios/Runner/Info.plist
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,16 @@
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>NSBluetoothAlwaysUsageDescription</key>
<string>Need BLE permission</string>
<key>NSBluetoothPeripheralUsageDescription</key>
<string>Need BLE permission</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Need Location permission</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Need Location permission</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need Location permission</string>
<key>CFBundleDevelopmentRegion</key>
<string>en</string>
<key>CFBundleExecutable</key>
Expand Down
49 changes: 28 additions & 21 deletions example/lib/generated/i18n.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,22 @@ import 'package:flutter/material.dart';
class S implements WidgetsLocalizations {
const S();

static S current;
static S? current;

static const GeneratedLocalizationsDelegate delegate =
GeneratedLocalizationsDelegate();
GeneratedLocalizationsDelegate();

static S of(BuildContext context) => Localizations.of<S>(context, S);
static S? of(BuildContext context) => Localizations.of<S>(context, S);

@override
TextDirection get textDirection => TextDirection.ltr;

}

class $en extends S {
const $en();
}

class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S?> {
const GeneratedLocalizationsDelegate();

List<Locale> get supportedLocales {
Expand All @@ -36,8 +35,9 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
];
}

LocaleListResolutionCallback listResolution({Locale fallback, bool withCountry = true}) {
return (List<Locale> locales, Iterable<Locale> supported) {
LocaleListResolutionCallback listResolution(
{Locale? fallback, bool withCountry = true}) {
return (List<Locale>? locales, Iterable<Locale> supported) {
if (locales == null || locales.isEmpty) {
return fallback ?? supported.first;
} else {
Expand All @@ -46,26 +46,27 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
};
}

LocaleResolutionCallback resolution({Locale fallback, bool withCountry = true}) {
return (Locale locale, Iterable<Locale> supported) {
LocaleResolutionCallback resolution(
{Locale? fallback, bool withCountry = true}) {
return (Locale? locale, Iterable<Locale> supported) {
return _resolve(locale, fallback, supported, withCountry);
};
}

@override
Future<S> load(Locale locale) {
final String lang = getLang(locale);
Future<S?> load(Locale locale) {
final String? lang = getLang(locale);
if (lang != null) {
switch (lang) {
case "en":
S.current = const $en();
return SynchronousFuture<S>(S.current);
return SynchronousFuture<S?>(S.current);
default:
// NO-OP.
// NO-OP.
}
}
S.current = const S();
return SynchronousFuture<S>(S.current);
return SynchronousFuture<S?>(S.current);
}

@override
Expand All @@ -77,7 +78,8 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
///
/// Internal method to resolve a locale from a list of locales.
///
Locale _resolve(Locale locale, Locale fallback, Iterable<Locale> supported, bool withCountry) {
Locale _resolve(Locale? locale, Locale? fallback, Iterable<Locale> supported,
bool withCountry) {
if (locale == null || !_isSupported(locale, withCountry)) {
return fallback ?? supported.first;
}
Expand Down Expand Up @@ -110,7 +112,9 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
}

// If no country requirement is requested, check if this locale has no country.
if (true != withCountry && (supportedLocale.countryCode == null || supportedLocale.countryCode.isEmpty)) {
if (true != withCountry &&
(supportedLocale.countryCode == null ||
supportedLocale.countryCode!.isEmpty)) {
return true;
}
}
Expand All @@ -119,8 +123,11 @@ class GeneratedLocalizationsDelegate extends LocalizationsDelegate<S> {
}
}

String getLang(Locale l) => l == null
? null
: l.countryCode != null && l.countryCode.isEmpty
? l.languageCode
: l.toString();
String? getLang(Locale l) {
try {
return l.countryCode != null && l.countryCode!.isEmpty
? l.languageCode
: l.toString();
} catch (_) {}
return null;
}
65 changes: 37 additions & 28 deletions example/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import 'dart:async';

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

void main() => runApp(MyApp());

Expand All @@ -12,11 +15,11 @@ class MyApp extends StatefulWidget {
}

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

@override
void initState() {
Expand All @@ -43,6 +46,7 @@ class _MyAppState extends State<MyApp> {
print('deviceAddress: $deviceAddress, percent: $percent');
}),
);
print("HELLLOOOOOOOOOOOOOOOOOOOOOOOOO");
print(s);
dfuRunning = false;
} catch (e) {
Expand All @@ -51,25 +55,30 @@ class _MyAppState extends State<MyApp> {
}
}

void startScan() async{
void startScan() async {
var scanStatus = await Permission.bluetoothScan.request().isGranted;
var bluetoothConnectStatus =
await Permission.bluetoothConnect.request().isGranted;

scanSubscription?.cancel();
await flutterBlue.stopScan();
setState(() {
scanResults.clear();
scanSubscription = flutterBlue.scan().listen(
(scanResult) {
if (scanResults.firstWhere(
(ele) => ele.device.id == scanResult.device.id,
orElse: () => null) !=
null) {
return;
}
setState(() {
/// add result to results if not added
scanResults.add(scanResult);
});
},
);
if (scanStatus && bluetoothConnectStatus) {
scanSubscription = flutterBlue.scan().listen(
(scanResult) {
if (scanResults.firstWhereOrNull(
(ele) => ele.device.id == scanResult.device.id) !=
null) {
return;
}
setState(() {
/// add result to results if not added
scanResults.add(scanResult);
});
},
);
}
});
}

Expand Down Expand Up @@ -150,19 +159,19 @@ class ProgressListenerListener extends DfuProgressListenerAdapter {
}

class DeviceItem extends StatelessWidget {
final ScanResult scanResult;
final ScanResult? scanResult;

final VoidCallback onPress;
final VoidCallback? onPress;

final bool isRunningItem;
final bool? isRunningItem;

DeviceItem({this.scanResult, this.onPress, this.isRunningItem});

@override
Widget build(BuildContext context) {
var name = "Unknow";
if (scanResult.device.name != null && scanResult.device.name.length > 0) {
name = scanResult.device.name;
var name = "Unknown";
if (scanResult!.device.name != null && scanResult!.device.name.length > 0) {
name = scanResult!.device.name;
}
return Card(
child: Padding(
Expand All @@ -175,14 +184,14 @@ class DeviceItem extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Text(name),
Text(scanResult.device.id.id),
Text("RSSI: ${scanResult.rssi}"),
Text(scanResult!.device.id.id),
Text("RSSI: ${scanResult!.rssi}"),
],
),
),
TextButton(
onPressed: onPress,
child: isRunningItem ? Text("Abort Dfu") : Text("Start Dfu"))
child: isRunningItem! ? Text("Abort Dfu") : Text("Start Dfu"))
],
),
),
Expand Down
11 changes: 7 additions & 4 deletions example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,18 @@ description: Demonstrates how to use the flutter_nordic_dfu plugin.
publish_to: 'none'

environment:
sdk: ">=2.0.0-dev.68.0 <3.0.0"
sdk: '>=2.12.0 <3.0.0'

dependencies:
flutter:
sdk: flutter

# The following adds the Cupertino Icons font to your application.
# Use with the CupertinoIcons class for iOS style icons.
cupertino_icons: ^0.1.2

flutter_blue: ^0.8.0
cupertino_icons: ^1.0.4
flutter_blue_plus: ^1.1.2
collection: ^1.15.0
permission_handler: ^9.2.0

dev_dependencies:
flutter_test:
Expand All @@ -22,6 +23,8 @@ dev_dependencies:
flutter_nordic_dfu:
path: ../

flutter_lints: ^1.0.4

# For information on the generic Dart part of this file, see the
# following page: https://www.dartlang.org/tools/pub/pubspec

Expand Down
2 changes: 1 addition & 1 deletion example/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void main() {
expect(
find.byWidgetPredicate(
(Widget widget) => widget is Text &&
widget.data.startsWith('Running on:'),
widget.data!.startsWith('Running on:'),
),
findsOneWidget,
);
Expand Down
Binary file added lib/assets/file.zip
Binary file not shown.
Loading