Anonymous
Как обрабатывать случайные ошибки сеанса NFC (500 и 409) в Flutter IOS во время непрерывного чтения тегов NFC?
Сообщение
Anonymous » 12 авг 2025, 13:26
Я разрабатываю приложение Flutter, которое непрерывно читает теги NFC для посещаемости, используя пакет Flutter_nfc_kit. Цикл чтения отлично работает на Android и в основном хорошо на iOS, но на устройствах iOS я случайным образом получаю эти исключения после каждого чтения: < /p>
Код: Выделить всё
PlatformException(500, Generic NFC Error, Session invalidated unexpectedly, null)
PlatformException(409, SessionCanceled, Session invalidated by user, null)
nfc считывающее цикл внутри метода :
Код: Выделить всё
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,
);
}
}
метод остановки :
Код: Выделить всё
void stopNfc() async {
_isNfcOn = false;
notifyListeners();
try {
await FlutterNfcKit.finish();
} catch (_) {}
}
Как я могу остановить или обрабатывать случайные ошибки платформы (500) и платформу (409) на iOS при постоянном чтении тегов NFC?>
Подробнее здесь:
https://stackoverflow.com/questions/797 ... ing-contin
1754994407
Anonymous
Я разрабатываю приложение Flutter, которое непрерывно читает теги NFC для посещаемости, используя пакет Flutter_nfc_kit. Цикл чтения отлично работает на Android и в основном хорошо на iOS, но на устройствах iOS я случайным образом получаю эти исключения после каждого чтения: < /p> [code]PlatformException(500, Generic NFC Error, Session invalidated unexpectedly, null) PlatformException(409, SessionCanceled, Session invalidated by user, null) [/code] [b] nfc считывающее цикл внутри метода [/b]: [code]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, ); } } [/code] [b] метод остановки [/b]: [code] void stopNfc() async { _isNfcOn = false; notifyListeners(); try { await FlutterNfcKit.finish(); } catch (_) {} } [/code] Как я могу остановить или обрабатывать случайные ошибки платформы (500) и платформу (409) на iOS при постоянном чтении тегов NFC?> Подробнее здесь: [url]https://stackoverflow.com/questions/79733020/how-to-handle-random-nfc-session-errors-500-409-in-flutter-ios-during-contin[/url]