Compose a message offline
Build an NDEF message from typed records before connecting a reader. This is useful for a size preview, automated tests, and forms that let users prepare a draft.
import com.rvi.application.NDefDecoder;import com.rvi.application.TlvEncoder;import com.rvi.application.creator.NDefMessageCreator;import com.rvi.domain.NDefMessage;import com.rvi.domain.NDefText;import com.rvi.domain.NDefUri;
public final class ComposeOffline{ public static void main(final String[] args) { final NDefMessage message = new NDefMessageCreator() .add(new NDefUri("https://rithush.dev")) .add(new NDefText("My portfolio", "en")) .build();
System.out.println("NDEF bytes: " + message.data().length); System.out.println("Tag bytes: " + new TlvEncoder().encode(message).length); System.out.println("Links: " + new NDefDecoder().decodeURIs(message)); }}For this exact message, the console shows the difference between the NDEF size and the size with its tag wrapper.
NDEF bytes: 35Tag bytes: 38Links: [https://rithush.dev]The builder keeps records in the order you add them. message.data() returns a copy of the NDEF bytes. For a tag-size preview, use TlvEncoder.encode(message).length to include the storage wrapper and terminator. The writer also checks the actual tag capacity and accounts for page padding.
| Content | Java type | Format |
|---|---|---|
| URL | NDefUri |
Absolute URI, such as HTTPS, tel: or mailto: |
| Text | NDefText |
Text with a language tag |
| Contact | NDefContact |
vCard 4.0 |
| Wi-Fi | NDefWifi |
Wi-Fi WSC credentials |
To store a draft, call reader.write(message) after connecting. To reconstruct supported typed records, call reader.readContents() or decode an existing NDefMessage with NDefDecoder. Wi-Fi credentials on a tag are readable by anyone who scans it with compatible software.