Pages

Wednesday, 10 September 2014

Delphi and NFC on Android

NFC is a neat technology.

If you’ve got an NFC sensor on your phone or tablet then there is a reasonable chance you’ve played around with apps like Trigger, which perform various actions on your device when you wave it over NFC tags in stickers or on key fobs etc. after a bit of setup.

So, for example, you could have an NFC sticker by the front door that has been coded by Trigger to turn off Wi-Fi and Bluetooth. With a variety of of NFC tags around and about you can set up your phone how you need it when you need it by a simple wave, avoiding a whole lot of fiddling about in the device settings.
Anyway, that’s one application of NFC tags. There are, of course, plenty more.

Indeed there have been a good number of questions in various forums asking how to get programmable NFC support in Delphi. Of course NFC is not currently “wrapped up” by Delphi’s RTL so up until now it’s been in the hands of the “creative”.

NFC tag scanning support in Android falls into 2 areas:

  1. You install an app that advertises that it can respond to some NFC tag content. When the device is waved against such a tag the app can be automatically launched. If more than 1 app is registered Android will initially launch a chooser dialog to allow the user to pick the app they require.
    Actually this support is easy enough to do in Delphi so long as you can locate a suitable import unit or two containing the required Android APIs for NFC.
  2. You run an app that permits the user to scan NFC tags while it is in the foreground and it directly responds to any scanned tags.
    This seems to be more commonly required in commercial applications and alas is much more difficult to achieve with Delphi. It falls into the category of problem that requires some Java smarts and some command prompt hackery to achieve.
    Bummer.

Anyway, I was recently looking into writing some NFC support, given the frequency of questions being asked. Part way through my research I discovered that Daniel Magin was simultaneously trying to do the same thing, primarily for a talk at Delphi Tage.

I helped Daniel out with the Java callback and command-line building trickery – that seems to be an area where I can readily add value to Android low level chicanery – and he helped me out overcoming a nasty crash bug with Java arrays of NFC data records not being surfaced correctly by the Delphi RTL.

In the end we both had some sample apps that permit NFC scanning and also, just as an added bonus, NFC writing!

You can find my solutions written up in quite some detail here:


Daniel’s efforts will also be made available very soon.

Enjoy coding up your NFC apps, everyone!

Update - Dec '15


A number of people had tried to use the code on versions of Android more recent than was current when I worked all this and hit problems with the code crashing. This was down to some inappropriate use of JNI constructs where the problem didn't originally show up but did show up as later versions of Android got more picky about things. I've remedied this issue in 2 ways.

1) I've updated the samples and code snippet in the article (it's OnNewIntentNative that has changed, FYI, in its usage of the intent JNI value).
2) However I've also devised a different sample app (for XE7, XE8 and 10 Seattle) that does much the same thing but doesn't really use explicit JNI. Instead it uses a listener interface implemented in Delphi and called from Java. The app has been slightly extended to allow the NFC support to be toggle on and off via a checkbox.

Updated samples:

Update – Sep ‘16

While catching up on what’s new in recent releases of Delphi I realised a change had been snuck into Delphi 10 Seattle that makes the whole NFC exercise much more straightforward now in Delphi. No more Java required! No more activity subclassing! No more debugging challenges!

It turns out that the key activity callback that NFC foreground dispatch relies upon (Activity.onNewIntent) is now hookable from Delphi code (with the right know-how) and so all the Java shenanigans can be put behind us (at least in the context of NFC access).

I have updated the article to show the more direct means of accessing NFC from Delphi 10 Seattle and later and provided corresponding updated samples for Delphi 10 Seattle and Delphi 10.1 Berlin.