Write NFC tag with data for moka - Android Network

Android examples for Network:NFC Tag

Description

Write NFC tag with data for moka

Demo Code


import android.annotation.SuppressLint;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import static fr.utc.nf28.moka.util.LogUtils.LOGE;
import static fr.utc.nf28.moka.util.LogUtils.LOGI;

public class Main{
    /**/*  w  w  w  . j a v a2  s.c o  m*/
     * result code : tag made read only
     */
    public static final int TAG_READ_ONLY = 0x00000001;
    /**
     * result code : not enough space while writing
     */
    public static final int TAG_NOT_ENOUGHT_FREE_SPACE = 0x00000002;
    /**
     * result code : writing success
     */
    public static final int TAG_WRITTEN_SUCCESS = 0x00000003;
    /**
     * result code : writing failed
     */
    public static final int TAG_WRITTEN_FAIL = 0x00000004;
    /**
     * result code : failed to format NDEF
     */
    public static final int TAG_UNABLE_TO_FORMAT_NDEF = 0x00000005;
    /**
     * result code : tag not support NDEF format
     */
    public static final int TAG_NOT_SUPPORT_FORMAT_NDEF = 0x00000006;
    /**
     * Log TAG
     */
    private static final String TAG = LogUtils.makeLogTag(NfcUtils.class);
    /**
     * Write tag with data for moka
     *
     * @param tag          tag detected by your device
     * @param data         data store in your tag
     * @param makeReadOnly turn your tag in readOnly mode
     * @return result code
     */
    @SuppressLint("NewApi")
    public static int writeMokaTag(Tag tag, String data,
            boolean makeReadOnly) {
        final NdefRecord[] records = { createMokaRecord(data) };
        final NdefMessage message = new NdefMessage(records);
        try {
            // check if tag is already NDEF formatted
            final Ndef ndef = Ndef.get(tag);
            if (ndef != null) {
                ndef.connect();
                if (!ndef.isWritable()) {
                    LOGE(TAG, "write tag failed : readOnly mode");
                    ndef.close();
                    return TAG_READ_ONLY;
                }

                //calculate our data size
                final int size = message.toByteArray().length;

                //check if there is enough place
                if (ndef.getMaxSize() < size) {
                    LOGE(TAG, "write tag failed : not enough free space");
                    ndef.close();
                    return TAG_NOT_ENOUGHT_FREE_SPACE;
                }

                //not a readOnly TAG && enough free place then write
                ndef.writeNdefMessage(message);

                //check is we want to turn this tag in readOnly Tag
                if (makeReadOnly) {
                    ndef.makeReadOnly();
                }
                LOGI(TAG, "write tag succeeded");
                ndef.close();
                return TAG_WRITTEN_SUCCESS;
                //tag isn't NDEF formatted
            } else {
                //try to format it
                final NdefFormatable format = NdefFormatable.get(tag);
                if (format != null) {
                    try {
                        format.connect();
                        format.format(message);
                        LOGI(TAG,
                                "tag ndef formatted and write tag succeeded");
                        return TAG_WRITTEN_SUCCESS;
                    } catch (IOException e) {
                        LOGE(TAG, "unable to format tag to NDEF format");
                        return TAG_UNABLE_TO_FORMAT_NDEF;
                    }
                } else {
                    LOGE(TAG, "tag does'nt support NDEF format");
                    return TAG_NOT_SUPPORT_FORMAT_NDEF;
                }
            }
        } catch (Exception e) {
            LOGE(TAG, "write tag failed");
        }

        return TAG_WRITTEN_FAIL;
    }
    /**
     * Create record with matching option to moka standard
     *
     * @param data your data
     * @return formatted NdefRecord
     * @throws UnsupportedEncodingException
     */
    @SuppressLint("NewApi")
    private static NdefRecord createMokaRecord(String data) {
        final byte[] uriBytes = data.getBytes(Charset.forName("UTF-8"));
        final int textLength = uriBytes.length;
        final byte[] payload = new byte[1 + textLength];

        System.arraycopy(uriBytes, 0, payload, 1, textLength);

        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_URI, new byte[0], payload);
    }
}

Related Tutorials