Implement NFC in Flutter To Transfer Peer-to-peer Data

CodeTrade India
3 min readDec 11, 2023

--

Near-field communication (NFC) is a wireless technology that allows devices to exchange data over short distances. Used in a variety of applications, such as contactless payments, mobile wallets, and access control systems.

Implementation of NFC in Flutter To Transfer Peer-to-peer Data Using The Nfc_manager Package | CodeTrade

To implement NFC peer-to-peer data transfer in Flutter, you will need to use the nfc_manager package. This package provides a simple and easy-to-use interface for interacting with NFC devices.

Steps To Implement NFC in Flutter To Transfer Peer-to-peer Data Using Nfc_manager Package

1. Add nfc_manager Package

To transfer peer-to-peer data using the nfc_manager package, you must first add it to your Flutter project. You can do this by running the following command in the root directory of your project:

flutter pub add nfc_manager

Once the package is installed, you can import it into your Flutter code and start using it to access the NFC features on Android and iOS.

2. Configure Permissions

You will need to configure the necessary permissions in both the Android and iOS applications:

For Android,

Open your AndroidManifest.xml file. Add the following permission, and Save the file.

<uses-permission android:name="android.permission.NFC">

For iOS,

Open your Info.plist file. Add the following key-value pair, and save the file.

<key>NFCReaderUsageDescription</key>

<string>Your explanation of why the app needs NFC</string>

Once you have configured the necessary permissions, you can start using the nfc_manager package to access the NFC features on Android and iOS.

3. Read NFC Tags

To read an NFC tag, you first need to check if NFC is available. Then check the availability, and initiate an NFC session using NfcManager.instance.startSession(). When an NFC tag is discovered, you can print its data to the console or handle it in your way. Let’s check the full example of Read NFC Tag data.

import 'package:flutter/material.dart';
import 'package:nfc_manager/nfc_manager.dart';

void main() {
runApp(const MyApp());
}

class MyApp extends StatelessWidget {
const MyApp({super.key});

@override
Widget build(BuildContext context) {
return const MaterialApp(
home: MyHomePage(),
);
}
}

class MyHomePage extends StatelessWidget {
const MyHomePage({super.key});

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('NFC Reader'),
),
body: Center(
child: ElevatedButton(
onPressed: _startNFCReading,
child: const Text('Start NFC Reading'),
),
),
);
}

void _startNFCReading() async {
try {
bool isAvailable = await NfcManager.instance.isAvailable();

//We first check if NFC is available on the device.
if (isAvailable) {
//If NFC is available, start an NFC session and listen for NFC tags to be discovered.
NfcManager.instance.startSession(
onDiscovered: (NfcTag tag) async {
// Process NFC tag, When an NFC tag is discovered, print its data to the console.
debugPrint('NFC Tag Detected: ${tag.data}');
},
);
} else {
debugPrint('NFC not available.');
}
} catch (e) {
debugPrint('Error reading NFC: $e');
}
}
}

This code will check if NFC is available on the device. If it is, the code will start an NFC session and listen for NFC tags. When an NFC tag is discovered, the code will print the data from the tag to the console.

4. Write NFC Tags

Writing NFC tags store data on an NFC tag. To write an NFC tag, you will need a blank NFC tag and an NFC writer. NFC writers are devices that can read and write NFC tags. Some NFC writers are built into smartphones, while others are external devices that connect to your computer. Let’s check the complete example of how to write an NDEF record to an NFC tag:

Click the link to view a complete article with Code: https://www.codetrade.io/blog/how-to-implement-nfc-in-flutter-a-beginners-guide/

Need help with NFC in Flutter? CodeTrade India, a top mobile app development company in India, can help you implement NFC in Flutter. We have a team of experienced Flutter developers who can assist you with your project. Hire a dedicated Flutter developer from CodeTrade today!

--

--

CodeTrade India

Top-notch custom software development service provider | Open edX Development | Odoo Development Agency | AI & ML Development | Mobile App Development