0
0

NFC writing plain text to a tag

2011/11/08 · 评论 

Android currently supports writing well known of three types of most widely used NdefMessages: Plain text, URI and Smartposter. In this tutorial, I will briefly introduce how we can use NFC supported Android devices writing plain text to a tag.

this tutorial assumes that you already know all the concept around NFC technologies, and that you are familiar with Android programming. if you are in doubt, please start reading NFC articles from the Android developer site. this tutorial aims to provide a fast learning path to NFC writing tags.

First of all, if you are trying to write an NDEF message to a tag, you need to create an NdefRecord which are sub elements of any NDEFMessage, then wrap the NdefRecord in an NdefMessage and write to a tag.

the following code creates an NdefRecord of plain text type:

public static NdefRecord createText(String text){
	try {
		Locale locale = Locale.US;
		final byte[] langBytes = locale.getLanguage().getBytes("US_ASCII");
		final byte[] textBytes = text.getBytes("UTF_8");
	    final int utfBit = 0;
	    final char status = (char) (utfBit + langBytes.length);
	    final byte[] data=Bytes.concat(new byte[]{(byte) status},langBytes,textBytes);
		NdefRecord record = new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
                             NdefRecord.RTD_TEXT, new byte[0], data);
		return record;
	}catch (UnsupportedEncodingException e) {
		e.printStackTrace();
	}
	return null;
}

in the above code, the most important part is:

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

Here, you create an NdefRecord of type RTD_TEXT, which is one of well known type formats defined in the android api. for more detail, please refers to NdefRecord API. Because NFC I/O must be perfomed with bytes, the string text must be convered to bytes array before writing to a tag. This is all it does in the simple method.

Create an NdefMessage:
Now that we have the NdefRecord, we just need to create an NdefMessage and write it to the tag.

NdefRecord[] records = new NdefRecord[1];
records[0] = record;
NdefMessage message = new NdefMessage(records);

Writing to a tag:

public void writeToTag(Intent intent, NdefMessage message){
	Tag t = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
    	Ndef tag = Ndef.get(t);
	try {
		tag.connect();
		tag.writeNdefMessage(message);
		tag.close();
	} catch (FormatException e) {
		Log.e(TAG,"the NdefMessage to write is mulformed",e);
	} catch (IOException e) {
		Log.e(TAG,"can not write to the tage! is the tag removed?",e);
	}
}

This method accepts an intent which you received from the Android NFC service when a tag is discovered, and the NdefMessage that we have created above. please note that the tag you are trying to write with an NdefMessage must support Ndef message format. This could be an ndefFormatable tag such as Mifare classic, NdefA, Ndef,NdefFormatable tag etc.

The next tutorial will introduce writing an URi to a tag, make sure to check back if you are interested.

您可能也喜欢

与大家分享点什么吧: