Skip to content

The flutter plugin for support using NFC features on Android and iOS.

License

Notifications You must be signed in to change notification settings

skwcrd/flutter_nfc

Repository files navigation

Flutter NFC

This is flutter plugin for accessing the NFC features on Android and iOS.

Plugin to help developers looking to use internal hardware inside iOS or Android devices for reading and writing NFC tags.

The system activates a pooling reading session that stops automatically once a tag has been recognized. You can also trigger the stop event manually using a dedicated function.

Note: This plugin depends on NFCTagReaderSession (requires iOS 13.0 or later) and NfcAdapter#enableReaderMode (requires Android API level 19 or later).

Getting Started

This project is a starting point for a Flutter plug-in package, a specialized package that includes platform-specific implementation code for Android and iOS.

For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.

Supported NFC Format

The following nfc-tag-classes are available:

Android and iOS

  • Ndef

Android

  • NdefFormatable
  • NfcA
  • NfcB
  • NfcF
  • NfcV
  • IsoDep
  • MifareClassic
  • MifareUtralight

iOS

  • MiFare
  • FeliCa
  • Iso7816
  • Iso15693

Installation

Download or clone repository and add a dependency on the flutter_nfc package in the dependencies section of pubspec.yaml (recommend use this):

dependencies:
  flutter_nfc:
    path: ../PATH_ROOT_DIRECTORY_PLUGIN/

or to get the experimental one:

dependencies:
  flutter_nfc:
    git:
      url: https://github.com/skwcrd/flutter_nfc.git
      ref: main

and then run the shell.

flutter pub get

last step import to the project:

import 'package:flutter_nfc/flutter_nfc.dart';

Android Setup

<uses-permission android:name="android.permission.NFC" />

If your app requires NFC, you can add the following to only allow it to be downloaded on devices that supports NFC:

<uses-feature
    android:name="android.hardware.nfc"
    android:required="true" />
  • Assign 19 in minSdkVersion in the build.gradle (Module: app)
defaultConfig {
    ...
    minSdkVersion 19
    ...
}

iOS Setup

On iOS you must add turn on the Near Field Communication capability, add a NFC usage description and a NFC entitlement.

Turn on Near Field Communication Tag Reading

Open your iOS project in Xcode, find your project's target and navigate to Capabilities. Scroll down to Near Field Communication Tag Reading and turn it on.

Turning on Near Field Communication Tag reading

'Turn on Near Field Communication Tag Reading' capability  turned on for a project in Xcode

from developer.apple.com: Building an NFC Tag-Reader app

NFC Usage Description

Open your ios/Runner/Info.plist file. It's value should be a description of what you plan on using NFC for.

<key>NFCReaderUsageDescription</key>
<string>...</string>
<key>com.apple.developer.nfc.readersession.felica.systemcodes</key>
<array>
    <string>...</string>
    ...
</array>

<key>com.apple.developer.nfc.readersession.iso7816.select-identifiers</key>
<array>
    <string>...</string>
    ...
</array>

In your Podfile add this code in the top.

platform :ios, '13.0'
use_frameworks!

IOS Specifics

When you call NFCTagReaderSession(...) on iOS, Core NFC (the iOS framework that allows NFC reading) opens a little window. On Android it just starts listening for NFC tag reads in the background.

  • Timeout when used tag session.

    • when transceive timing open session approximately 20 second.
    • when wait tap a tag timing open session approximately 1 minute.
  • IOS behaves a bit different in terms of NFC reading and writing.

    • Ids of the tags aren't possible in the current implementation.
    • each scan is visible for the user with a bottom sheet.

Core NFC

image from developer.apple.com: Near Field Communication

Error handling

Errors aren't exception to NFC in Flutter. The call function returned by transceive(...) can send via ErrorCallback different exceptions (iOS only), and even worse; they are different for each platform.

Usage

Handling Session

// Check availability
bool isAvailable = await FlutterNfc.instance.isAvailable();

if ( isAvailable ) {
    // Start session
    FlutterNfc.instance.startSession(
        onTagDiscovered: (NFCTag tag) async {
            // Do something with an NFCTag instance.
        },
    );

    // Stop session
    FlutterNfc.instance.stopSession();
} else {
    FlutterNfc.instance.openSetting();
}

Handling Platform Tag

Obtain an instance by calling the method on the NFCTag class. For example:

final ndef = tag.ndef;

if (ndef == null) {
  print('Tag is not compatible with NDEF');
  return;
}

// Do something with an Ndef instance

Real-World-App

See this repo which is a Real-World-App demonstrates how to use this plugin.

Releases

No releases published

Packages

No packages published