use of net.rim.device.api.io.nfc.ndef.NDEFMessage in project Samples-for-Java by blackberry.
the class NfcReadNdefSmartTagUpdate method diyParsing.
/*
* This code has been replaced by the new 7.1 Helper classes. Kept here for reference purposes.
*/
private void diyParsing() {
/*
* The NDEF Message may consist of a number of NDEF records
*/
NDEFRecord[] records = _message.getRecords();
/*
* This is the number of NDEF records in the NDEF message
*/
int numRecords = records.length;
Utilities.log("XXXX NfcReadNdefSmartTagUpdate in runnable #records=" + numRecords);
/*
* Only unpick the message if it contains a non-zero number of records
*/
if (numRecords > 0) {
/*
* Work our way through each record in the message in turn
*/
for (int j = numRecords - 1; j >= 0; j--) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate processing NDEF record#=" + j);
/*
* Extract the NDEF record payload as an array of bytes
*
* This is an example of what an NDEF Smart Poster payload looks like representing:
*
* Text (en_US): This is a title for the BBC URI URI : http://www.bbc.co.uk
*
* Original length 59 decimal bytes
*
* NDEF PayLoad:
*
* 99 01 25 01 54 31 05 65 6E 2D :..%.T1.en-: 55 53 54 68 69 73 20 69 73 20 :USThis is : 61 20 74 69 74 6C 65 20
* 66 6F :a title fo: 72 20 74 68 65 20 42 42 43 20 :r the BBC : 55 52 49 59 01 0A 01 55 32 01 :URIY...U2.: 62 62
* 63 2E 63 6F 2E 75 6B :bbc.co.uk :
*
* Notice that "http://www." is so common it has a special binary encoding and only the remainder of the URL is
* text encoded "bbc.co.uk"
*/
byte[] payloadBytes = records[j].getPayload();
/*
* Parse and display
*/
StringBuffer hexPayload = new StringBuffer();
StringBuffer characterPayload = new StringBuffer();
String hexpair;
int numberOfHexPairs = 8;
for (int i = 0; i < payloadBytes.length; i++) {
hexpair = byte2HexPair(payloadBytes[i]);
characterPayload.append(byte2Ascii(payloadBytes[i]));
hexPayload.append(hexpair + " " + (((i + 1) % numberOfHexPairs == 0) ? "\n" : ""));
characterPayload.append((((i + 1) % numberOfHexPairs == 0) ? "\n" : ""));
}
/*
* Parse the elements of the NDEF record It should identify WELL_KNOWN, TEXT or URI and "Sp" for smart poster
*/
String record = "Root NDEF Record\nID: " + records[j].getId() + "\nType: " + records[j].getType() + "\nTNF: " + records[j].getTypeNameFormat() + "\nPayload Len: " + records[j].getPayload().length + "\nHex Payload: \n" + hexPayload + "\nCharacter Payload: \n" + characterPayload;
_screen.logEvent(record);
/*
* If we recognise this as a Smart Tag Type "Sp"
*/
if ("Sp".equals(records[j].getType())) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate recognised a Smart Poster Message");
try {
NDEFMessage smartPosterMessage = new NDEFMessage(records[j].getPayload());
NDEFRecord[] spRecords = smartPosterMessage.getRecords();
int numSpRecords = spRecords.length;
Utilities.log("XXXX NfcReadNdefSmartTagUpdate in runnable #records=" + numSpRecords);
if (numSpRecords > 0) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate processing Smart Poster Message");
for (int k = numSpRecords - 1; k >= 0; k--) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate processing SP record#=" + k);
byte[] spPayloadBytes = spRecords[k].getPayload();
hexPayload = new StringBuffer();
characterPayload = new StringBuffer();
for (int i = 0; i < spPayloadBytes.length; i++) {
hexpair = byte2HexPair(spPayloadBytes[i]);
characterPayload.append(byte2Ascii(spPayloadBytes[i]));
hexPayload.append(hexpair + " " + (((i + 1) % numberOfHexPairs == 0) ? "\n" : ""));
characterPayload.append((((i + 1) % numberOfHexPairs == 0) ? "\n" : ""));
}
String spRecordLog = "Subsidiary NDEF Record\nID: " + spRecords[k].getId() + "\nType: " + spRecords[k].getType() + "\nTNF: " + spRecords[k].getTypeNameFormat() + "\nPayload Len: " + spRecords[k].getPayload().length + "\nHex Payload: \n" + hexPayload + "\nCharacter Payload: \n" + characterPayload;
_screen.logEvent(spRecordLog);
if ((spRecords[k].getTypeNameFormat() == NDEFRecord.TNF_WELL_KNOWN) && "T".equals(spRecords[k].getType())) {
/*
* Well Known Type "T" is a TEXT record
*/
String text = new String();
String langCode = new String();
// bit 7 indicates UTF-8 if 0, UTF-16 if 1. Bits 5..0 len
int statusByte = spPayloadBytes[0];
// of IANA language code.
boolean is_utf16 = Utilities.isUtf16Encoded(statusByte);
int iana_language_code_len = Utilities.getIanaLanguageCodeLength(statusByte);
// extract the IANA language code as an ASCII string
byte[] iana_lang_code_bytes = new byte[iana_language_code_len];
if (iana_language_code_len > 0) {
for (int m = 0; m < iana_language_code_len; m++) {
iana_lang_code_bytes[m] = spPayloadBytes[m + 1];
}
}
langCode = new String(iana_lang_code_bytes, "US-ASCII");
// extract the text which may be UTF-8 or UTF-16 encoded depending on bit 7 of the status byte
byte[] text_bytes = new byte[spPayloadBytes.length - iana_language_code_len + 1];
int i = 0;
for (int m = iana_language_code_len + 1; m < spPayloadBytes.length; m++) {
text_bytes[i] = spPayloadBytes[m];
i++;
}
if (!is_utf16) {
text = new String(text_bytes, "UTF-8");
} else {
text = new String(text_bytes, "UTF-16");
}
_screen.logEvent("Language: " + langCode);
_screen.logEvent("Text: " + text);
} else if ((spRecords[k].getTypeNameFormat() == NDEFRecord.TNF_WELL_KNOWN) && "U".equals(spRecords[k].getType())) {
/*
* Well Known Type "U" is a URI record
*/
StringBuffer urlBuffer = new StringBuffer();
int urlOffset = 1;
if (spPayloadBytes[0] == (byte) 0x01) {
urlBuffer.append("http://www.");
} else if (spPayloadBytes[0] == (byte) 0x02) {
urlBuffer.append("https://");
} else if (spPayloadBytes[0] == (byte) 0x03) {
urlBuffer.append("http://");
} else if (spPayloadBytes[0] == (byte) 0x04) {
urlBuffer.append("https://");
}
// extract the URI which must be UTF-8 encoded
byte[] uri_bytes = new byte[spPayloadBytes.length - 1];
int i = 0;
for (int m = urlOffset; m < spPayloadBytes.length; m++) {
uri_bytes[i] = spPayloadBytes[m];
i++;
}
urlBuffer.append(new String(uri_bytes, "UTF-8"));
_screen.logEvent("URL: " + urlBuffer);
}
}
} else {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate empty Smart Poster Message");
}
} catch (BadFormatException e) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate bad NDEF format message");
e.printStackTrace();
} catch (NFCException e) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate NFC Exception");
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate UnsupportedEncodingException");
e.printStackTrace();
}
}
}
} else {
Utilities.log("XXXX NfcReadNdefSmartTagUpdate no records in message");
_screen.logEvent("This is not the tag you're looking for!\n" + "It contains no records!\n" + "You should search elsewhere!");
}
}
use of net.rim.device.api.io.nfc.ndef.NDEFMessage in project Samples-for-Java by blackberry.
the class NfcWriteNdefSmartTagListener method createSmartPosterTag.
private NDEFMessage createSmartPosterTag() throws IOException {
// do this the old school way!
String uri = (String) _tag_attrs.get(Constants.TAG_ATTRIBUTE_URI);
String text = (String) _tag_attrs.get(Constants.TAG_ATTRIBUTE_TEXT);
// (SP (TEXT, URL) message
NDEFMessage rootMessage = new NDEFMessage();
// (TEXT, URL) message
NDEFMessage ndefMessage = new NDEFMessage();
// Smart Poster Record
NDEFRecord rootRecord = new NDEFRecord();
// Tag Title TEXT record
NDEFRecord tagTitleRecord = new NDEFRecord();
// Tag URL record
NDEFRecord tagUrlRecord = new NDEFRecord();
// to build title
ByteArrayOutputStream titlePayload = new ByteArrayOutputStream();
// to build URL
ByteArrayOutputStream urlPayload = new ByteArrayOutputStream();
/*
* ================ Record 0 ===========================================
*
* This is the NDEF record that represents the title associated with the URL that will the URL part of the Smart Poster
* Tag
*/
// status byte: character encoding indicator bit plus length of
titlePayload.write((byte) URL_TEXT_LOCALE.length());
// locale language field
// locale language
titlePayload.write(URL_TEXT_LOCALE.getBytes("US-ASCII"));
/*
* This is the text to be associated with the Smart Poster Tag
*/
// Text
titlePayload.write(text.getBytes("UTF-8"));
titlePayload.flush();
/*
* Construct the record itself
*/
// record Id
tagTitleRecord.setId("0");
// It's TEXT type
tagTitleRecord.setType(NDEFRecord.TNF_WELL_KNOWN, "T");
// construct the record
tagTitleRecord.setPayload(titlePayload.toByteArray());
/*
* ================ Record 1 ===========================================
*
* This is the NDEF record that represents the URL associated with the title that will the Text part of the Smart Poster
* Tag
*/
// coded abbreviation for "http://www."
urlPayload.write((byte) 0x01);
// The rest of the URL
urlPayload.write(uri.getBytes());
urlPayload.flush();
/*
* Construct the record itself
*/
// record Id
tagUrlRecord.setId("1");
// It's a URL(I) type
tagUrlRecord.setType(NDEFRecord.TNF_WELL_KNOWN, "U");
// construct the record
tagUrlRecord.setPayload(urlPayload.toByteArray());
/*
* ================ Construct an NDEF MEssage ==========================
*
* This NDEF Message comprises the Title and URL records (TEXT, URL)
*/
ndefMessage.setRecords(new NDEFRecord[] { tagTitleRecord, tagUrlRecord });
/*
* ================ Wrap the message as a Smart Poster Tag ============
*
* What we have now is a single NDEF message with two records, a URL and some text associated with it. We now need to make
* that into a Smart poster Tag which is a well known type: "Sp"
*/
// Smart Poster Type
rootRecord.setType(NDEFRecord.TNF_WELL_KNOWN, "Sp");
// construct the record
rootRecord.setPayload(ndefMessage.getBytes());
/*
* ================ Construct an NDEF MEssage ==========================
*
* This NDEF message contains a single record encoding the Smart Poster Tag
*/
// (SP, (TEXT, URL))
rootMessage.setRecords(new NDEFRecord[] { rootRecord });
/*
* Return the Smart Poster Tag
*/
return rootMessage;
}
use of net.rim.device.api.io.nfc.ndef.NDEFMessage in project Samples-for-Java by blackberry.
the class NfcSender method buildNDEFMessages.
public NDEFMessage[] buildNDEFMessages() {
Utilities.log("XXXX NfcSender sending ProtocolMessage:" + _message);
NDEFMessage[] ndef_messages = null;
NDEFMessage game_message;
try {
byte[] payload = _message.marshall();
Utilities.log("XXXX NfcSender sending payload 0x" + ByteArrayUtilities.byteArrayToHex(payload));
game_message = NDEFMessageUtils.createExternalTypeMessage(Constants.SNEP_DOMAIN, Constants.SNEP_TYPE, payload);
ndef_messages = new NDEFMessage[] { game_message };
} catch (NFCException e) {
Utilities.log("XXXX NfcSender:" + e.getClass().getName() + ":" + e.getMessage());
}
return ndef_messages;
}
Aggregations