0

I'm developing a Flutter app that continuously reads NFC tags for attendance using the flutter_nfc_kit package. The reading loop works fine on Android and mostly fine on iOS, but on iOS devices I randomly get these exceptions after every read:

PlatformException(500, Generic NFC Error, Session invalidated unexpectedly, null)
PlatformException(409, SessionCanceled, Session invalidated by user, null)

nfc reading loop inside a method :

void startNfcForAttendance({required BuildContext context}) async {
  try {
    bool isAvailable =
        await FlutterNfcKit.nfcAvailability == NFCAvailability.available;

    if (!isAvailable) {
      AppUtils.showToast(
        message: "NFC not available for this device",
        backgroundColor: AppColors.errorToastColor,
        textColor: AppColors.toastTextColor,
      );
      return;
    }

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

    if (_isNfcOn) {
      log("message");
      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),
        );

        String finalNFCValue = BigInt.parse(tag.id, radix: 16).toString();

        await FlutterNfcKit.finish();

        if (Platform.isIOS) {
          await Future.delayed(const Duration(seconds: 2));
        }

        if (!context.mounted) {
          return;
        }

        await markCardAttendance(cardValue: finalNFCValue);
      } on PlatformException catch (e) {
        if (Platform.isAndroid) {
          if (e.code == "500") {
            AppUtils.showToast(
              message: "Please Restart Your App",
              backgroundColor: AppColors.errorToastColor,
              textColor: AppColors.toastTextColor,
            );
          }
        }
        if (e.code == "409" || e.code == "408") {
          stopNfc();
        }
      } catch (e) {
        await FlutterNfcKit.finish();
        await Future.delayed(const Duration(milliseconds: 500));
      }
    }
  } catch (e) {
    AppUtils.showToast(
      message: "NFC is not supported on this device.",
      backgroundColor: AppColors.errorToastColor,
      textColor: AppColors.toastTextColor,
    );
  }
}

stop method :

  void stopNfc() async {
    _isNfcOn = false;
    notifyListeners();
    try {
      await FlutterNfcKit.finish();
    } catch (_) {}
  }

How can I stop or handle the random PlatformException(500) and PlatformException(409) errors on iOS when reading NFC tags continuously?

Is there a better way to do continuous NFC reading in Flutter on iOS because of its limits?

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.