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.
Note
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);
QtNative.onNewIntent(newIntent);
}
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.
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.
void MainWindow::targetDetected(QNearFieldTarget *target) { switch (m_touchAction) { case NoAction: break; case ReadNdef: connect(target, &QNearFieldTarget::ndefMessageRead, this, &MainWindow::ndefMessageRead); connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError); m_request = target->readNdefMessages(); if (!m_request.isValid()) // cannot read messages targetError(QNearFieldTarget::NdefReadError, m_request); break; case WriteNdef: connect(target, &QNearFieldTarget::requestCompleted, this, &MainWindow::ndefMessageWritten); connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError); m_request = target->writeNdefMessages(QList<QNdefMessage>() << ndefMessage()); if (!m_request.isValid()) // cannot write messages targetError(QNearFieldTarget::NdefWriteError, m_request); break; } }
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()
.