Android examples for Network:NFC Message
write NFC Message To Tag
//package com.java2s; import java.io.IOException; import android.nfc.NdefMessage; import android.nfc.Tag; import android.nfc.tech.Ndef; import android.nfc.tech.NdefFormatable; public class Main { private static boolean writeMsgToTag(NdefMessage message, Tag detectedTag) {/*ww w .j av a2 s.c o m*/ 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; } } }