write NFC Message - Android Network

Android examples for Network:NFC Message

Description

write NFC Message

Demo Code


//package com.java2s;
import java.io.IOException;

import java.nio.charset.Charset;

import java.util.Locale;

import android.content.Intent;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;

import android.nfc.tech.Ndef;
import android.nfc.tech.NdefFormatable;

public class Main {
    private final static String textEncoding = "UTF-8";

    public static Boolean writeMessage(String[] msg, Intent intent) {

        try {/*from  w ww .j  a  v  a2s. c  o m*/

            Tag detectedTag = (Tag) intent
                    .getParcelableExtra(NfcAdapter.EXTRA_TAG);

            NdefRecord[] records = new NdefRecord[msg.length];

            for (int i = 0; i < msg.length; i++) {
                records[i] = getNdefRecord(msg[i]);
            }

            NdefMessage ndefMessage = new NdefMessage(records);

            writeMsgToTag(ndefMessage, detectedTag);

        } catch (Exception e) {
            // TODO: handle exception
            return false;
        }

        return true;
    }

    private static NdefRecord getNdefRecord(String msg) {

        Locale locale = Locale.ENGLISH;

        byte[] langBytes = locale.getLanguage().getBytes(
                Charset.forName("US-ASCII"));

        Charset utfEncoding = Charset.forName(textEncoding);
        byte[] textBytes = msg.getBytes(utfEncoding);

        int utfBit = 0;
        char status = (char) (utfBit + langBytes.length);

        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte) status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length,
                textBytes.length);

        return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                NdefRecord.RTD_TEXT, new byte[0], data);

    }

    private static boolean writeMsgToTag(NdefMessage message,
            Tag detectedTag) {
        System.out.println("writing msg to tag");
        int size = message.toByteArray().length;
        try {
            Ndef ndef = Ndef.get(detectedTag);
            if (ndef != null) {
                ndef.connect();

                if (!ndef.isWritable()) {
                    //Toast.makeText(context, "Tag is read-only.", Toast.LENGTH_LONG).show();
                    return false;
                }
                if (ndef.getMaxSize() < size) {
                    //Toast.makeText(context, "The data cannot written to tag, Tag capacity is " + ndef.getMaxSize() + " bytes, message is " + size + " bytes.", Toast.LENGTH_LONG).show();
                    return false;
                }

                ndef.writeNdefMessage(message);
                ndef.close();
                //Toast.makeText(context, "Message is written over to tag, message=" + message, Toast.LENGTH_LONG).show();
                return true;
            } else {
                NdefFormatable ndefFormat = NdefFormatable.get(detectedTag);
                if (ndefFormat != null) {
                    try {
                        ndefFormat.connect();
                        ndefFormat.format(message);
                        ndefFormat.close();
                        //Toast.makeText(context, "The data is written to the tag ", Toast.LENGTH_SHORT).show();
                        return true;
                    } catch (IOException e) {
                        //Toast.makeText(context, "Failed to format tag", Toast.LENGTH_SHORT).show();
                        return false;
                    }
                } else {
                    //Toast.makeText(context, "NDEF is not supported", Toast.LENGTH_SHORT).show();
                    return false;
                }
            }
        } catch (Exception e) {
            //Toast.makeText(context, "Write opreation is failed", Toast.LENGTH_SHORT).show();
            return false;
        }

    }
}

Related Tutorials