get NFC Ndef Mime Message - Android Network

Android examples for Network:NFC Message

Description

get NFC Ndef Mime Message

Demo Code


//package com.java2s;

import java.util.Random;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;

public class Main {
    public static NdefMessage getNdefMimeMessage(String mimeType,
            String content) {/*from  w w w.ja va  2  s  .  c  om*/
        NdefRecord mimeRecord = getMimeRecord(mimeType, content);
        return createNdefMessage(mimeRecord);
    }

    private static NdefRecord getMimeRecord(String mimeType, String content) {
        NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                mimeType.getBytes(), getRandomIdBytes(), content.getBytes());
        return record;
    }

    private static NdefMessage createNdefMessage(NdefRecord... records) {
        NdefMessage ndefMessage = new NdefMessage(records);
        return ndefMessage;
    }

    private static byte[] getRandomIdBytes() {
        Random rand = new Random(System.currentTimeMillis());
        byte buf[] = new byte[4];
        rand.nextBytes(buf);
        return buf;
    }
}

Related Tutorials