How to use NFC on Android

Note: NFC tag reading is supported on LiveCode as of v.9.0.0. However, due to a bug in the engine, NFC tag reading does not work as expected in LiveCode versions 9.6.9 to 9.6.11. The bug is fixed in LiveCode 9.6.12 RC-1 and above.

What is NFC?

Most smartphones have Near Field Communication, or NFC technology, built in these days. If you’ve ever used a mobile payments app like Samsung Pay or Google Pay, you already know how NFC works. In a nutshell, it is a proximity-based wireless communication standard. Unlike Wi-Fi or Bluetooth, however, NFC interaction is limited to an extremely short range. Besides smartphones, you can sometimes find NFC on tablets, speakers, collectibles, and even gaming consoles like the Nintendo Switch and 3DS. (source: https://www.androidauthority.com/what-is-nfc-270730/)

How does NFC work with LiveCode on Android?

It is possible to access data directly from NFC tags read by Android devices.

The functions mobileIsNFCAvailable and mobileIsNFCEnabled can be used to test if the device is able to read NFC tags.

The commands mobileEnableNFCDispatch and mobileDisableNFCDispatch can be used to control whether or not your app intercepts all NFC tags while in the foreground.

The handler nfcTagReceived will be sent with the tag data to your app whenever an NFC tag is read by the device.

Can you show an example?

We will create a simple sample stack that will use all these commands. Add these handlers to the card script:

on openCard
   mobileEnableNFCDispatch
end openCard

on closeCard
   mobileDisableNFCDispatch
end closeCard

on nfcTagReceived pTag
   answer "nfcTagReceived"
   
   local tID, tTagID
   put pTag["id"] into tID
   put decodeNFC(tID) into tTagID
   answer tTagID
   
   answer pTag["ndef"][1]["payload"]
end nfcTagReceived 

function decodeNFC pTagID
   -- convert a binary NFC tag ID to colon-delimited ASCII
   -- pTagID = scanned tag; hardware ID is binary
   local tAsciiTag
   get binaryDecode("H*",pTagID,tAsciiTag) -- contains no colon separators
   repeat with x = len(tAsciiTag) to 4 step -2 -- add colons
      put colon before char x-1 of tAsciiTag
   end repeat
   return tAsciiTag
end decodeNFC

When the card opens, we use mobileEnableNFCDispatch to allow our app to intercept all NFC tags while in the foreground. Similarly, when the card closes, we use mobileDisableNFCDispatch.

Next, we handle the nfcTagReceived message which will be sent with the tag data to your app whenever an NFC tag is read by the device. Note that the pTag parameter is an array (see the dictionary for a full list of the array keys), so we do some processing before presenting some of its key values, using a helper function (decodeNFC)

Note: The helper function is taken from here https://forums.livecode.com/viewtopic.php?t=37191 - Thanks Jacque!

We could also add a button to check if NFC is available and enabled in the device:

on mouseUp pButtonNumber
   answer "isAvailable: " && mobileIsNFCAvailable()
   answer "isEnabled: " && mobileIsNFCEnabled()   
end mouseUp

If these functions return true, then you can start using your Android device to scan NFC tags.

One final note - you have to make sure you check the NFC support checkbox in the Android standalone application settings:

You can find the sample stack here:

0 Comments

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.