flutter_nfc_plugin 0.0.3

Flutter NFC plugin #

The Flutter NFC plugin allows you to read NFC state and tags

Supported Platforms #

  • Android

Installing #

Add to pubspec.yaml:

dependencies:
  flutter_nfc_plugin:
    git:
      url: git://github.com/dukhanov/flutter-nfc-plugin.git
      ref: master

Add NFC permissions to Android package's manifest file:

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

To be able to start the app within an NFC tag scanned Add such intent filter to Android package's manifest file inside of the Main activity section:

<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="*" />
</intent-filter>

And nfc_tech_filter.xml to the android/app/src/main/res/xml folder (create if not exist). See example project

File: nfc_tech_filter.xml
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.NfcA</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcB</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcBarcode</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcF</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NfcV</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.IsoDep</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareClassic</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.MifareUltralight</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
</resources>

Add such metadata to Android package's manifest file inside of the Main activity section:

<meta-data android:name="android.nfc.action.TECH_DISCOVERED" android:resource="@xml/nfc_tech_filter" />

Example of usage #

Check NFC state: #

NfcPlugin nfcPlugin = NfcPlugin();
try {
  final NfcState _nfcState = await nfcPlugin.nfcState;
  print('NFC state is $_nfcState');
} on PlatformException {
  print('Method "NFC state" exception was thrown');
}

Check NFC message which was used to start the app: #

NfcPlugin nfcPlugin = NfcPlugin();
try {
  final NfcEvent _nfcEventStartedWith = await nfcPlugin.nfcStartedWith;
  print('NFC event started with is ${_nfcEventStartedWith.toString()}');
  if (_nfcEventStartedWith != null) {
    setState(() {
      nfcMessageStartedWith = _nfcEventStartedWith.message;
    });
  }
} on PlatformException {
  print('Method "NFC event started with" exception was thrown');
}

Read NFC tags subscribe: #

NfcPlugin nfcPlugin = NfcPlugin();
nfcPlugin.onNfcMessage.listen((NfcEvent event) {
if (event.error.isNotEmpty) {
  print('NFC read error: ${event.error}');
} else {
  print('NFC read successful');
  print(event.message.payload.toString());
  print(event.message.id);
});

See more at: example project

0.0.3 #

  • Adapt with Flutter 1.6

0.0.2 #

  • Fixed: podspec for iOS

0.0.1 #

  • Added: an ability to read NFC tags
  • Added: an ability to read NFC state
  • Added: getting NFC message in case the app was started within an NFC tag

example/README.md

Flutter NFC plugin example #

Demonstrates how to use the nfc_plugin plugin.

Note: the plugin is currently supported Android platform only

Getting Started #

For help getting started with Flutter, view flutter online documentation

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  flutter_nfc_plugin: ^0.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:flutter_nfc_plugin/models/nfc_event.dart';
import 'package:flutter_nfc_plugin/models/nfc_message.dart';
import 'package:flutter_nfc_plugin/models/nfc_state.dart';
import 'package:flutter_nfc_plugin/nfc_plugin.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
54
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
90
Overall:
Weighted score of the above. [more]
75
Learn more about scoring.

We analyzed this package on Jul 17, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.4.0
  • pana: 0.12.19
  • Flutter: 1.7.8+hotfix.3

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
Transitive dependencies
collection 1.14.11
meta 1.1.6 1.1.7
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test

Admin