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