nfc.* — NFC


Type Library


Overview

The NFC plugin enables your application to read NFC tags. NFC stands for Near Field Communication. At the moment the plugin only supports NDEF tags, but in the future based on your feedback, the plugin can be enhaced.

Supported platforms: Android 4.0+. Some features require Android 4.1+ or 6.0+. iOS 11+.

On iOS only iPhone 7 and later have capability to read NFC tags. And only NDEF tags are supported.

The latest version requires Corona 2016.3068 or later.

Syntax

local nfc = require('plugin.nfc')  

Functions

nfc.enableDebug()

nfc.init()

nfc.show()

nfc.close()

nfc.setListener()

nfc.removeListener()

nfc.writeTag()

nfc.releaseTag()

nfc.beam()

nfc.cancelBeam()

Events

init

nfc

writeTag

beam

Types

NdefMessage

Project Settings

To use this plugin, add an entry into the plugins table of build.settings. When added, the build server will integrate the plugin during the build phase.

settings = {
    plugins = {
        ['plugin.nfc'] = {
            publisherId = 'com.spiralcodestudio'
        }
    }
}

As per Apple requirement, you also have to add an NFC usage description. And a special entitlement record.

settings = {
    iphone = {
        plist = {
            NFCReaderUsageDescription = 'Testing NFC'
        },
        entitlements = {
			['com.apple.developer.nfc.readersession.formats'] = {'NDEF'}
		}
    }
}

You can replace 'Testing NFC' with your own description.

And don’t forget to enable “NFC Tag Reading” service for your iOS App Id on Apple’s developer portal.

If you want your Android application to be able to launch when an NFC device is discovered, you should add a specific intent filter into build.settings. You can read more on that and about NFC in general here.

For example a broad intent filter with low priority can look like this:

settings = {
	android = {
        intentFilters = {
            {
                actions = {'android.nfc.action.NDEF_DISCOVERED'},
                categories = {'android.intent.category.DEFAULT'}
            },{
                actions = {'android.nfc.action.TAG_DISCOVERED'},
                categories = {'android.intent.category.DEFAULT'}
            }
        }
    }
}

A narrower and hence higher priority intent filter that only wants NDEF tags with text/plain mime type can look like this:

settings = {
	android = {
        intentFilters = {
            {
                actions = {'android.nfc.action.NDEF_DISCOVERED'},
                categories = {'android.intent.category.DEFAULT'},
                data = {mimeType = 'text/plain'}
            }
        }
    }
}

You don’t need to specify any extra Android permissions.

When you want to use the plugin, require it, init it and set a listener.

local nfc = require('plugin.nfc')  
nfc.init(function(event)  
    print('Init event:', json.prettify(event))
end))

nfc.setListener(function(event)  
    print('NFC event:', json.prettify(event))
end))

On iOS you also have to call nfc.show() to open NFC reading dialog.

nfc.show{message = 'Place an NDEF tag.'}

Sample Project

A sample project can be found here.

https://github.com/Lerg/plugins-sample-nfc