Skip to content

Add: isAvailable (i.e hardware), isEnabled, openAndroidSettings for NFC #188

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

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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,15 @@ Note: This plugin depends on `NFCTagReaderSession` (requires iOS 13.0 or later)
**Handling Session**

```dart
// Check availability
// Check hardware availability
bool isAvailable = await NfcManager.instance.isAvailable();

// Check if enabled on device
bool isEnabled = await NfcManager.instance.isEnabled()

// Open NFC Settings in Android
await NfcManager.instance.openAndroidNFCSettings();

// Start Session
NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import android.nfc.tech.NfcF
import android.nfc.tech.NfcV
import android.nfc.tech.TagTechnology
import android.os.Build
import android.content.Intent
import android.provider.Settings

import io.flutter.embedding.engine.plugins.FlutterPlugin
import io.flutter.embedding.engine.plugins.activity.ActivityAware
Expand Down Expand Up @@ -63,6 +65,8 @@ class NfcManagerPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
override fun onMethodCall(call: MethodCall, result: Result) {
when (call.method) {
"Nfc#isAvailable" -> handleNfcIsAvailable(call, result)
"Nfc#isEnabled" -> handleNfcIsEnabled(call, result)
"Nfc#openSettings" -> handleNfcOpenSettings(call, result)
"Nfc#startSession" -> handleNfcStartSession(call, result)
"Nfc#stopSession" -> handleNfcStopSession(call, result)
"Nfc#disposeTag" -> handleNfcDisposeTag(call, result)
Expand Down Expand Up @@ -93,9 +97,20 @@ class NfcManagerPlugin: FlutterPlugin, MethodCallHandler, ActivityAware {
}

private fun handleNfcIsAvailable(call: MethodCall, result: Result) {
result.success(adapter != null)
}

private fun handleNfcIsEnabled(call: MethodCall, result: Result) {
result.success(adapter?.isEnabled == true)
}

private fun handleNfcOpenSettings(call: MethodCall, result: Result) {
val intent = Intent(Settings.ACTION_NFC_SETTINGS)
this.activity?.startActivity(intent)
result.success(null)
}


private fun handleNfcStartSession(call: MethodCall, result: Result) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
result.error("unavailable", "Requires API level 19.", null)
Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ packages:
path: ".."
relative: true
source: path
version: "3.3.0"
version: "3.4.0"
path:
dependency: transitive
description:
Expand Down
24 changes: 22 additions & 2 deletions lib/src/nfc_manager/nfc_manager.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:flutter/services.dart';
import 'dart:io' show Platform;

import '../channel.dart';
import '../translator.dart';
Expand All @@ -25,11 +26,30 @@ class NfcManager {
// _onError
NfcErrorCallback? _onError;

/// Checks whether the NFC features are available.
/// Checks whether the NFC features are available. (i.e hardware)
Future<bool> isAvailable() async {
return channel.invokeMethod('Nfc#isAvailable').then((value) => value!);
}

/// Checks whether the NFC features are enabled in Android
Future<bool> isEnabled() async {
if (Platform.isAndroid) {
return channel.invokeMethod('Nfc#isEnabled').then((value) => value!);
} else {
throw Exception('This method is only available on Android');
//NFC is always enabled in iOS
}
}

/// Open the NFC settings page in Android
Future<void> openAndroidNFCSettings() async {
if (Platform.isAndroid) {
channel.invokeMethod('Nfc#openSettings');
} else {
throw Exception('This method is only available on Android');
}
}

/// Start the session and register callbacks for tag discovery.
///
/// This uses the NFCTagReaderSession (on iOS) or NfcAdapter#enableReaderMode (on Android).
Expand All @@ -41,7 +61,7 @@ class NfcManager {
///
/// (iOS only) `alertMessage` is used to display the message on the popup shown when the session is started.
///
/// (iOS only) `invalidateAfterFirstRead` is used to specify whether the session should be invalidated
/// (iOS only) `invalidateAfterFirstRead` is used to specify whether the session should be invalidated
/// after the first tag is discovered. Default is true.
///
/// (iOS only) `onError` is called when the session is stopped for some reason after the session has started.
Expand Down