0
NFC writing Smartposter to a tag
In the last tutorial, we have briefly introduced how to write plain-text to a tag. in this serial post, I will introduce how we can write a smart poster record to a tag using an Android NFC supported devices, and use the class ParsedNdefRecord.java provided by Android API to parse the record.
According to the NFC forum smart poster specification, a smart poster record must contain a URI, and may contain a title, an action, a external resource type and size info etc. But here we will only include most possibly used info like title, Uri, action and type in our smart poster record to demonstrate the creation of an valid smart poster record . the smart poster record, after having being written to a tag, can then be read and parsed into a well formed and human readable information using the ParsedNdefRecord.java mentioned above.
So here is what we need to do:
create a smart poster record
//create an smart poster record that can be written to a NFC tag,
/*
title(optional) the title of smart poster(notice: in here this is required)
Uri(required) the Uri to the external resource
action(optional) the recommended action when user scans the poster
type(optional) the type of external resource that this poster refers
*/
public static NdefRecord creteSPoster(String title, String uri, RecomAction action,
String refType){
NdefRecord[] records=new NdefRecord[4];
NdefRecord spTitle=createText(title);
records[0]=spTitle;
NdefRecord spUri=createUri(uri);
records[1]=spUri;
if(action!=null){
NdefRecord spAct=new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
ACTION_RECORD_TYPE, new byte[0], new byte[]{action.getByte()});
records[2]=spAct;
}else
records[2]=getEmptyRecord();
if(refType!=null){
NdefRecord spType=new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
TYPE_TYPE, new byte[0], refType.getBytes());
records[3]=spType;
}else
records[3]=getEmptyRecord();
NdefMessage nm=new NdefMessage(records);
//one record contains many records
NdefRecord record= new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_SMART_POSTER, new byte[0] ,nm.toByteArray());
return record;
}
createText(): refers to the previous article
createUri() is the new nfc api in 4.0 that will be introduced in the next post.
//create empty record when your don't need some information in the record.
private static NdefRecord getEmptyRecord(){
byte[] empty ={};
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN, empty, empty, empty);
}
define an enum of recommended actions
public enum RecomAction {
UNKNOWN((byte) -1),
DO_ACTION((byte) 0),
SAVE_FOR_LATER((byte) 1),
OPEN_FOR_EDITING((byte) 2);
protected static final ImmutableMap LOOKUP;
static {
ImmutableMap.Builder builder = ImmutableMap.builder();
for (RecomAction action : RecomAction.values()) {
builder.put(action.getByte(), action);
}
LOOKUP = builder.build();
}
private final byte mAction;
private RecomAction(byte val) {
this.mAction = val;
}
public byte getByte() {
return mAction;
}
}
After you have created a smart poster, you can write it to a tag the same way writing plan-text to a tag.