Giter Club home page Giter Club logo

flutter-nfc-plugin's Introduction

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

flutter-nfc-plugin's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.