flutter_nfc_manager

repository·main·Indexed 42 days ago

https://github.com/okadan/flutter-nfc-manager

A Flutter plugin providing access to Near Field Communication (NFC) features on Android and iOS. It allows developers to check NFC availability, start and stop scanning sessions using NfcManager, and interact with platform-specific tag classes such as NfcAAndroid, FeliCaIos, and NdefIos.

Tokens
1.1K
Snippets
4
Records
6
Agent score
4%

What's inside flutter_nfc_manager

  1. Convert NfcTag to platform-specific tag classes

    main

    An NfcTag instance is a generic container. To interact with specific tag technologies, you must convert it to a platform-specific class using that class's static .from(tag) method. If the tag is not compatible with the requested technology, the method will return null.

    import 'package:nfc_manager/nfc_manager.dart';
    import 'package:nfc_manager_ndef/nfc_manager_ndef.dart';
    
    final Ndef ndef = Ndef.from(tag);
    
    if (ndef == null) {
      print('This tag is not compatible with NDEF.');
      return;
    }
    
    // Do something with an Ndef instance...
    print(ndef);
  2. Configure iOS for nfc_manager

    main

    Setting up NFC on iOS requires several entitlements and Info.plist entries:

    1. Add Near Field Communication Tag Reader Session Formats Entitlements to your entitlements file.
    2. Add NFCReaderUsageDescription to your Info.plist to provide a reason for using NFC.
    3. Add com.apple.developer.nfc.readersession.iso7816.select-identifiers to your Info.plist if you need to select specific identifiers.
    4. If you specify NfcPollingOption.iso18092 in startSession, you must add com.apple.developer.nfc.readersession.felica.systemcodes to your Info.plist to avoid errors.
  3. Check NFC availability and start a session

    main

    Before starting an NFC session, check if the device supports NFC and if it is enabled using NfcManager.instance.checkAvailability().

    To scan for tags, use NfcManager.instance.startSession(). You can provide pollingOptions (such as NfcPollingOption.iso14443, iso18092, or iso15693) and an onDiscovered callback which provides an NfcTag instance. Remember to call NfcManager.instance.stopSession() when the session is no longer needed.

    import 'package:nfc_manager/nfc_manager.dart';
    
    // Check the availability of NFC on the current device.
    NfcAvailability availability = await NfcManager.instance.checkAvailability();
    
    if (availability != NfcAvailability.enabled) {
      print('NFC may not be supported or may be temporarily disabled.');
      return;
    }
    
    // Start the session.
    NfcManager.instance.startSession(
      pollingOptions: {NfcPollingOption.iso14443}, // You can also specify iso18092 and iso15693.
      onDiscovered: (NfcTag tag) async {
        // Do something with an NfcTag instance...
        print(tag);
    
        // Stop the session when no longer needed.
        await NfcManager.instance.stopSession();
      },
    );
  4. Customize iOS launch screen assets

    main

    To change the launch screen image for the iOS version of your app, you can either replace the image files directly in the example/ios/Runner/Assets.xcassets/LaunchImage.imageset/ directory or use Xcode for a more visual approach.

    Using Xcode:

    1. Open the iOS project in Xcode by running open ios/Runner.xcworkspace from your terminal.
    2. In the Xcode Project Navigator, navigate to Runner/Assets.xcassets.
    3. Drag and drop your desired images into the asset catalog to replace the existing ones.
    open ios/Runner.xcworkspace
  5. Reference platform-specific tag classes

    main

    The following classes are available for converting an NfcTag via their .from() method:

    Android only:

    • NfcAAndroid
    • NfcBAndroid
    • NfcFAndroid
    • NfcVAndroid
    • IsoDepAndroid
    • MifareClassicAndroid
    • MifareUltralightAndroid
    • NfcBarcodeAndroid
    • NdefAndroid
    • NdefFormatableAndroid

    iOS only:

    • FeliCaIos
    • MiFareIos
    • Iso15693Ios
    • Iso7816Ios
    • NdefIos

    Cross-platform abstractions (via separate packages):

    • Ndef (from nfc_manager_ndef)
    • FeliCa (from nfc_manager_felica)