write NFC Ndef Tag - Android Network

Android examples for Network:NFC Tag

Description

write NFC Ndef Tag

Demo Code


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

import android.content.Intent;

import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;

import android.nfc.tech.Ndef;

import android.util.Log;

public class Main {
    private static final String TAG = "NfcFilter";

    public static boolean writeNdefTag(Intent intent, NdefRecord[] records) {
        Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG); 
        if (tag == null) {
            Log.v(TAG, "TAGNULL");
        }/*from w  w w  . j  a  va2s.c o m*/
        Ndef ndef = Ndef.get(tag);
        if (ndef == null) {
            Log.v(TAG, "NDEFNULL");
            return false;
        }
        try {
            ndef.close();
            ndef.connect();
            NdefMessage msg = new NdefMessage(records);
            ndef.writeNdefMessage(msg);
        } catch (IOException e) {
            return false;
        } catch (FormatException e) {
            return false;
        }
        return true;
    }
}

Related Tutorials