From aa016ed0ad25f827189d4843ab92a56e7a8cff3d Mon Sep 17 00:00:00 2001 From: Ahmed Mirza Date: Sat, 3 Oct 2020 13:38:49 +0500 Subject: [PATCH 1/3] add is nfc enable method --- android/.project | 28 +++++ .../org.eclipse.buildship.core.prefs | 13 +++ .../nfc_in_flutter/NfcInFlutterPlugin.java | 11 +- example/android/.project | 28 +++++ .../org.eclipse.buildship.core.prefs | 13 +++ example/pubspec.lock | 101 ++++++------------ lib/src/api.dart | 7 ++ pubspec.lock | 97 ++++++----------- 8 files changed, 162 insertions(+), 136 deletions(-) create mode 100644 android/.project create mode 100644 android/.settings/org.eclipse.buildship.core.prefs create mode 100644 example/android/.project create mode 100644 example/android/.settings/org.eclipse.buildship.core.prefs diff --git a/android/.project b/android/.project new file mode 100644 index 0000000..eb3eaf1 --- /dev/null +++ b/android/.project @@ -0,0 +1,28 @@ + + + android_ + Project android_ created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + + + 1601710681614 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/android/.settings/org.eclipse.buildship.core.prefs b/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..36ea04f --- /dev/null +++ b/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(WRAPPER) +connection.project.dir= +eclipse.preferences.version=1 +gradle.user.home= +java.home=/Library/Java/JavaVirtualMachines/adoptopenjdk-11-openj9.jdk/Contents/Home +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java b/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java index 0882fcc..6245b6c 100644 --- a/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java +++ b/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java @@ -76,6 +76,9 @@ private NfcInFlutterPlugin(Activity activity) { public void onMethodCall(MethodCall call, Result result) { switch (call.method) { case "readNDEFSupported": + result.success(nfcIsSupported()); + break; + case "readNDEFEnabled": result.success(nfcIsEnabled()); break; case "startNDEFReading": @@ -137,8 +140,12 @@ public void onMethodCall(MethodCall call, Result result) { private Boolean nfcIsEnabled() { NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); - if (adapter == null) return false; - return adapter.isEnabled(); + return adapter != null && adapter.isEnabled(); + } + + private Boolean nfcIsSupported() { + NfcAdapter adapter = NfcAdapter.getDefaultAdapter(activity); + return adapter != null; } private void startReading(boolean noSounds) { diff --git a/example/android/.project b/example/android/.project new file mode 100644 index 0000000..c5b4567 --- /dev/null +++ b/example/android/.project @@ -0,0 +1,28 @@ + + + android + Project android created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + + + 1601710681609 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/example/android/.settings/org.eclipse.buildship.core.prefs b/example/android/.settings/org.eclipse.buildship.core.prefs new file mode 100644 index 0000000..97a9583 --- /dev/null +++ b/example/android/.settings/org.eclipse.buildship.core.prefs @@ -0,0 +1,13 @@ +arguments= +auto.sync=false +build.scans.enabled=false +connection.gradle.distribution=GRADLE_DISTRIBUTION(VERSION(6.3)) +connection.project.dir= +eclipse.preferences.version=1 +gradle.user.home= +java.home=/Library/Java/JavaVirtualMachines/adoptopenjdk-11-openj9.jdk/Contents/Home +jvm.arguments= +offline.mode=false +override.workspace.settings=true +show.console.view=true +show.executions.view=true diff --git a/example/pubspec.lock b/example/pubspec.lock index ae726eb..0260854 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -1,62 +1,48 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" - charcode: + version: "2.1.0-nullsafety.1" + characters: dependency: transitive description: - name: charcode + name: characters url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.1.0-nullsafety.3" + charcode: dependency: transitive description: - name: collection + name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: convert + name: clock url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: crypto + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.15.0-nullsafety.3" cupertino_icons: dependency: "direct main" description: @@ -64,6 +50,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.2" + fake_async: + dependency: transitive + description: + name: fake_async + url: "https://pub.dartlang.org" + source: hosted + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -74,27 +67,20 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" nfc_in_flutter: dependency: "direct dev" description: @@ -108,21 +94,7 @@ packages: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -134,63 +106,56 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0" flutter: ">=1.10.0" diff --git a/lib/src/api.dart b/lib/src/api.dart index 73bf4d5..406546a 100644 --- a/lib/src/api.dart +++ b/lib/src/api.dart @@ -224,6 +224,13 @@ class NFC { assert(supported is bool); return supported as bool; } + + /// isNDEFEnabled checks if the device supports and enable reading NDEF tags + static Future get isNDEFEnabled async { + final supported = await _channel.invokeMethod("readNDEFEnabled"); + assert(supported is bool); + return supported as bool; + } } /// NFCReaderMode is an interface for different reading modes diff --git a/pubspec.lock b/pubspec.lock index 8ef651e..706519b 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -1,62 +1,55 @@ # Generated by pub # See https://dart.dev/tools/pub/glossary#lockfile packages: - archive: - dependency: transitive - description: - name: archive - url: "https://pub.dartlang.org" - source: hosted - version: "2.0.13" - args: - dependency: transitive - description: - name: args - url: "https://pub.dartlang.org" - source: hosted - version: "1.6.0" async: dependency: transitive description: name: async url: "https://pub.dartlang.org" source: hosted - version: "2.4.1" + version: "2.5.0-nullsafety.1" boolean_selector: dependency: transitive description: name: boolean_selector url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" + characters: + dependency: transitive + description: + name: characters + url: "https://pub.dartlang.org" + source: hosted + version: "1.1.0-nullsafety.3" charcode: dependency: transitive description: name: charcode url: "https://pub.dartlang.org" source: hosted - version: "1.1.3" - collection: + version: "1.2.0-nullsafety.1" + clock: dependency: transitive description: - name: collection + name: clock url: "https://pub.dartlang.org" source: hosted - version: "1.14.12" - convert: + version: "1.1.0-nullsafety.1" + collection: dependency: transitive description: - name: convert + name: collection url: "https://pub.dartlang.org" source: hosted - version: "2.1.1" - crypto: + version: "1.15.0-nullsafety.3" + fake_async: dependency: transitive description: - name: crypto + name: fake_async url: "https://pub.dartlang.org" source: hosted - version: "2.1.4" + version: "1.2.0-nullsafety.1" flutter: dependency: "direct main" description: flutter @@ -67,48 +60,27 @@ packages: description: flutter source: sdk version: "0.0.0" - image: - dependency: transitive - description: - name: image - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.12" matcher: dependency: transitive description: name: matcher url: "https://pub.dartlang.org" source: hosted - version: "0.12.6" + version: "0.12.10-nullsafety.1" meta: dependency: transitive description: name: meta url: "https://pub.dartlang.org" source: hosted - version: "1.1.8" + version: "1.3.0-nullsafety.3" path: dependency: transitive description: name: path url: "https://pub.dartlang.org" source: hosted - version: "1.6.4" - petitparser: - dependency: transitive - description: - name: petitparser - url: "https://pub.dartlang.org" - source: hosted - version: "2.4.0" - quiver: - dependency: transitive - description: - name: quiver - url: "https://pub.dartlang.org" - source: hosted - version: "2.1.3" + version: "1.8.0-nullsafety.1" sky_engine: dependency: transitive description: flutter @@ -120,63 +92,56 @@ packages: name: source_span url: "https://pub.dartlang.org" source: hosted - version: "1.7.0" + version: "1.8.0-nullsafety.2" stack_trace: dependency: transitive description: name: stack_trace url: "https://pub.dartlang.org" source: hosted - version: "1.9.3" + version: "1.10.0-nullsafety.1" stream_channel: dependency: transitive description: name: stream_channel url: "https://pub.dartlang.org" source: hosted - version: "2.0.0" + version: "2.1.0-nullsafety.1" string_scanner: dependency: transitive description: name: string_scanner url: "https://pub.dartlang.org" source: hosted - version: "1.0.5" + version: "1.1.0-nullsafety.1" term_glyph: dependency: transitive description: name: term_glyph url: "https://pub.dartlang.org" source: hosted - version: "1.1.0" + version: "1.2.0-nullsafety.1" test_api: dependency: transitive description: name: test_api url: "https://pub.dartlang.org" source: hosted - version: "0.2.15" + version: "0.2.19-nullsafety.2" typed_data: dependency: transitive description: name: typed_data url: "https://pub.dartlang.org" source: hosted - version: "1.1.6" + version: "1.3.0-nullsafety.3" vector_math: dependency: transitive description: name: vector_math url: "https://pub.dartlang.org" source: hosted - version: "2.0.8" - xml: - dependency: transitive - description: - name: xml - url: "https://pub.dartlang.org" - source: hosted - version: "3.6.1" + version: "2.1.0-nullsafety.3" sdks: - dart: ">=2.6.0 <3.0.0" + dart: ">=2.10.0-110 <2.11.0" flutter: ">=1.10.0" From b1fa0663427278a51f5ec0383229921c4b9ec9b2 Mon Sep 17 00:00:00 2001 From: Ahmed Mirza Date: Wed, 7 Oct 2020 12:16:47 +0500 Subject: [PATCH 2/3] reader mode fixes --- lib/src/api.dart | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/lib/src/api.dart b/lib/src/api.dart index 406546a..27b62c5 100644 --- a/lib/src/api.dart +++ b/lib/src/api.dart @@ -8,8 +8,7 @@ import './exceptions.dart'; class NFC { static MethodChannel _channel = MethodChannel("nfc_in_flutter"); - static const EventChannel _eventChannel = - const EventChannel("nfc_in_flutter/tags"); + static const EventChannel _eventChannel = const EventChannel("nfc_in_flutter/tags"); static Stream _tagStream; @@ -62,8 +61,7 @@ class NFC { }); } - static void _startReadingNDEF( - bool once, String alertMessage, NFCReaderMode readerMode) { + static void _startReadingNDEF(bool once, String alertMessage, NFCReaderMode readerMode) { // Start reading Map arguments = { "scan_once": once, @@ -128,7 +126,7 @@ class NFC { _startReadingNDEF( once, alertMessage, - const NFCNormalReaderMode(), + readerMode, ); } on PlatformException catch (err) { if (err.code == "NFCMultipleReaderModes") { @@ -269,6 +267,8 @@ class NFCNormalReaderMode implements NFCReaderMode { class NFCDispatchReaderMode implements NFCReaderMode { String get name => "dispatch"; + const NFCDispatchReaderMode(); + @override Map get _options { return {}; From 1ececd102679fc3b3a215f275512770903b9862e Mon Sep 17 00:00:00 2001 From: Ahmed Mirza Date: Wed, 2 Dec 2020 18:27:48 +0500 Subject: [PATCH 3/3] add stop nfc func --- .../nfc_in_flutter/NfcInFlutterPlugin.java | 12 ++++++++ example/android/app/.project | 28 +++++++++++++++++++ lib/src/api.dart | 7 +++++ 3 files changed, 47 insertions(+) create mode 100644 example/android/app/.project diff --git a/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java b/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java index 6245b6c..3987ce3 100644 --- a/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java +++ b/android/src/main/java/me/andisemler/nfc_in_flutter/NfcInFlutterPlugin.java @@ -81,6 +81,9 @@ public void onMethodCall(MethodCall call, Result result) { case "readNDEFEnabled": result.success(nfcIsEnabled()); break; + case "stopNDEFReading": + result.success(stopReading()); + break; case "startNDEFReading": if (!(call.arguments instanceof HashMap)) { result.error("MissingArguments", "startNDEFReading was called with no arguments", ""); @@ -148,6 +151,15 @@ private Boolean nfcIsSupported() { return adapter != null; } + private Boolean stopReading() { + adapter = NfcAdapter.getDefaultAdapter(activity); + if(adapter == null) return false; + adapter.disableReaderMode(activity); + adapter.disableForegroundDispatch(activity); + adapter = null; + return true; + } + private void startReading(boolean noSounds) { adapter = NfcAdapter.getDefaultAdapter(activity); if (adapter == null) return; diff --git a/example/android/app/.project b/example/android/app/.project new file mode 100644 index 0000000..23a6983 --- /dev/null +++ b/example/android/app/.project @@ -0,0 +1,28 @@ + + + app + Project app created by Buildship. + + + + + org.eclipse.buildship.core.gradleprojectbuilder + + + + + + org.eclipse.buildship.core.gradleprojectnature + + + + 1603716476333 + + 30 + + org.eclipse.core.resources.regexFilterMatcher + node_modules|.git|__CREATED_BY_JAVA_LANGUAGE_SERVER__ + + + + diff --git a/lib/src/api.dart b/lib/src/api.dart index 27b62c5..a583035 100644 --- a/lib/src/api.dart +++ b/lib/src/api.dart @@ -229,6 +229,13 @@ class NFC { assert(supported is bool); return supported as bool; } + + /// stopNDEFReading stop reading NDEF tags + static Future get stopNDEFReading async { + final supported = await _channel.invokeMethod("stopNDEFReading"); + assert(supported is bool); + return supported as bool; + } } /// NFCReaderMode is an interface for different reading modes