Android Application Record compatibility with pre-ICS

1.4k Views Asked by At

There's a new method in NdefRecord that allows writing AndroidApplicationRecord to the NdefMessage. This was not necessary in pre Ice-Cream-Sandwich, but since then if you want to handle specific URI from a NFC tag in your application (like defined in the intent-filter) it will not be delivered to your application, unless you define that record.

createApplicationRecord(String packageName);

This is not available with some kind of compatibility package (I didn't find one), but the implementaion is fairly simple.

First add your NdefRecord you want to be readable by any NFC device (remember that URI can be formatted/shortened with URI_PREFIX_MAP)

NdefRecord[] nr = new NdefRecord[2];
nr[0] = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI, new byte[0], uriBytes);

Add your AAR in the next place

static final byte[] RTD_ANDROID_APP = "android.com:pkg".getBytes();
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
    nr[1] = NdefRecord.createApplicationRecord("your.package.name");
else
    nr[1] = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE, RTD_ANDROID_APP, new byte[] {}, "your.package.name".getBytes());
2

There are 2 best solutions below

0
On

You do not need an AAR to handle a specific URI. The AAR is just another method to guarantee that your app is started instead of another app that can handle the same URI.

0
On

The AAR guarantees on ICS that your app receives the NDEF message. It also does not have to be the first record in the NDEF message (which is what the Intent filter will be matched against). So it is quite different from URI or MIME type matching in an Intent filter. However, an AAR uses an External Type for the NDEF record, which is a kind of record that is not supported pre-ICS. So normally you should not use it as the first record of your NDEF message if you want it to work with pre-ICS devices.