Search Results for

    Show / Hide Table of Contents

    How to write an NDEF message and verify it

    Purpose: replace the NDEF message of a tag and confirm what was written. Platforms: Android, iOS. Status: stable.

    Prerequisites: a configured host (see configure the Android manifest and iOS entitlements) and a disposable, writable tag. The library never asks the user for consent: obtain it before the write.

    Write with read-back verification

    Request verification with NfcNdefWriteOptions(VerifyAfterWrite: true). The library reads the tag back on the same connection and compares the logical records:

    var message = new NfcNdefMessage([NfcNdefRtd.CreateTextRecord("Hello NFC", "en")]);
    
    async Task<NfcResult<bool>> WriteAsync(INfcTag tag, CancellationToken token) {
      if (tag.GetProtocol<INfcNdefTag>() is not { } ndef) {
        return NfcResult<bool>.Failure(NfcOperationContext.Create(tag.SessionId, tag.Id),
          new(NfcErrorCode.Unsupported, "app.ndef.missing"));
      }
      var write = await ndef.WriteAsync(message, new NfcNdefWriteOptions(VerifyAfterWrite: true), token);
      return write.IsSuccess
        ? NfcResult<bool>.Success(write.Context, true, write.Effects)
        : NfcResult<bool>.Failure(write.Context, write.Error!, write.Effects);
    }
    
    var result = await nfc.ScanOnceAsync(new NfcScanOptions(), WriteAsync, TimeSpan.FromSeconds(30), cancellationToken);
    

    ScanOnceAsync preserves the operation's effects in result.Effects. For multi-step flows, start a session with StartSessionAsync and call WriteAsync from TagDiscovered instead. On iOS prepare the message and obtain consent before starting: the system sheet covers the app.

    Interpret the result

    Read the mutation and the verification separately. A failed verification does not undo a completed write.

    Effects.Mutation Effects.Verification?.State Meaning What to do
    Completed Matched Written and confirmed. Done.
    Completed Mismatched Written, but the tag holds different content. Show the error; ask before writing again.
    Completed Unavailable Written; the read-back could not observe the content (portable.verification.unavailable). Read the tag again on a new tap before deciding.
    OutcomeUnknown any The write may or may not have reached the tag. Never replay automatically; read the tag on a new tap.
    NotAttempted none Nothing was written (for example capacity, read-only tag or cancellation before the write). Fix the cause from Error.PortableReasonCode.

    Effects.CleanupError is set when the connection could not be released; the result is then ResourceFailure while the mutation and verification effects above are preserved. See quarantine.

    Avoid common failures

    • Capacity: the complete serialized message must fit the tag's NDEF capacity; a larger message returns CapacityExceeded before any write.
    • Read-only tags: a read-only tag returns ReadOnly (portable.ndef.readOnly) without a write.
    • Empty messages: an empty record list is not an erase. Write an explicit NfcNdefTypeNameFormat.Empty record instead.
    • Formatting and locking are separate, explicitly confirmed operations and never a fallback of the write.

    Related

    • Result, effect and verification types: contracts.
    • Reason codes of failed writes: reason codes.
    • Cancellation during a write: cancellation.
    • Edit this page
    In this article
    Back to top Nementis MAUI NFC documentation