Qt 6.x
The Qt SDK
Loading...
Searching...
No Matches
nfc-overview.qdoc
Go to the documentation of this file.
1// Copyright (C) 2013 Aaron McCarthy <mccarthy.aaron@gmail.com>
2// Copyright (C) 2017 The Qt Company Ltd.
3// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GFDL-1.3-no-invariants-only
4
5/*!
6\ingroup technology-apis
7\title Qt NFC Overview
8\page qtnfc-overview.html
9\brief Provides access to NFC enabled devices.
10
11\tableofcontents
12
13With the Qt NFC API typical use cases are:
14
15\list
16 \li Detecting NFC tags.
17 \li Reading and writing NDEF messages.
18 \li Registering NDEF message handlers.
19 \li Sharing files and messages.
20\endlist
21
22The following sections describe how to use Qt NFC C++ classes and QML types for the above use cases.
23
24\note On Android, the detection of new NFC tags only works in foreground applications. Android
25services do not support this because of API limitations on the Android side. The only way to use a
26\l{https://developer.android.com/reference/android/nfc/Tag}{Tag} in a service is to provide an
27\l{https://developer.android.com/guide/components/aidl}{AIDL} interface accepting the Tag and forward
28it to Qt as shown in the following example.
29
30\code
31 public void setTag(Tag pTag) {
32 Intent newIntent = new Intent();
33 newIntent.putExtra(NfcAdapter.EXTRA_TAG, pTag);
34 QtNative.onNewIntent(newIntent);
35 }
36\endcode
37
38\section1 C++ Overview
39
40The C++ API provides access to the full feature set of the Qt NFC API. This section introduces the
41major features available to developers.
42
43\section2 Detecting NFC Tags
44
45The \l QNearFieldManager class is responsible for the detection of new NFC tags coming
46into range of the device. The \l QNearFieldManager::targetDetected() and
47\l QNearFieldManager::targetLost() signals are emitted when
48a tag comes into or leaves the range. The passed \l QNearFieldTarget parameter acts
49as primary interaction point for each detected tag. The detection does not actually start though until
50\l QNearFieldManager::startTargetDetection() has been called.
51
52\code
53m_manager = new QNearFieldManager(this);
54connect(m_manager, &QNearFieldManager::targetDetected,
55 this, &MainWindow::targetDetected);
56connect(m_manager, &QNearFieldManager::targetLost,
57 this, &MainWindow::targetLost);
58m_manager->startTargetDetection(QNearFieldTarget::NdefAccess);
59\endcode
60
61Finally the detection can be stopped:
62
63\code
64m_manager->stopTargetDetection();
65\endcode
66
67Although each \l QNearFieldTarget instance is owned by its related \l QNearFieldManager
68instance it can be beneficial to manually delete each instance. Otherwise they would continue to
69exist until the \l QNearFieldManager instance is deleted. The best way to do that would be in response
70to the \l QNearFieldManager::targetLost() signal:
71
72\code
73void MainWindow::targetLost(QNearFieldTarget *target)
74{
75 target->deleteLater();
76}
77\endcode
78
79\note The target object should only be deleted via deleteLater() if it is deleted inside the slot.
80
81\section2 Connecting NFC Tags
82
83All functions of \l QNearFieldTarget that require a connection will create one by its own.
84An active connection will prevent other instances to create a connection because only one
85connection at the time is allowed.
86
87Qt 5 disconnected the tag at the end of the functions to allow other instances to connect.
88QNearFieldManager::setKeepConnection() allowed to change this behavior.
89
90Since Qt 6, \l QNearFieldTarget keeps the connection by default. The connection is only closed
91when the \l QNearFieldTarget is destroyed or \l QNearFieldManager::disconnect() is called.
92
93\section2 Reading and Writing NDEF Messages
94
95The \l QNearFieldTarget instance returned by \l QNearFieldManager::targetDetected() signal
96is used to interact with the tag. Reading and writing a message is an asynchronous operation.
97The \l QNearFieldTarget::RequestId class associates individual operations and their results.
98
99\code
100void MainWindow::targetDetected(QNearFieldTarget *target)
101{
102 switch (m_touchAction) {
103 case NoAction:
104 break;
105 case ReadNdef:
106 connect(target, &QNearFieldTarget::ndefMessageRead, this, &MainWindow::ndefMessageRead);
107 connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError);
108
109 m_request = target->readNdefMessages();
110 if (!m_request.isValid()) // cannot read messages
111 targetError(QNearFieldTarget::NdefReadError, m_request);
112 break;
113 case WriteNdef:
114 connect(target, &QNearFieldTarget::requestCompleted, this, &MainWindow::ndefMessageWritten);
115 connect(target, &QNearFieldTarget::error, this, &MainWindow::targetError);
116
117 m_request = target->writeNdefMessages(QList<QNdefMessage>() << ndefMessage());
118 if (!m_request.isValid()) // cannot write messages
119 targetError(QNearFieldTarget::NdefWriteError, m_request);
120 break;
121 }
122}
123\endcode
124
125Once the \l QNearFieldTarget::readNdefMessages() request was successfully processed, the
126\l QNearFieldTarget::ndefMessageRead() signal is emitted. Each returned \l QNdefMessage
127may consist of zero or more \l QNdefRecord entries, which can be identified by their type.
128For more information about processing of records, see the \l QNdefRecord class documentation.
129As the above code demonstrates, writing of NDEF messages is triggered via
130\l QNearFieldTarget::writeNdefMessages(). The successful completion of the write operation
131is indicated by the emission of the \l QNearFieldTarget::requestCompleted() signal with the
132corresponding request id. Any type of error during read or write is indicated via
133\l QNearFieldTarget::error().
134*/