nfc_util 0.1.1 copy "nfc_util: ^0.1.1" to clipboard
nfc_util: ^0.1.1 copied to clipboard

A Flutter plugin providing access to NFC features on Android and iOS.

example/lib/main.dart

import 'dart:typed_data';

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

void main() {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

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

  @override
  State<StatefulWidget> createState() => MyAppState();
}

class MyAppState extends State<MyApp> {
  ValueNotifier<dynamic> result = ValueNotifier(null);

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(title: Text('Nfc Util Plugin Example')),
        body: SafeArea(
          child: FutureBuilder<bool>(
            future: NfcManager.instance.isAvailable(),
            builder: (context, ss) => ss.data != true
                ? Center(child: Text('NfcUtil.isAvailable(): ${ss.data}'))
                : Flex(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    direction: Axis.vertical,
                    children: [
                      Flexible(
                        flex: 2,
                        child: Container(
                          margin: EdgeInsets.all(4),
                          constraints: BoxConstraints.expand(),
                          decoration: BoxDecoration(border: Border.all()),
                          child: SingleChildScrollView(
                            child: ValueListenableBuilder<dynamic>(
                              valueListenable: result,
                              builder: (context, value, _) => Text('${value ?? ''}'),
                            ),
                          ),
                        ),
                      ),
                      Flexible(
                        flex: 3,
                        child: GridView.count(
                          padding: EdgeInsets.all(4),
                          crossAxisCount: 2,
                          childAspectRatio: 4,
                          crossAxisSpacing: 4,
                          mainAxisSpacing: 4,
                          children: [
                            ElevatedButton(onPressed: _tagRead, child: Text('Tag Read')),
                            ElevatedButton(onPressed: _ndefWrite, child: Text('Ndef Write')),
                            ElevatedButton(onPressed: _ndefWriteLock, child: Text('Ndef Write Lock')),
                          ],
                        ),
                      ),
                    ],
                  ),
          ),
        ),
      ),
    );
  }

  void _tagRead() {
    NfcManager.instance.startSession(
      pollingOptions: {NfcPollingOption.iso14443},
      onDiscovered: (NfcTag tag) async {
        result.value = tag.data;
        NfcManager.instance.stopSession();
      },
    );
  }

  void _ndefWrite() {
    NfcManager.instance.startSession(
      onDiscovered: (NfcTag tag) async {
        var ndef = Ndef.from(tag);
        if (ndef == null || !ndef.isWritable) {
          result.value = 'Tag is not ndef writable';
          NfcManager.instance.stopSession(errorMessage: result.value);
          return;
        }

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

        try {
          await ndef.write(message);
          result.value = 'Success to "Ndef Write"';
          NfcManager.instance.stopSession();
        } catch (e) {
          result.value = e;
          NfcManager.instance.stopSession(errorMessage: result.value.toString());
          return;
        }
      },
    );
  }

  void _ndefWriteLock() {
    NfcManager.instance.startSession(
      onDiscovered: (NfcTag tag) async {
        var ndef = Ndef.from(tag);
        if (ndef == null) {
          result.value = 'Tag is not ndef';
          NfcManager.instance.stopSession(errorMessage: result.value.toString());
          return;
        }

        try {
          await ndef.writeLock();
          result.value = 'Success to "Ndef Write Lock"';
          NfcManager.instance.stopSession();
        } catch (e) {
          result.value = e;
          NfcManager.instance.stopSession(errorMessage: result.value.toString());
          return;
        }
      },
    );
  }
}
1
likes
150
points
95
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter plugin providing access to NFC features on Android and iOS.

Repository (GitHub)
View/report issues

Documentation

API reference

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on nfc_util

Packages that implement nfc_util