nfc_manager

A Flutter plugin to use NFC. Supported on both Android and iOS.

Setup

Android Setup

iOS Setup

Usage

Managing Session

// Start session and register callback.
NfcManager.instance.startTagSession(
  alertMessageIOS: '...',
  pollingOptions: {TagPollingOption.iso14443, TagPollingOption.iso15693, TagPollingOption.iso18092},
  onDiscovered: (NfcTag tag) {
    // Manipulating tag
  },
);

// Stop session and unregister callback.
NfcManager.instance.stopSession(
  alertMessageIOS: '...',
  errorMessageIOS: '...',
);

Manipulating NDEF

// Obtain an Ndef instance from tag
Ndef ndef = Ndef.fromTag(tag);

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

// Read an NdefMessage object cached at discovery time
print(ndef.cachedMessage);

if (!ndef.isWritable) {
  print('Tag is not ndef writable');
  return;
}

NdefMessage messageToWrite = NdefMessage([
  NdefRecord.createText('Hello'),
  NdefRecord.createUri(Uri.parse('https://flutter.dev')),
  NdefRecord.createMime('text/plain', Uint8List.fromList('Hello'.codeUnits)),
  NdefRecord.createExternal('mydomain', 'mytype', Uint8List.fromList('mydata'.codeUnits)),
]);

// Write an NdefMessage
try {
  await ndef.write(messageToWrite);
} catch (e) {
  // handle error
  return;
}

Manipulating Platform-Specific-Tag

The following platform-specific-tag classes are available:

iOS

  • MiFare
  • FeliCa
  • ISO15693
  • ISO7816

Android

  • NfcA
  • NfcB
  • NfcF
  • NfcV
  • IsoDep

Example

MiFare miFare = MiFare.fromTag(tag);

if (miFare == null) {
  print('MiFare is not available on this tag');
  return;
}

Uint8List response = await miFare.sendMiFareCommand(...);

Libraries

nfc_manager
platform_tags