Open2

FlutterでNFC readまとめ

ピン留めされたアイテム
hndrhndr

nfc_manager

https://pub.dev/packages/nfc_manager
https://github.com/okadan/flutter-nfc-manager
https://github.com/okadan/nfc-manager

https://github.com/okadan/flutter-nfc-manager/issues/38
https://github.com/okadan/flutter-nfc-manager/issues/17
https://stackoverflow.com/questions/68851357/flutter-nfc-manager-issue-on-ios
https://blog.matwright.dev/2021/05/19/using-nfc-tags-with-flutter/
https://qiita.com/f-nakahara/items/82fefe574a2e5a25182b

import

import 'package:nfc_manager/nfc_manager.dart';

check

NfcManager.instance.isAvailable().then((bool isAvailable) {
    if (isAvailable) {
      // doSomething();
    } else {
      // doSomething();
    }
});

read

await NfcManager.instance.startSession(
  pollingOptions: {
    NfcPollingOption.iso14443,
    NfcPollingOption.iso15693
  },
  onDiscovered: (NfcTag tag) async {
    final ndef = Ndef.from(tag);
    if (ndef == null) {
      debugPrint('Tag is not compatible with NDEF');
      await NfcManager.instance.stopSession(errorMessage: 'error');
      return;
    } else {
      final message = await ndef.read();
      final tagValue = String.fromCharCodes(message.records.first.payload)
          .substring(3);

      await NfcManager.instance.stopSession();
    }
  },
  onError: (dynamic e) async {
    debugPrint('NFC error: $e');
    await NfcManager.instance.stopSession(errorMessage: 'error');
  },
);