Skip to content

MissingPluginException on nfcAvailability (Android) – works only after device restart #230

@akhil-ge0rge

Description

@akhil-ge0rge

I am getting MissingPluginException (No implementation found for method getNFCAvailability), and NFC starts working again only after restarting the phone.

Device : Samsung Galaxy A23
flutter_nfc_kit: ^3.6.2

void startNfc({required BuildContext context}) async {
  final locale = AppLocalizations.of(context)!;

  final isLocationPermissionEnabled = await context
      .read<LoginViewModel>()
      .checkLocationPermissions(context: context);

  try {
    if (!isLocationPermissionEnabled) {
      log("Location Permission Unavailable");
      debugPrint("Location Permission Unavailable");
      return;
    }

    final NFCAvailability nfcStatus =
        await FlutterNfcKit.nfcAvailability;

    switch (nfcStatus) {
      case NFCAvailability.available:
        break;

      case NFCAvailability.disabled:
        AppUtils.showToast(
          message: locale.nfcDisabled,
          backgroundColor: AppColors.errorToastColor,
          textColor: AppColors.toastTextColor,
        );
        return;

      case NFCAvailability.not_supported:
        AppUtils.showToast(
          message: locale.nfvIsNotSupportedOnThisDevice,
          backgroundColor: AppColors.errorToastColor,
          textColor: AppColors.toastTextColor,
        );
        return;
    }

    try {
      await FlutterNfcKit.finish();
    } catch (e) {
      log("====> $e");
      debugPrint("====> $e");
    }

    if (_isNfcOn) {
      stopNfc();
      return;
    }

    _isNfcOn = true;
    notifyListeners();

    while (_isNfcOn) {
      try {
        final tag = await FlutterNfcKit.poll(
          readIso14443A: true,
          readIso15693: true,
          readIso14443B: true,
          readIso18092: true,
          timeout: const Duration(seconds: 15),
        );

        log("NFC TAG ID ${tag.id}");
        debugPrint("NFC TAG ID ${tag.id}");

        String finalNFCValue;

        if (tag.type == NFCTagType.mifare_classic) {
          // OLD CARD
          final List<int> bytes = [];

          for (int i = 0; i < tag.id.length; i += 2) {
            bytes.add(
              int.parse(tag.id.substring(i, i + 2), radix: 16),
            );
          }

          int decodedValue = 0;
          for (int i = 0; i < bytes.length; i++) {
            decodedValue += bytes[i] * pow(256, i).toInt();
          }

          finalNFCValue = decodedValue.toString();
        } else {
          // NEW CARD
          finalNFCValue = BigInt.parse(tag.id, radix: 16).toString();
        }

        log("🎬 final nfc value $finalNFCValue");
        debugPrint("🎬 final nfc value $finalNFCValue");

        await FlutterNfcKit.finish();

        if (Platform.isIOS) {
          // Avoid iOS popup flood
          await Future.delayed(const Duration(seconds: 2));
        }

        await apiCall(
          cardId: finalNFCValue,
          isNfc: true,
        );
      } on PlatformException catch (e) {
        log(e.toString());
        debugPrint(e.toString());

        if (Platform.isAndroid && e.code == "500") {
          stopNfc();
          AppUtils.showToast(
            message: locale.pleaseRestartYourApp,
            backgroundColor: AppColors.errorToastColor,
            textColor: AppColors.toastTextColor,
          );
        }

        if (e.code == "409") {
          stopNfc();
        }
      } catch (e) {
        log("⚠️ NFC Read Error: $e");
        debugPrint("⚠️ NFC Read Error: $e");

        await FlutterNfcKit.finish();
        await Future.delayed(
          const Duration(milliseconds: 500),
        ); // Small gap before retry
      }
    }
  } catch (e) {
    AppUtils.showToast(
      message: e.toString(),
      backgroundColor: AppColors.errorToastColor,
      textColor: AppColors.toastTextColor,
    );
    log("Session Error: $e");
    debugPrint("Session Error: $e");
  }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions