Android NFC: Smart Poster NDEF Record with concatenated "URI" and "Text" Contents -
i have developed multi-purpose nfc app, capable of reading , writing different types of tags , in different types of formats, including external type, rtd text, , rtd uri. there is, however, problem parsing , reading "tnf_well_known" smart poster contents. more specifically, , begin problem have, used well-known "nfc tagwriter" app on android create simple contact content. contact contains name, e.g., "police", , phone number, e.g., "911". when trying read same content using yet popular app called "nfc taginfo", able read smart poster, showing supposed show:
well-known: urn:nfc:wkt:sp (smart poster)
- well-known: urn:nfc:wkt:u (uri) identifier: 0x05 ("tel:") "911"
- well-known: urn:nfc:wkt:u (text) encoding: utf-8 language: en "police"
on other hand, not able achieve same. here code came attempt decode aforementioned content:
first extract ndef message(s) , corresponding ndef record(s):
// use ndefmessage[] getndefmessages(intent) method extract ndef message(s) ndefmessage[] ndefmessages = getndefmessages(intent); for(int = 0; < ndefmessages.length; i++) { ndefrecords = ndefmessages[i].getrecords(); (int j = 0; j < ndefrecords.length; j++) { // ndef record parsing snippet comes here! if (ndefrecords[j].gettnf() == ndefrecord.tnf_well_known) { // if tnf indicates "well-known" type, // determine type using "type" field if (arrays.equals(ndefrecords[j].gettype(), ndefrecord.rtd_smart_poster)) { // first try decode content payload = new string(ndefrecords[j].getpayload(), 1, ndefrecords[j].getpayload().length - 1, charset.forname("utf-8")); log.i(tag, "content: " + payload); // second try decode content log.i(tag, "content: " + ndefrecords[j].touri()); } } } } when looking @ logcat output, first log.i prints:
u911q tenpolice while second prints:
tel:911 it appears me nfc tagwriter app creates nested ndef record, in 1 uri type , 1 text type concatenated. trying figure out how content decoded successfully. first attempt removes payload header , decodes remaining part of ndef record payload. clearly, first part contains "tel:911" content , second part contains "police", "en" indicates language. second attempt uses ndefrecord class' touri() helper method, according api documentation able extract rtd uris. wondering in order extract nested records, decode one, convert byte[] array , remove payload, re-iterated yet decoding. though, approach looks inefficient. so, know other experts suggest on how approach problem.
[update] following short snippet compose smart poster ndef record interested:
ndefrecord urindefrecord = ndefrecord.createuri(uri.parse((string) o[0])); //rtd_uri ndefrecord textndefrecord = createtextrecord((string) o[1], locale.us, true); //rtd_text ndefrecord[] recs = new ndefrecord[]{urindefrecord, textndefrecord}; ndefmessage smartposterpcontentmessage = new ndefmessage(recs); byte[] bytes = smartposterpcontentmessage.tobytearray(); ndefrecord newrec = new ndefrecord((short) 0x01, ndefrecord.rtd_smart_poster, null, bytes); ndefmessagewriter(new ndefmessage(newrec)); in above snippet, "ndefmessagewriter" uses thread communicate tag , write content it.
as correctly found out smart poster record contains text record , uri record payload. have these records decode smart poster records payload (btw. if had googled "smart poster record" have come smart poster record type definition specification, grab here).
you found out sp record contains ndef message, decode record's payload ndef message:
ndefmessage spcontentmsg = new ndefmessage(sprecord.getpayload()); now have ndef message which, per specification, contains @ least 1 uri record , other optional records (e.g. 1 descriptive text record each language):
ndefrecord[] spcontentrecs = spcontentmsg.getrecords(); (ndefrecord rec : spcontentrecs) { if (rec.gettnf() == ndefrecord.tnf_well_known) { if (arrays.equals(rec.gettype(), ndefrecord.rtd_uri)) { // } else if (arrays.equals(rec.gettype(), ndefrecord.rtd_text)) { // } } } } btw, of nfc forum's specifications written , easy follow (particularly various ndef , rtd related specifications).
Comments
Post a Comment