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.

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.

Thursday 15 May 2014

Delphi Developer Days

Cary Jensen and Bob Swart are well into their Delphi Developer Days 2014 tour, having travelled around America and shortly to hit Europe and the UK. Tour dates are here.

As well as a varied agenda of Delphi sessions (full details here), each stop has a different guest speaker.

For example Jim McKeeth (Embarcadero), Nick Hodges and Ray Konopka (Raize Software) have been on the bill in America, and Bruno Fierens (TMS Software) and Michael Phillipenko and Denis Zubov (FastReports) are lined up for Europe.

When the tour gets to London I’m delighted to announce that I’m on the agenda as the guest speaker!

So this is a quick heads up that if you’re booked onto the tour at London, I’ll see you there when I’m up talking about creative debugging techniques. This is an updated version of a talk that was popular when I was more active on the conference circuit some decade or more ago.

Wednesday 7 May 2014

Launching other activities from Delphi Android apps

Following on from my recent post about adding Android app splash screens in Delphi XE5 and Delphi XE6 I’ve now spent some time writing up details on how to launch various Android system activities. This includes the more interesting business of getting feedback from launched activities; a common use case here is using a barcode scanner and finding what barcode what scanned.

Again there are two articles, one for Delphi XE5 and one for Delphi XE6. The XE5 version is much longer as it involves documenting a whole bunch of command-line shenanigans (much like for the splash screen) to hook the activity results. This is made much easier in Delphi XE6, thankfully.

I’ve tried to do a thorough coverage of the subject matter, highlighting any issues I bumped into – I hope it is of use to anyone tinkering with Android apps in Delphi.

Wednesday 23 April 2014

Splash screens in Delphi Android apps

I’ve been meaning to do it for quite a while and I recently started writing up my steps for creating an Android splash screen (as per my CodeRage 8 session). No sooner did I do so that Delphi XE6 launched itself onto the scene, making my write up immediately slightly out of date :-/

Unperturbed, I finished the job off and if you’re interested you can read it here. Hopefully that makes things clearer than the breakneck pace of the video….

I also then did the necessary updates for Delphi 6, taking account of the small changes that I’ve seen in this area. You can read the Delphi XE6 version here.

I’ll hopefully get some more words written onto the Web soon, regarding other Android features not wrapped up by Delphi’s Android FMX implementation, as per some of the other demos from the CodeRage session.