write method
Implementation
Future<bool> write() async {
stop = false;
Log().debug("Attempting Write to NDEF NFC Tag");
ndef.NDEFRecord record = ndef.TextRecord(
encoding: ndef.TextEncoding.values[0], language: 'en', text: value);
try {
NFCTag tag =
await FlutterNfcKit.poll(timeout: const Duration(seconds: 10));
if (tag.type == NFCTagType.mifare_ultralight ||
tag.type == NFCTagType.mifare_classic ||
tag.type == NFCTagType.iso15693) {
Log().debug('Writing $value to NFC Tag...');
try {
await FlutterNfcKit.writeNDEFRecords([record]);
await FlutterNfcKit.finish();
Log().debug('NFC Write Successful');
return true;
} catch (e) {
Log().error('NFC Write Unsuccessful');
Log().exception(e, caller: 'nfc.dart: Writer.write() - write fail');
return false;
}
}
return false;
} on PlatformException catch (e) {
// throw a custom exception on timeout with a message.
if (e.code == "408") {
throw const CustomException(code: 408, message: 'Poll Timed Out');
}
if (e.code == "405") {
throw const CustomException(
code: 405, message: 'NFC Tag Not Writeable');
} else {
return false;
}
}
}