Qt NFC Overview▲
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);
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.
Detecting NFC Tags▲
m_manager =
new
QNearFieldManager(this
);
connect(m_manager, &
amp;QNearFieldManager::
targetDetected,
this
, &
amp;MainWindow::
targetDetected);
connect(m_manager, &
amp;QNearFieldManager::
targetLost,
this
, &
amp;MainWindow::
targetLost);
m_manager-&
gt;startTargetDetection(QNearFieldTarget::
NdefAccess);
Finally the detection can be stopped:
m_manager-&
gt;stopTargetDetection();
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 QNearFieldManager::targetLost() signal:
void
MainWindow::
targetLost(QNearFieldTarget *
target)
{
target-&
gt;deleteLater();
}
The target object should only be deleted via deleteLater() if it is deleted inside the slot.
Connecting NFC Tags▲
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 QNearFieldManager::disconnect() is called.
Reading and Writing NDEF Messages▲
The QNearFieldTarget instance returned by QNearFieldManager::targetDetected() signal is used to interact with the tag. Reading and writing a message is an asynchronous operation. The QNearFieldTarget::RequestId class associates individual operations and their results.
void
MainWindow::
targetDetected(QNearFieldTarget *
target)
{
switch
(m_touchAction) {
case
NoAction:
break
;
case
ReadNdef:
connect(target, &
amp;QNearFieldTarget::
ndefMessageRead, this
, &
amp;MainWindow::
ndefMessageRead);
connect(target, &
amp;QNearFieldTarget::
error, this
, &
amp;MainWindow::
targetError);
m_request =
target-&
gt;readNdefMessages();
if
(!
m_request.isValid()) // cannot read messages
targetError(QNearFieldTarget::
NdefReadError, m_request);
break
;
case
WriteNdef:
connect(target, &
amp;QNearFieldTarget::
requestCompleted, this
, &
amp;MainWindow::
ndefMessageWritten);
connect(target, &
amp;QNearFieldTarget::
error, this
, &
amp;MainWindow::
targetError);
m_request =
target-&
gt;writeNdefMessages(QList&
lt;QNdefMessage&
gt;() &
lt;&
lt; ndefMessage());
if
(!
m_request.isValid()) // cannot write messages
targetError(QNearFieldTarget::
NdefWriteError, m_request);
break
;
}
}
Once the QNearFieldTarget::readNdefMessages() request was successfully processed, the QNearFieldTarget::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 QNearFieldTarget::writeNdefMessages(). The successful completion of the write operation is indicated by the emission of the QNearFieldTarget::requestCompleted() signal with the corresponding request id. Any type of error during read or write is indicated via QNearFieldTarget::error().