Skip to content

BIG BUG v4.1.1 nfc tag not working. #256

Description

@onderadaidenfit

nfc tag no data. The new version no longer includes data.

v3.5.0 working code.

import 'dart:async';
import 'dart:io';

import 'package:easy_localization/easy_localization.dart';
import 'package:nfc_manager/nfc_manager.dart';
import 'package:people/common/data/model/response/data_response.dart';
import 'package:people/init/lang/locale_keys.g.dart';

///Use all nfc methods.
final class NfcUtil {
///Singleton Lazy
factory NfcUtil() {
_instance ??= NfcUtil._internal();
return _instance!;
}

NfcUtil._internal();

static NfcUtil? _instance;

final _mifareUltraLight = 'mifareultralight';
final _mifare = 'mifare';
final _identifier = 'identifier';
var _isRunning = false;

///Start
Future<DataResponse> start() async {
final completer = Completer<DataResponse>();

// Check availability
final isAvailable = await NfcManager.instance.isAvailable();

if (isAvailable) {
  _isRunning = true;
  await NfcManager.instance.startSession(
    pollingOptions: {NfcPollingOption.iso14443},
    onDiscovered: (NfcTag tag) async {
      // Do something with an NfcTag instance.
      //debugPrint('data: ${tag.data}');
      if (!_isRunning) {
        return;
      }

      final mifare = (tag.data[_mifareUltraLight] ?? tag.data[_mifare]) as Map<Object?, Object?>?;
      if (mifare != null) {
        final identifier = mifare[_identifier] as List<int>?;
        if (identifier != null) {
          final currentTag = identifier.map((e) => e.toRadixString(16).padLeft(2, '0')).join();
          //debugPrint('currentTag Code: $currentTag');
          completer.complete(DataResponse(data: currentTag));
        } else {
          completer.complete(DataResponse(error: LocaleKeys.no_data.tr()));
        }
      } else {
        completer.complete(DataResponse(error: LocaleKeys.no_data.tr()));
      }

      _isRunning = false;
      if (Platform.isAndroid) {
        await Future<void>.delayed(const Duration(seconds: 2)).then((
          value,
        ) async {
          await NfcManager.instance.stopSession();
        });
      } else {
        await NfcManager.instance.stopSession();
      }
    },
    onError: (error) async {
      completer.complete(DataResponse(error: error.message));
      return;
    },
  );
} else {
  completer.complete(DataResponse(error: LocaleKeys.nfc_is_available.tr()));
}

return completer.future;

}
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions