static INFC CreateNFC() { #if NETSTANDARD1_0 || NETSTANDARD2_0 return(null); #elif XAMARIN_IOS if (NFCUtils.IsWritingSupported() && !Legacy) { return(new NFCImplementation()); } return(new NFCImplementation_Before_iOS13()); #else #pragma warning disable IDE0022 // Use expression body for methods return(new NFCImplementation()); #pragma warning restore IDE0022 // Use expression body for methods #endif }
/// <summary> /// Write or Clear a NDEF message /// </summary> /// <param name="tagInfo"><see cref="ITagInfo"/></param> /// <param name="clearMessage">Clear Message</param> /// <param name="makeReadOnly">Make tag read-only</param> internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false, bool makeReadOnly = false) { try { if (_currentTag == null) { throw new Exception(Configuration.Messages.NFCErrorMissingTag); } if (tagInfo == null) { throw new Exception(Configuration.Messages.NFCErrorMissingTagInfo); } var ndef = Ndef.Get(_currentTag); if (ndef != null) { try { if (!ndef.IsWritable) { throw new Exception(Configuration.Messages.NFCErrorReadOnlyTag); } if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records)) { throw new Exception(Configuration.Messages.NFCErrorCapacityTag); } ndef.Connect(); OnTagConnected?.Invoke(null, EventArgs.Empty); NdefMessage message = null; if (clearMessage) { message = GetEmptyNdefMessage(); } else { var records = new List <NdefRecord>(); for (var i = 0; i < tagInfo.Records.Length; i++) { var record = tagInfo.Records[i]; if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord) { records.Add(ndefRecord); } } if (records.Any()) { message = new NdefMessage(records.ToArray()); } } if (message != null) { ndef.WriteNdefMessage(message); if (!clearMessage && makeReadOnly) { if (!MakeReadOnly(ndef)) { Console.WriteLine("Cannot lock tag"); } } var nTag = GetTagInfo(_currentTag, ndef.NdefMessage); OnMessagePublished?.Invoke(nTag); } else { throw new Exception(Configuration.Messages.NFCErrorWrite); } } catch (Android.Nfc.TagLostException tlex) { throw new Exception("Tag Lost Error: " + tlex.Message); } catch (Java.IO.IOException ioex) { throw new Exception("Tag IO Error: " + ioex.Message); } catch (Android.Nfc.FormatException fe) { throw new Exception("Tag Format Error: " + fe.Message); } catch (Exception ex) { throw new Exception("Tag Error:" + ex.Message); } finally { if (ndef.IsConnected) { ndef.Close(); } _currentTag = null; OnTagDisconnected?.Invoke(null, EventArgs.Empty); } } else { throw new Exception(Configuration.Messages.NFCErrorNotCompliantTag); } } catch (Exception ex) { StopPublishingAndThrowError(ex.Message); } }
/// <summary> /// Writes or clears a TAG /// </summary> /// <param name="session"><see cref="NFCTagReaderSession"/></param> /// <param name="tag"><see cref="INFCNdefTag"/></param> /// <param name="tagInfo"><see cref="ITagInfo"/></param> /// <param name="clearMessage">Clear message</param> void ExecuteWriteOrClear(NFCTagReaderSession session, INFCNdefTag tag, ITagInfo tagInfo, bool clearMessage = false) { tag.QueryNdefStatus((status, capacity, error) => { if (error != null) { Invalidate(session, error.LocalizedDescription); return; } if (status == NFCNdefStatus.ReadOnly) { Invalidate(session, UIMessages.NFCErrorReadOnlyTag); return; } if (Convert.ToInt32(capacity) < NFCUtils.GetSize(tagInfo.Records)) { Invalidate(session, UIMessages.NFCErrorCapacityTag); return; } NFCNdefMessage message = null; if (!clearMessage) { session.AlertMessage = UIMessages.NFCSuccessWrite; var records = new List <NFCNdefPayload>(); for (var i = 0; i < tagInfo.Records.Length; i++) { var record = tagInfo.Records[i]; if (GetiOSPayload(record) is NFCNdefPayload ndefPayload) { records.Add(ndefPayload); } } if (records.Any()) { message = new NFCNdefMessage(records.ToArray()); } } else { session.AlertMessage = UIMessages.NFCSuccessClear; message = GetEmptyNdefMessage(); } if (message != null) { tag.WriteNdef(message, (error) => { if (error != null) { Invalidate(session, error.LocalizedDescription); return; } tagInfo.Records = GetRecords(message.Records); OnMessagePublished?.Invoke(tagInfo); Invalidate(NfcSession); }); } else { Invalidate(session, UIMessages.NFCErrorWrite); } }); }
/// <summary> /// Write or Clear a NDEF message /// </summary> /// <param name="tagInfo"><see cref="ITagInfo"/></param> /// <param name="clearMessage">Clear Message</param> internal void WriteOrClearMessage(ITagInfo tagInfo, bool clearMessage = false) { if (_currentTag == null) { throw new Exception("Tag error: No tag to write"); } if (tagInfo == null) { throw new Exception("TagInfo error: No tag to write"); } var ndef = Ndef.Get(_currentTag); if (ndef != null) { try { if (!ndef.IsWritable) { throw new Exception("Tag is not writable"); } if (ndef.MaxSize < NFCUtils.GetSize(tagInfo.Records)) { throw new Exception("Tag is too small"); } ndef.Connect(); OnTagConnected?.Invoke(null, EventArgs.Empty); NdefMessage message = null; if (clearMessage) { message = GetEmptyNdefMessage(); } else { var records = new List <NdefRecord>(); for (var i = 0; i < tagInfo.Records.Length; i++) { var record = tagInfo.Records[i]; if (GetAndroidNdefRecord(record) is NdefRecord ndefRecord) { records.Add(ndefRecord); } } if (records.Any()) { message = new NdefMessage(records.ToArray()); } } if (message != null) { ndef.WriteNdefMessage(message); var nTag = GetTagInfo(_currentTag, ndef.NdefMessage); OnMessagePublished?.Invoke(nTag); } else { throw new Exception("nothing to write on tag"); } } catch (Android.Nfc.TagLostException tlex) { throw new Exception("Tag lost error: " + tlex.Message); } catch (Java.IO.IOException ioex) { throw new Exception("Tag IO error: " + ioex.Message); } catch (Android.Nfc.FormatException fe) { throw new Exception("Tag format error: " + fe.Message); } catch (Exception ex) { throw new Exception("Tag other error:" + ex.Message); } finally { if (ndef.IsConnected) { ndef.Close(); } _currentTag = null; OnTagDisconnected?.Invoke(null, EventArgs.Empty); } } else { throw new Exception("Tag Error: NDEF is not supported"); } }