Creates an Ndef message for NFC - Android Network

Android examples for Network:NFC Message

Description

Creates an Ndef message for NFC

Demo Code


//package com.java2s;

import java.nio.charset.Charset;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;

public class Main {
    /**/*w ww. j a  v  a2s.  c  o m*/
     * Creates an Ndef message
     * 
     * @param payload
     * @return
     */
    public static NdefMessage createMessage(String mimeType, byte[] payload) {
        // Min API Level of 14 requires an array as the argument
        return new NdefMessage(new NdefRecord[] { createRecord(mimeType,
                payload) });
    }

    /**
     * Creates a custom MIME type encapsulated in an NDEF record for a given
     * payload
     * 
     * @param mimeType
     */
    public static NdefRecord createRecord(String mimeType, byte[] payload) {
        byte[] mimeBytes = mimeType.getBytes(Charset.forName("US-ASCII"));
        NdefRecord mimeRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                mimeBytes, new byte[0], payload);
        return mimeRecord;
    }
}

Related Tutorials