1 | // Copyright (C) 2016 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only |
3 | |
4 | #include "qnearfieldmanager.h" |
5 | #include "qnearfieldmanager_p.h" |
6 | |
7 | #if defined(QT_SIMULATOR) |
8 | #include "qnearfieldmanager_simulator_p.h" |
9 | #elif defined(NEARD_NFC) |
10 | #include "qnearfieldmanager_neard_p.h" |
11 | #elif defined(ANDROID_NFC) |
12 | #include "qnearfieldmanager_android_p.h" |
13 | #elif defined(IOS_NFC) |
14 | #include "qnearfieldmanager_ios_p.h" |
15 | #elif defined(PCSC_NFC) |
16 | #include "qnearfieldmanager_pcsc_p.h" |
17 | #else |
18 | #include "qnearfieldmanager_generic_p.h" |
19 | #endif |
20 | |
21 | #include <QtCore/QMetaType> |
22 | #include <QtCore/QMetaMethod> |
23 | |
24 | QT_BEGIN_NAMESPACE |
25 | |
26 | /*! |
27 | \class QNearFieldManager |
28 | \brief The QNearFieldManager class provides access to notifications for NFC events. |
29 | |
30 | \ingroup connectivity-nfc |
31 | \inmodule QtNfc |
32 | \since 5.2 |
33 | |
34 | NFC Forum devices support two modes of communications. The first mode, peer-to-peer |
35 | communications, is used to communicate between two NFC Forum devices. The second mode, |
36 | master/slave communications, is used to communicate between an NFC Forum device and an NFC |
37 | Forum Tag or Contactless Card. The targetDetected() signal is emitted when a target device |
38 | enters communications range. Communications can be initiated from the slot connected to this |
39 | signal. |
40 | |
41 | NFC Forum devices generally operate as the master in master/slave communications. Some devices |
42 | are also capable of operating as the slave, so called Card Emulation mode. In this mode the |
43 | local NFC device emulates a NFC Forum Tag or Contactless Card. |
44 | |
45 | Applications can connect to the targetDetected() and targetLost() signals to get notified when |
46 | an NFC Forum Tag enters or leaves proximity. Before these signals are emitted target detection |
47 | must be started with the startTargetDetection() function. Target detection can be stopped with |
48 | the stopTargetDetection() function. When the target is no longer required the target should be |
49 | deleted as other applications may be blocked from accessing the target. |
50 | |
51 | \section3 NFC on Linux |
52 | The \l{https://github.com/linux-nfc/neard}{Linux NFC project} provides software to support NFC |
53 | on Linux platforms. The neard daemon will allow access to the supported hardware via DBus |
54 | interfaces. QtNfc requires neard version 0.14 which can be built from source or installed via |
55 | the appropriate Linux package manager. Not all API features are currently supported. |
56 | To allow QtNfc to access the DBus interfaces the neard daemon has to be running. In case of |
57 | problems debug output can be enabled by enabling categorized logging for 'qt.nfc.neard'. |
58 | */ |
59 | |
60 | /*! |
61 | \enum QNearFieldManager::AdapterState |
62 | |
63 | \since 5.12 |
64 | |
65 | This enum describes the different states a NFC adapter can have. |
66 | |
67 | \value Offline The nfc adapter is offline. |
68 | \value TurningOn The nfc adapter is turning on. |
69 | \value Online The nfc adapter is online. |
70 | \value TurningOff The nfc adapter is turning off. |
71 | */ |
72 | |
73 | /*! |
74 | \fn void QNearFieldManager::adapterStateChanged(AdapterState state) |
75 | |
76 | \since 5.12 |
77 | |
78 | This signal is emitted whenever the \a state of the NFC adapter changed. |
79 | |
80 | \note Currently, this signal is only emitted on Android. |
81 | */ |
82 | |
83 | /*! |
84 | \fn void QNearFieldManager::targetDetectionStopped() |
85 | |
86 | \since 6.2 |
87 | |
88 | This signal is emitted whenever the target detection is stopped. |
89 | |
90 | \note Mostly this signal is emitted when \l stopTargetDetection() has been called. |
91 | Additionally the user is able to stop the detection on iOS within a popup shown |
92 | by the system during the scan, which also leads to emitting this signal. |
93 | */ |
94 | |
95 | /*! |
96 | \fn void QNearFieldManager::targetDetected(QNearFieldTarget *target) |
97 | |
98 | This signal is emitted whenever a target is detected. The \a target parameter represents the |
99 | detected target. |
100 | |
101 | This signal will be emitted for all detected targets. |
102 | |
103 | QNearFieldManager maintains ownership of \a target, however, it will not be destroyed until |
104 | the QNearFieldManager destructor is called. Ownership may be transferred by calling |
105 | setParent(). |
106 | |
107 | Do not delete \a target from the slot connected to this signal, instead call deleteLater(). |
108 | |
109 | \note that if \a target is deleted before it moves out of proximity the targetLost() signal |
110 | will not be emitted. |
111 | |
112 | \sa targetLost() |
113 | */ |
114 | |
115 | /*! |
116 | \fn void QNearFieldManager::targetLost(QNearFieldTarget *target) |
117 | |
118 | This signal is emitted whenever a target moves out of proximity. The \a target parameter |
119 | represents the lost target. |
120 | |
121 | Do not delete \a target from the slot connected to this signal, instead use deleteLater(). |
122 | |
123 | \sa QNearFieldTarget::disconnected() |
124 | */ |
125 | |
126 | /*! |
127 | Constructs a new near field manager with \a parent. |
128 | */ |
129 | QNearFieldManager::QNearFieldManager(QObject *parent) |
130 | : QObject(parent), d_ptr(new QNearFieldManagerPrivateImpl) |
131 | { |
132 | qRegisterMetaType<AdapterState>(); |
133 | |
134 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::adapterStateChanged, |
135 | context: this, slot: &QNearFieldManager::adapterStateChanged); |
136 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetDetectionStopped, |
137 | context: this, slot: &QNearFieldManager::targetDetectionStopped); |
138 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetDetected, |
139 | context: this, slot: &QNearFieldManager::targetDetected); |
140 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetLost, |
141 | context: this, slot: &QNearFieldManager::targetLost); |
142 | } |
143 | |
144 | /*! |
145 | \internal |
146 | |
147 | Constructs a new near field manager with the specified \a backend and with \a parent. |
148 | |
149 | \note: This constructor is only enable for internal builds and is used for testing the |
150 | simulator backend. |
151 | */ |
152 | QNearFieldManager::QNearFieldManager(QNearFieldManagerPrivate *backend, QObject *parent) |
153 | : QObject(parent), d_ptr(backend) |
154 | { |
155 | qRegisterMetaType<AdapterState>(); |
156 | |
157 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::adapterStateChanged, |
158 | context: this, slot: &QNearFieldManager::adapterStateChanged); |
159 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetDetectionStopped, |
160 | context: this, slot: &QNearFieldManager::targetDetectionStopped); |
161 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetDetected, |
162 | context: this, slot: &QNearFieldManager::targetDetected); |
163 | connect(sender: d_ptr, signal: &QNearFieldManagerPrivate::targetLost, |
164 | context: this, slot: &QNearFieldManager::targetLost); |
165 | } |
166 | |
167 | /*! |
168 | Destroys the near field manager. |
169 | */ |
170 | QNearFieldManager::~QNearFieldManager() |
171 | { |
172 | delete d_ptr; |
173 | } |
174 | |
175 | /*! |
176 | \since 6.2 |
177 | |
178 | Returns \c true if the device has a NFC adapter and |
179 | it is turned on; otherwise returns \c false. |
180 | |
181 | \sa isSupported() |
182 | */ |
183 | bool QNearFieldManager::isEnabled() const |
184 | { |
185 | Q_D(const QNearFieldManager); |
186 | |
187 | return d->isEnabled(); |
188 | } |
189 | |
190 | /*! |
191 | \since 5.12 |
192 | |
193 | Returns \c true if the underlying device has a NFC adapter; otherwise |
194 | returns \c false. If an \a accessMethod is given, the function returns |
195 | \c true only if the NFC adapter supports the given \a accessMethod. |
196 | |
197 | \sa isEnabled() |
198 | */ |
199 | bool QNearFieldManager::isSupported(QNearFieldTarget::AccessMethod accessMethod) const |
200 | { |
201 | Q_D(const QNearFieldManager); |
202 | |
203 | return d->isSupported(accessMethod); |
204 | } |
205 | |
206 | /*! |
207 | \fn bool QNearFieldManager::startTargetDetection(QNearFieldTarget::AccessMethod accessMethod) |
208 | |
209 | Starts detecting targets and returns \c true if target detection started successfully; |
210 | otherwise returns \c false. Causes the targetDetected() signal to be emitted |
211 | when a target is within proximity. Only tags with the given \a accessMethod will be reported. |
212 | Target detection continues until \l stopTargetDetection() is called. |
213 | |
214 | To detect targets with a different \a accessMethod, stopTargetDetection() must be called first. |
215 | |
216 | \note On iOS, it is impossible to start target detection for both NdefAccess and TagTypeSpecificAccess |
217 | at the same time. So if AnyAccess is selected, NdefAccess will be used instead. |
218 | |
219 | \note On platforms using neard, target detection will stop as soon as a tag has been detected. |
220 | |
221 | \sa stopTargetDetection() |
222 | */ |
223 | bool QNearFieldManager::startTargetDetection(QNearFieldTarget::AccessMethod accessMethod) |
224 | { |
225 | Q_D(QNearFieldManager); |
226 | |
227 | return d->startTargetDetection(accessMethod); |
228 | } |
229 | |
230 | /*! |
231 | Stops detecting targets. The \l targetDetected() signal will no longer be emitted until another |
232 | call to \l startTargetDetection() is made. Targets detected before are still valid. |
233 | |
234 | \note On iOS, detected targets become invalid after this call (e.g. an attempt to write or |
235 | read NDEF messages will result in an error). |
236 | |
237 | If an \a errorMessage is provided, it is a hint to the system that the application's goal |
238 | was not achieved. The \a errorMessage and a matching error icon are shown to the user. |
239 | Calling this function with an empty \a errorMessage implies a successful end of operation; |
240 | otherwise, an \a errorMessage should be passed to this function. |
241 | |
242 | \note Currently, \a errorMessage only has an effect on iOS because the system shows a popup |
243 | during the scan where the \a errorMessage is visible. Other platforms will ignore this |
244 | parameter. |
245 | |
246 | \sa setUserInformation() |
247 | */ |
248 | void QNearFieldManager::stopTargetDetection(const QString &errorMessage) |
249 | { |
250 | Q_D(QNearFieldManager); |
251 | |
252 | d->stopTargetDetection(errorMessage); |
253 | } |
254 | |
255 | /*! |
256 | \since 6.2 |
257 | |
258 | Sets the message that the system shows to the user. If target detection is running, the |
259 | \a message will be updated immediately and can be used as a progress message. The last message |
260 | set before a call to \l startTargetDetection() without an error message is used as a success |
261 | message. If target detection is not running, the \a message will be used as the initial |
262 | message when the next detection is started. By default, no message is shown to the user. |
263 | |
264 | \note Currently, this function only has an effect on iOS because the system shows a popup |
265 | during the scan. On iOS, this \a message is mapped to the alert message which is shown upon |
266 | successful completion of the scan. Other platforms will ignore \a message. |
267 | |
268 | \sa startTargetDetection(), stopTargetDetection() |
269 | */ |
270 | void QNearFieldManager::setUserInformation(const QString &message) |
271 | { |
272 | Q_D(QNearFieldManager); |
273 | |
274 | d->setUserInformation(message); |
275 | } |
276 | |
277 | QT_END_NAMESPACE |
278 | |
279 | #include "moc_qnearfieldmanager_p.cpp" |
280 | |
281 | #include "moc_qnearfieldmanager.cpp" |
282 | |