Qt NFC Overview#

Provides access to NFC enabled devices.

With the Qt NFC API typical use cases are:

  • Detecting NFC tags.

  • Reading and writing NDEF messages.

  • Registering NDEF message handlers.

  • Sharing files and messages.

The following sections describe how to use Qt NFC C++ classes and QML types for the above use cases.


On Android, the detection of new NFC tags only works in foreground applications. Android services do not support this because of API limitations on the Android side. The only way to use a Tag in a service is to provide an AIDL interface accepting the Tag and forward it to Qt as shown in the following example.

public void setTag(Tag pTag) {
    Intent newIntent = new Intent();
    newIntent.putExtra(NfcAdapter.EXTRA_TAG, pTag);

C++ Overview#

The C++ API provides access to the full feature set of the Qt NFC API. This section introduces the major features available to developers.

Detecting NFC Tags#

The QNearFieldManager class is responsible for the detection of new NFC tags coming into range of the device. The targetDetected() and targetLost() signals are emitted when a tag comes into or leaves the range. The passed QNearFieldTarget parameter acts as primary interaction point for each detected tag. The detection does not actually start though until startTargetDetection() has been called.

m_manager = QNearFieldManager(self)
connect(m_manager, QNearFieldManager.targetDetected,
        self, MainWindow::targetDetected)
connect(m_manager, QNearFieldManager.targetLost,
        self, MainWindow::targetLost)        m_manager.startTargetDetection(QNearFieldTarget.NdefAccess)

Finally the detection can be stopped:


Although each QNearFieldTarget instance is owned by its related QNearFieldManager instance it can be beneficial to manually delete each instance. Otherwise they would continue to exist until the QNearFieldManager instance is deleted. The best way to do that would be in response to the targetLost() signal:

def targetLost(self, target):



The target object should only be deleted via deleteLater() if it is deleted inside the slot.

Connecting NFC Tags#

All functions of QNearFieldTarget that require a connection will create one by its own. An active connection will prevent other instances to create a connection because only one connection at the time is allowed.

Qt 5 disconnected the tag at the end of the functions to allow other instances to connect. QNearFieldManager::setKeepConnection() allowed to change this behavior.

Since Qt 6, QNearFieldTarget keeps the connection by default. The connection is only closed when the QNearFieldTarget is destroyed or disconnect() is called.

Reading and Writing NDEF Messages#

The QNearFieldTarget instance returned by targetDetected() signal is used to interact with the tag. Reading and writing a message is an asynchronous operation. The RequestId class associates individual operations and their results.

def targetDetected(self, target):

    switch (m_touchAction) {
    NoAction: = case()
    ReadNdef: = case()
        connect(target, QNearFieldTarget.ndefMessageRead, self, MainWindow.ndefMessageRead)
        connect(target, QNearFieldTarget.error, self, MainWindow.targetError)
        m_request = target.readNdefMessages()
        if (not m_request.isValid()) // cannot read messages
            targetError(QNearFieldTarget.NdefReadError, m_request)
    WriteNdef: = case()
        connect(target, QNearFieldTarget.requestCompleted, self, MainWindow.ndefMessageWritten)
        connect(target, QNearFieldTarget.error, self, MainWindow.targetError)
        m_request = target.writeNdefMessages(QList<QNdefMessage>() << ndefMessage())
        if (not m_request.isValid()) // cannot write messages
            targetError(QNearFieldTarget.NdefWriteError, m_request)

Once the readNdefMessages() request was successfully processed, the ndefMessageRead() signal is emitted. Each returned QNdefMessage may consist of zero or more QNdefRecord entries, which can be identified by their type. For more information about processing of records, see the QNdefRecord class documentation. As the above code demonstrates, writing of NDEF messages is triggered via writeNdefMessages() . The successful completion of the write operation is indicated by the emission of the requestCompleted() signal with the corresponding request id. Any type of error during read or write is indicated via error() .