I've started learning more about AAR and NFC and found this neat piece of code working. However, I have trouble understanding what it really does and is it already adding in the AAR into the NDEF message. Can someone guide me on what line of code does what? Thanks a lot!
private NdefMessage getTagAsNdef() {
boolean addAAR = false;
String uniqueId = "ichatime.com";
byte[] uriField = uniqueId.getBytes(Charset.forName("US-ASCII"));
byte[] payload = new byte[uriField.length + 1]; //add 1 for the URI Prefix
payload[0] = 0x01; //prefixes http://www. to the URI
System.arraycopy(uriField, 0, payload, 1, uriField.length); //appends URI to payload
NdefRecord rtdUriRecord = new NdefRecord(
NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
if(addAAR) {
// note: returns AAR for different app (nfcreadtag)
return new NdefMessage(new NdefRecord[] {
rtdUriRecord, NdefRecord.createApplicationRecord("com.example.ponpon")
});
} else {
return new NdefMessage(new NdefRecord[] {
rtdUriRecord});
}
}
An AAR is a form of External Type Record, for which you can download the spec from nfc-forum.org. An external type consists of three pieces of data:
Domain
Type
Value
I've written a library project which provides high-level NDEF record objects, which also includes external type records.
There is also a template for using NFC in activities you might be interested in :-)
Related
My Android app at the moment consumes a URL for a Google spreadsheet through pasting from the clipboard, reading a QR code, or reading from NFC. I'm having trouble writing to an NFC tag and I get this error:
[ERROR:nfa_rw_act.cc(1571)] Unable to write NDEF. Tag maxsize=137, request write size=171
I cannot write to this tag because the payload I'm trying to write it is larger than the writable space on it.
All I'm trying to do is write the URL I've already read (from clipboard or QR) to an NFC tag, and also add my app record so it launches the Play Store to my app in case the user doesn't have it installed already. Unfortunately, it seems this is too much data. I thought about maybe only including the spreadsheetID value, but I think this will add complications in the future when I inevitably want to add support for spreadsheets outside of Google Sheets.
Here's how I'm writing it currently:
public NdefMessage createNdefMessage() {
String text = "https://docs.google.com/spreadsheets/d/1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms/edit?usp=sharing";
NdefRecord appRecord = NdefRecord.createApplicationRecord(context.getPackageName());
NdefRecord relayRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, new String("application/" + context.getPackageName()).getBytes(Charset.forName("US-ASCII")), null, text.getBytes());
return new NdefMessage(new NdefRecord[] {relayRecord, appRecord});
}
Is writing just the ID ("1BxiMVs0XRA5nFMdKvBdBZjgmUUqptlbs74OgvE2upms" in this case) my only option? I'm also assuming the NFC tag I'm using is an average size and not a super tiny one for 2018.
EDIT:
Thanks to Michael I was able to get it to fit, though barely (134/137 bytes). I write the URI to NFC via this:
NdefRecord relayRecord = NdefRecord.createUri(text);
I added this intent filter to catch it:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/>
<category android:name="android.intent.category.DEFAULT"/>
<data android:scheme="https" android:host="docs.google.com"/>
</intent-filter>
And I read the NFC tag with this:
NdefMessage[] messages = new NdefMessage[rawMessages.length];
for (int i = 0; i < rawMessages.length; i++) {
messages[i] = (NdefMessage) rawMessages[i];
}
for (NdefRecord r : messages[0].getRecords()) {
if (r.getTnf() == NdefRecord.TNF_WELL_KNOWN) {
byte[] payload = r.getPayload();
try {
String payloadText = new String(payload, 1, payload.length - 1, "UTF-8");
int firstByte = payload[0];
return getUriPrefix(firstByte) + payloadText;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return "Read error";
}
}
}
And the first byte from the URI compression I get via this, even though I only ever assign "04" (4 when I read it as int) in my app:
private String getUriPrefix(int firstByte) {
if (firstByte == 0) {
return "";
} else if (firstByte == 1) {
return "http://www.";
} else if (firstByte == 2) {
return "https://www.";
} else if (firstByte == 3) {
return "http://";
} else if (firstByte == 4) {
return "https://";
} else {
return "";
}
}
The error message that you got in the log is pretty clear. Your NDEF message is too large to fit the data area of the tag. The obvious solution would be to use NFC tags with a sufficiently large storage capacity -- there's quite a few larger tags available. There's nothing else you could do about it if you want to store exactly that NDEF message.
However, there is ways to improve the NDEF message itself. You currently use a MIME type record to store a URL. That's definitely not the best choice of a record type to store a URL. (or actually any application-specific data). The MIME type that you chose costs ("application/" + context.getPackageName()).length() = 31 bytes (assuming your package name is 19 bytes).
If you used an NFC Forum external type record instead, you could create a much shorter type name of the form "mydomain.tld:appurl", so this would save quite a few bytes.
relayRecord = NdefRecord.createExternal("mydomain.tld", "appurl", text.getBytes());
Since you want to store a URL, there's even a more efficient (and readily available) record type: the NFC Forum URI well-known type. The type name of that record consists only of the single letter "U", so this would already save 30 bytes compared to your MIME type record. Moreover, the URI record type uses a compression scheme to further reduce the size of the record: there is a list of well-known URI prefixes (e.g. "https://") that can be represented as a single byte in the URI record (the method NdefRecord.createUri() will automatically take care of that compression). Consequently, you would save another 7 bytes.
relayRecord = NdefRecord.createUri(text);
I am trying to write a URI/URL to an NFC tag. I am able to do that successfully but there is a prefix that gets automatically added to the URI that I write.
For example: If the URL that I want to write is "https://indies.net.in", then the URL that is actually written on the tag is "https://www.enindies.net.in".
Can someone please tell me what am I'm doing wrong here?
Creating message:
private NdefMessage createNdefMessage(String content){
NdefRecord ndefRecord= createTextRecord(content);
NdefMessage ndefMessage=new NdefMessage(new NdefRecord[]{ndefRecord});
return ndefMessage;
}
Creating the URL record:
private NdefRecord createUrlRecord(String content) {
try{
byte[] language;
language= Locale.getDefault().getLanguage().getBytes();
final byte[] text=content.getBytes("UTF-8");
final int languageSize=language.length;
final int textLength=text.length;
final ByteArrayOutputStream payload= new ByteArrayOutputStream(1+languageSize+textLength);
payload.write((byte) (languageSize & 0x1F));
payload.write(language,0,languageSize);
payload.write(text,0,textLength);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,NdefRecord.RTD_URI,new byte[0],payload.toByteArray());
}catch (Exception e){
Log.e("createTextRecord",e.getMessage());
}
return null;
}
Writing message:
private void writeNdefMessage(Tag tag, NdefMessage ndefMessage){
try {
if (tag== null){
Toast.makeText(this," Tag object cannot be null",Toast.LENGTH_SHORT).show();
return;
}
Ndef ndef=Ndef.get(tag);
if (ndef==null){
formatTag(tag,ndefMessage);
} else {
ndef.connect();
if (!ndef.isWritable()){
Toast.makeText(this," Tag cannot be Written",Toast.LENGTH_SHORT).show();
ndef.close();
return;
}
ndef.writeNdefMessage(ndefMessage);
ndef.close();
Toast.makeText(this," Tag Written!",Toast.LENGTH_SHORT).show();
}
The problem with your method of writing the URL to the NFC tag is that you seem to have copied that from code that was originally intended to write NFC Forum Text records. This is also suggested by the fact that your method seems to have originally been called "createTextRecord" (at least that's what you call in createNdefMessage).
Now the problem with this is that Text records and URI records have a completely different format. Hence, if you put the payload of a Text record into a URI record you will get an unexpected outcome. In your case, the status byte (first byte of the Text record, containing the size of the language field) was mapped to the Identifier code of the URI record. As the value of that field was 0x02, this was translated to the URI prefix "https://www.". Moreover, the language field itself (containing the language code "en") was added to the URI.
In general, you would want to use the method NdefRecord.createUri() to create a proper URI record for your URL. This will automatically take care of normalizing and compressing the URI according to the compression scheme of the NFC Forum URI record type definition.
NdefRecord ndefRecord= NdefRecord.createUri("https://indies.net.in");
However, if you need to support platforms before API 14 (where this method was introduced) you could also create the URI record manually. For example, if you don't care about compression, you could use:
private NdefRecord createUrlRecord(String url) {
byte[] uriBytes = content.getBytes("UTF-8");
byte[] payload = new byte[uriBytes.length + 1];
payload[0] = (byte)0x00;
System.arraycopy(uriBytes, 0, payload, 1, uriBytes.length);
return new NdefRecord(NdefRecord.TNF_WELL_KNOWN,
NdefRecord.RTD_URI,
null,
payload);
}
NdefRecord ndefRecord= createUrlRecord("https://indies.net.in");
I have written a program in which I am trying to write and read NFC tag, which may help user in sending message. I have successfully written to the tag but whenever I am trying to read, the Tags app shows vnd.android.nfc://ext/nfclab.com:smsService but does not allow me to send the message.
WriteSmsActivity.java:
#Override
public void onNewIntent(Intent intent) {
Log.i("Foreground dispatch", "Discovered tag with intent: " + intent);
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String externalType = "nfclab.com:smsService";
String smsNumber = smsNumberEditText.getText().toString();
String smsBody = smsBodyEditText.getText().toString();
String urlAddress = "sms:"+smsNumber+"?body="+smsBody;
NdefRecord extRecord = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, externalType.getBytes(), new byte[0], urlAddress.getBytes());
NdefMessage newMessage = new NdefMessage(new NdefRecord[] { extRecord});
writeNdefMessageToTag(newMessage, tag);
}
}
Android displays the unrecognized NFC Forum external type nfclab.com:smsService (note that only lower-case letters should be used for external type names, see my answer here), because you stored that record type on your tag. That type is a custom type created by nfclab.com (for rather artificial examples in their book that won't work without a customized reader app) and by no means standardized. Consequently, Android does not know what it should do with that record.
The standard way to store ready-made SMS messages on NFC tags is the sms: URI scheme. Hence, you would typically create a URI record containing your sms: URI:
String smsUri = "sms:" + phone_number + "?body=" + text_message;
NdefRecord smsUriRecord = NdefRecord.createUri(smsUri);
Since Android 4.1 (or so), such records should be handled automatically by Android and will permit you to open the sms: URI in the default SMS app.
I have developped 2 Android applications. The first one, to write into an NFC tag, and the second to read the contents I have written .
This is what i did in the first application (WriteNFC)
private NdefRecord createRecord1(String data)
{
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);
}
private NdefRecord createRecord2(String data)
{
byte[] payload = data.getBytes(Charset.forName("UTF-8"));
byte[] empty = new byte[] {};
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);
}
And in the second application (ReadNFC)
NdefRecord cardRecord = msg.getRecords()[1];//Extract the second Record
String url_data = new String(cardRecord.getType());//Read data type
When I read with my own application (ReadNFC), of course I had on screen only the payload of the second Record, which I stored through "Record Type". But with a third-party application, especially that natively installed ("tag") -shown in photo-, It display correctly the first record, and for the second it's an empty field. How can I hide this field. Otherwise, how can I force the other third-party applications to not read the second record?
You simply cannot do that. Android will read the complete NDEF messages (i.e. all records) and pass it on in the Intent to an app.
Upps, it is no wonder this is happening, look at your code. First
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, empty, empty, payload);
then
return new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, payload, empty, empty);
So the 3rd party apps are really showing the correct data, you have a bug in your record creation.
The native NDEF support in android is somewhat crude (byte-array based), so I've written a library which helps in creating records - which you might find interesting. The above issue might be simple to resolve, but there are many other much more complex record types, so some help might come in handy ;-)
Edit: So if this is the preferred result, rather create an Unknown Record and put your 'secret' data as the payload - the will not be any good way for any 3rd party app to display that data - whereas the ID of the absolute URI Record certainly can be displayed by any NDEF-reading app (like mine?)
This third party app was bothersome to me, so I had to use foregroundDispatch to read the tag contents manually, there you have the freedom to read or not read anything you want.
This snippet is from the OnResume(). `
mNfcAdapter.enableForegroundDispatch(this, pendingIntent,
intentFiltersArray, techListsArray);
Toast.makeText(getApplicationContext(), "TAG disscovered",
Toast.LENGTH_LONG).show();
Parcelable[] rawMsgs = getIntent()
.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawMsgs != null) {
NdefMessage[] msgs = new NdefMessage[rawMsgs.length];
for (int i = 0; i < rawMsgs.length; i++) {
msgs[i] = (NdefMessage) rawMsgs[i];
setText=new String(msgs[i].getRecords()[0].getPayload());
}
mInfoText.setText(setText);
}
}`
Here I get payload of the 1st record.
I am asked to write the following data in smart poster tag.
1) URL of the discount offer
2) Count value (like offer for first 200 entries)
How to write these information in smart poster programmatically?
Is it something that can be written as key/value pairs?
Any pointers to sample code can help me a lot?
Thanks in advance.
I Think that that requirements 1) and 2), are two different levels.
Req. 1) is on the Android/NFC programming level, using TNF_WELL_KNOWN and RTD-SMART_POSTER(since this is a URL, usingRTD_URI works jus right).
here is some code:
private NdefRecord createRecord(String text)
throws UnsupportedEncodingException {
//Intent intent = getIntent();
//EditText editTextWeb = (EditText)
EditText editText = (EditText) findViewById(R.id.editTextWeblinks);
String webLink = editText.getText().toString();
byte[] uriField = webLink.getBytes(Charset.forName("US-ASCII"));
byte[] payload = new byte[uriField.length + 1]; //1 =URIPrefix
payload[0] = 0x01; //http://www. to the URI
System.arraycopy(uriField, 0, payload, 1, uriField.length); //appends URI to payload
NdefRecord rtdUriRecord = new NdefRecord(
NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], payload);
return rtdUriRecord;
}
On req. 2 we have a tricky part.
with Android/NFC API you can Identify each tag that you write but you can not add a sort of counter program logic within the NF Tag, this function have to be performed on an external application (name it on Android, PC, Mac, custom device, etc.).
Fancy way to do it: Build custom NFC terminal for sales and offers of your business.
Simpe way to do it: Build a simple app on the cashiers computers to scan a barcode (an image downloaded) and write a counter wi that event.
hope it helps.