Code example for NdefRecord

0
    private NdefRecord createRandomMimeRecord(Random random) {
        byte[] mimeBytes = MIME_TYPE.getBytes(Charset.forName("US-ASCII"));
        byte[] id = new byte[4];
        random.nextBytes(id);
        byte[] payload = PAYLOAD.getBytes(Charset.forName("US-ASCII"));
        return new NdefRecord(NdefRecord.TNF_MIME_MEDIA, mimeBytes, id, payload);
    } 
 
    private void writeMessage(Tag tag, NdefMessage message) throws IOException, FormatException {
        Ndef ndef = null;
        try { 
            ndef = Ndef.get(tag);
            ndef.connect();
            ndef.writeNdefMessage(message);
        } finally { 
            if (ndef != null) {
                try { 
                    ndef.close();
                } catch (IOException e) {
                    Log.e(TAG, "IOException while closing NDEF...", e);
                }