Skip to content

codewithbek/nfc_app

Repository files navigation

NFC Reader/Writer App

A Flutter application that reads and writes NFC NDEF tags using the flutter_nfc_kit package. Supports both Android and iOS platforms.

Features

  • ✅ Check NFC availability before scanning
  • ✅ Read NFC tag UID and NDEF records
  • ✅ Display tag ID, type, and payload information
  • ✅ Write NDEF URL records to NFC tags
  • ✅ Graceful error handling for timeouts and user cancellation
  • ✅ Proper NFC session management
  • ✅ Clean architecture with service layer separation
  • ✅ Material Design UI with modern Flutter practices

Prerequisites

  • Flutter SDK (3.9.2 or higher)
  • Android Studio / Xcode (for platform-specific setup)
  • Physical device with NFC support (NFC cannot be tested on emulators)

Installation

  1. Clone the repository and navigate to the project directory:
cd nfc_app
  1. Install dependencies:
flutter pub get

Platform-Specific Setup

Android Setup

The Android configuration is already set up in android/app/src/main/AndroidManifest.xml:

  • ✅ NFC permission (android.permission.NFC)
  • ✅ NFC feature declaration (optional, not required)

Additional Steps:

  1. Minimum SDK Version: Ensure your android/app/build.gradle.kts has a minimum SDK version of 21 or higher:

    android {
        defaultConfig {
            minSdk = 21  // or higher
        }
    }
  2. Enable NFC on Device:

    • Go to Settings → Connected devices → Connection preferences → NFC
    • Ensure NFC is enabled
  3. Testing:

    • NFC operations require a physical Android device
    • NFC cannot be tested on Android emulators
    • Ensure your device supports NFC (most modern Android devices do)

iOS Setup

The iOS configuration has been set up with:

  • NFCReaderUsageDescription in Info.plist
  • com.apple.developer.nfc.readersession.formats in Info.plist
  • Runner.entitlements file with NFC capability

CRITICAL: Xcode Configuration Steps (Required for NFC to Work)

You MUST complete these steps in Xcode for NFC to work on iOS:

  1. Open the Project in Xcode:

    open ios/Runner.xcworkspace

    ⚠️ Important: Open .xcworkspace, NOT .xcodeproj

  2. Add NFC Capability:

    • In Xcode, select the Runner project in the left sidebar
    • Select the Runner target (under TARGETS)
    • Click the "Signing & Capabilities" tab
    • Click the "+ Capability" button (top left of the capabilities section)
    • Search for and add "Near Field Communication Tag Reading"
    • Verify that the capability appears with a checkmark
  3. Link Entitlements File (if not automatically done):

    • Still in "Signing & Capabilities" tab
    • Look for "Code Signing Entitlements" field
    • If empty, enter: Runner/Runner.entitlements
    • Xcode should automatically link it when you add the capability, but verify it's set
  4. Verify Build Settings:

    • Go to "Build Settings" tab
    • Search for "Code Signing Entitlements"
    • Ensure it shows: Runner/Runner.entitlements (for all configurations: Debug, Release, Profile)
  5. Clean and Rebuild:

    cd ios
    rm -rf Pods Podfile.lock
    pod install
    cd ..
    flutter clean
    flutter pub get
  6. Minimum iOS Version:

    • iOS 11.0 or higher is required for NFC functionality
    • Your project is already configured for iOS 13.0+ (see IPHONEOS_DEPLOYMENT_TARGET)

Why NFC Availability Check Fails Without This Setup:

On iOS, FlutterNfcKit.nfcAvailability will return notAvailable or throw an exception if:

  • The NFC capability is not added in Xcode
  • The entitlements file is not linked to the target
  • The app is not signed with NFC entitlements
  • NFCReaderUsageDescription is missing from Info.plist

Even on iPhone 13 Pro (which definitely supports NFC), the availability check will fail without proper Xcode configuration.

Testing:

  • NFC operations require a physical iOS device (iPhone 7 or later)
  • NFC cannot be tested on iOS simulators
  • After completing Xcode setup, rebuild and test on a real device

Running the App

  1. Connect a physical device with NFC support
  2. Ensure NFC is enabled on the device
  3. Run the app:
flutter run

Usage

Reading NFC Tags

  1. Tap the "Read Tag" button
  2. Hold your device near an NFC tag
  3. The app will display:
    • Tag ID (UID)
    • Tag Type (NFC-A, NFC-B, etc.)
    • NDEF Records (if available)

Writing to NFC Tags

  1. Tap the "Write URL" button
  2. Enter a URL in the dialog (e.g., https://example.com)
  3. Hold your device near a writable NFC tag
  4. The app will write the URL as an NDEF record

Project Structure

lib/
├── main.dart                 # Main UI and app entry point
└── services/
    └── nfc_service.dart      # NFC service class with read/write logic

Key Components

NfcService (lib/services/nfc_service.dart)

Service class that handles all NFC operations:

  • isNfcAvailable(): Checks if NFC is available on the device
  • readTag(): Reads tag UID and NDEF records
  • writeUrl(String url): Writes a URL as an NDEF record
  • cancel(): Cancels ongoing NFC operations

Main UI (lib/main.dart)

Material Design UI with:

  • NFC availability status indicator
  • Read and Write buttons
  • Tag information display
  • Error handling and user feedback
  • Loading states during NFC operations

Error Handling

The app handles various error scenarios:

  • NFC not available or disabled
  • Timeout while waiting for tag
  • User cancellation
  • Tag read/write failures
  • Invalid tag types

All errors are displayed to the user with clear messages.

NFC Session Management

The app properly manages NFC sessions:

  • Sessions are automatically closed after each operation
  • Sessions are cancelled when the user navigates away
  • Proper cleanup in dispose() method

Troubleshooting

Android Issues

  • "NFC is not available":

    • Check if NFC is enabled in device settings
    • Ensure you're testing on a physical device (not emulator)
    • Verify your device has NFC hardware
  • "Error reading tag":

    • Ensure the tag is close enough to the device
    • Try holding the tag steady for a few seconds
    • Some tags may not support NDEF format

iOS Issues

  • "NFC is not available" (Most Common Issue):

    Root Cause: Missing NFC capability or entitlements configuration in Xcode.

    Solution:

    1. Open ios/Runner.xcworkspace in Xcode
    2. Select Runner target → "Signing & Capabilities" tab
    3. Add "Near Field Communication Tag Reading" capability
    4. Verify Runner/Runner.entitlements is linked in "Code Signing Entitlements"
    5. Clean build folder: Product → Clean Build Folder (⇧⌘K)
    6. Rebuild: flutter clean && flutter pub get && flutter run

    Verification Steps:

    • Check that Runner.entitlements exists in ios/Runner/
    • Verify it contains com.apple.developer.nfc.readersession.formats
    • Ensure Info.plist has NFCReaderUsageDescription
    • Confirm the capability appears in Xcode's "Signing & Capabilities" tab
  • "NFC is not available" on iPhone 13 Pro or other supported devices:

    • This is always a configuration issue, not a hardware issue
    • iPhone 7 and later models support NFC
    • The problem is missing Xcode capability setup (see above)
  • Build errors:

    • Clean and rebuild: flutter clean && flutter pub get
    • Reinstall pods: cd ios && rm -rf Pods Podfile.lock && pod install && cd ..
    • Ensure Xcode has the NFC capability enabled
    • Check that minimum iOS version is 11.0+ (your project uses 13.0+)
  • Entitlements not found error:

    • Verify Runner.entitlements file exists at ios/Runner/Runner.entitlements
    • Check that it's added to the Xcode project (should appear in file navigator)
    • Ensure "Code Signing Entitlements" build setting points to Runner/Runner.entitlements

Dependencies

  • flutter_nfc_kit: ^5.0.0 - NFC functionality for Flutter

License

This project is a Flutter application template.

Notes

  • NFC operations require physical devices - emulators/simulators do not support NFC
  • Some NFC tags may be read-only and cannot be written to
  • The app uses a 30-second timeout for NFC operations
  • NDEF URL records use the standard format with prefix codes (0x04 for https://)

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published