The QNearFieldTarget class provides an interface for communicating with a target device. 更多...
头: | #include <QNearFieldTarget> |
qmake: | QT += nfc |
Since: | Qt 5.2 |
继承: | QObject |
该类在 Qt 5.2 引入。
class | RequestId |
enum | AccessMethod { UnknownAccess, NdefAccess, TagTypeSpecificAccess, LlcpAccess } |
flags | AccessMethods |
enum | Error { NoError, UnknownError, UnsupportedError, TargetOutOfRangeError, NoResponseError, …, CommandError } |
enum | Type { ProprietaryTag, NfcTagType1, NfcTagType2, NfcTagType3, NfcTagType4, MifareTag } |
QNearFieldTarget (QObject * parent = nullptr) | |
virtual | ~QNearFieldTarget () |
virtual QNearFieldTarget::AccessMethods | accessMethods () const = 0 |
bool | disconnect () |
virtual bool | hasNdefMessage () |
bool | isProcessingCommand () const |
bool | keepConnection () const |
int | maxCommandLength () const |
virtual QNearFieldTarget::RequestId | readNdefMessages () |
QVariant | requestResponse (const QNearFieldTarget::RequestId & id ) |
virtual QNearFieldTarget::RequestId | sendCommand (const QByteArray & 命令 ) |
virtual QNearFieldTarget::RequestId | sendCommands (const QList<QByteArray> & 命令 ) |
bool | setKeepConnection (bool isPersistent ) |
void | setResponseForRequest (const QNearFieldTarget::RequestId & id , const QVariant & response , bool emitRequestCompleted = true) |
virtual QNearFieldTarget::Type | type () const = 0 |
virtual QByteArray | uid () const = 0 |
virtual QUrl | url () const |
virtual bool | waitForRequestCompleted (const QNearFieldTarget::RequestId & id , int msecs = 5000) |
virtual QNearFieldTarget::RequestId | writeNdefMessages (const QList<QNdefMessage> & messages ) |
void | disconnected () |
void | error (QNearFieldTarget::Error error , const QNearFieldTarget::RequestId & id ) |
void | ndefMessageRead (const QNdefMessage & message ) |
void | ndefMessagesWritten () |
void | requestCompleted (const QNearFieldTarget::RequestId & id ) |
virtual bool | handleResponse (const QNearFieldTarget::RequestId & id , const QByteArray & response ) |
void | reportError (QNearFieldTarget::Error error , const QNearFieldTarget::RequestId & id ) |
quint16 | qNfcChecksum (const char * data , uint len ) |
QNearFieldTarget provides a generic interface for communicating with an NFC target device. Both NFC Forum devices and NFC Forum Tag targets are supported by this class. All target specific classes subclass this class.
The type () function can be used to get the type of the target device. The uid () function returns the unique identifier of the target. The AccessMethods flags returns from the accessMethods () function can be tested to determine which access methods are supported by the target.
If the target supports NdefAccess , hasNdefMessage () can be called to test if the target has a stored NDEF message, readNdefMessages () 和 writeNdefMessages () functions can be used to get and set the NDEF message.
If the target supports TagTypeSpecificAccess , sendCommand () can be used to send a single proprietary command to the target and retrieve the response. sendCommands () can be used to send multiple proprietary commands to the target and retrieve all of the responses.
If the target supports LlcpAccess , the QLlcpSocket class can be used to connected to a service provided by the target.
This enum describes the access methods a near field target supports.
常量 | 值 | 描述 |
---|---|---|
QNearFieldTarget::UnknownAccess
|
0x00
|
The target supports an unknown access type. |
QNearFieldTarget::NdefAccess
|
0x01
|
The target supports reading and writing NDEF messages using readNdefMessages () 和 writeNdefMessages (). |
QNearFieldTarget::TagTypeSpecificAccess
|
0x02
|
The target supports sending tag type specific commands using sendCommand () 和 sendCommands (). |
QNearFieldTarget::LlcpAccess
|
0x04
|
The target supports peer-to-peer LLCP communication. |
The AccessMethods type is a typedef for QFlags <AccessMethod>. It stores an OR combination of AccessMethod values.
This enum describes the error codes that a near field target reports.
常量 | 值 | 描述 |
---|---|---|
QNearFieldTarget::NoError
|
0
|
没有出现错误。 |
QNearFieldTarget::UnknownError
|
1
|
发生无法识别的错误。 |
QNearFieldTarget::UnsupportedError
|
2
|
The requested operation is unsupported by this near field target. |
QNearFieldTarget::TargetOutOfRangeError
|
3
|
The target is no longer within range. |
QNearFieldTarget::NoResponseError
|
4
|
The target did not respond. |
QNearFieldTarget::ChecksumMismatchError
|
5
|
The checksum has detected a corrupted response. |
QNearFieldTarget::InvalidParametersError
|
6
|
Invalid parameters were passed to a tag type specific function. |
QNearFieldTarget::NdefReadError
|
7
|
Failed to read NDEF messages from the target. |
QNearFieldTarget::NdefWriteError
|
8
|
Failed to write NDEF messages to the target. |
QNearFieldTarget::CommandError
|
9
|
Failed to send a command to the target. |
This enum describes the type of tag the target is detected as.
常量 | 值 | 描述 |
---|---|---|
QNearFieldTarget::ProprietaryTag
|
0
|
An unidentified proprietary target tag. |
QNearFieldTarget::NfcTagType1
|
1
|
An NFC tag type 1 target. |
QNearFieldTarget::NfcTagType2
|
2
|
An NFC tag type 2 target. |
QNearFieldTarget::NfcTagType3
|
3
|
An NFC tag type 3 target. |
QNearFieldTarget::NfcTagType4
|
4
|
An NFC tag type 4 target. |
QNearFieldTarget::MifareTag
|
5
|
A Mifare target. |
Constructs a new near field target with parent .
[signal]
void
QNearFieldTarget::
disconnected
()
This signal is emitted when the near field target moves out of proximity.
[signal]
void
QNearFieldTarget::
error
(
QNearFieldTarget::Error
error
, const
QNearFieldTarget::RequestId
&
id
)
This signal is emitted when an error occurs while processing request id 。 error parameter describes the error.
[signal]
void
QNearFieldTarget::
ndefMessageRead
(const
QNdefMessage
&
message
)
This signal is emitted when a complete NDEF message has been read from the target.
另请参阅 readNdefMessages ().
[signal]
void
QNearFieldTarget::
ndefMessagesWritten
()
This signal is emitted when NDEF messages have been successfully written to the target.
另请参阅 writeNdefMessages ().
[signal]
void
QNearFieldTarget::
requestCompleted
(const
QNearFieldTarget::RequestId
&
id
)
This signal is emitted when a request id completes.
另请参阅 sendCommand ().
[虚拟]
QNearFieldTarget::
~QNearFieldTarget
()
Destroys the near field target.
[pure virtual]
QNearFieldTarget::AccessMethods
QNearFieldTarget::
accessMethods
() const
Returns the access methods support by this near field target.
Closes the connection to the target.
Returns true only if an existing connection was successfully closed.
该函数在 Qt 5.9 引入。
另请参阅 keepConnection () 和 setKeepConnection ().
[virtual protected]
bool
QNearFieldTarget::
handleResponse
(const
QNearFieldTarget::RequestId
&
id
, const
QByteArray
&
response
)
处理 response received for the request id . Returns true if the response is handled; otherwise returns false.
Classes reimplementing this virtual function should call the base class implementation to ensure that requests initiated by those classes are handled correctly.
The default implementation stores the response such that it can be retrieved by requestResponse ().
注意: 此函数可以被援引,通过元对象系统和从 QML。见 Q_INVOKABLE .
[虚拟]
bool
QNearFieldTarget::
hasNdefMessage
()
Returns true if at least one NDEF message is stored on the near field target; otherwise returns false.
Returns true if the target is processing commands; otherwise returns false.
Returns true if this feature is enabled.
该函数在 Qt 5.9 引入。
另请参阅 setKeepConnection () 和 disconnect ().
Returns the maximum number of bytes that can be sent with sendCommand . 0 will be returned if the target does not support sending tag type specific commands.
该函数在 Qt 5.9 引入。
另请参阅 sendCommand () 和 sendCommands ().
[虚拟]
QNearFieldTarget::RequestId
QNearFieldTarget::
readNdefMessages
()
Starts reading NDEF messages stored on the near field target. Returns a request id which can be used to track the completion status of the request. An invalid request id will be returned if the target does not support reading NDEF messages.
An ndefMessageRead () signal will be emitted for each NDEF message. The requestCompleted () signal will be emitted was all NDEF messages have been read. The error () signal is emitted if an error occurs.
[protected]
void
QNearFieldTarget::
reportError
(
QNearFieldTarget::Error
error
, const
QNearFieldTarget::RequestId
&
id
)
Reports the error for the request id by appending the signal emission to the event queue.
该函数在 Qt 5.12 引入。
Returns the decoded response for request id . If the request is unknown or has not yet been completed an invalid QVariant 被返回。
[虚拟]
QNearFieldTarget::RequestId
QNearFieldTarget::
sendCommand
(const
QByteArray
&
命令
)
发送 命令 to the near field target. Returns a request id which can be used to track the completion status of the request. An invalid request id will be returned if the target does not support sending tag type specific commands.
The requestCompleted () signal will be emitted on successful completion of the request; otherwise the error () 信号会被发射。
Once the request completes successfully the response can be retrieved from the requestResponse () function. The response of this request will be a QByteArray .
另请参阅 requestCompleted () 和 waitForRequestCompleted ().
[虚拟]
QNearFieldTarget::RequestId
QNearFieldTarget::
sendCommands
(const
QList
<
QByteArray
> &
命令
)
Sends multiple 命令 to the near field target. Returns a request id which can be used to track the completion status of the request. An invalid request id will be returned if the target does not support sending tag type specific commands.
If all commands complete successfully the requestCompleted () signal will be emitted; otherwise the error () signal will be emitted. If a command fails succeeding commands from this call will not be processed.
Once the request completes the response for successfully completed requests can be retrieved from the requestResponse () function. The response of this request will be a QList < QByteArray >.
另请参阅 requestCompleted () 和 waitForRequestCompleted ().
Preserves the connection to the target device after processing a command or reading/writing NDEF messages if
isPersistent
is
true
. By default, this behavior is not enabled.
返回
true
if enabling this feature was successful. A possible reason for a failure is the lack of support on the used platform.
Enabling this feature requires to use the disconnect () function too, to close the connection manually and enable communication with the target from a different instance. Disabling this feature will also close an open connection.
该函数在 Qt 5.9 引入。
另请参阅 keepConnection () 和 disconnect ().
Sets the decoded response for request id to response 。若 emitRequestCompleted 为 true requestCompleted () signal will be emitted for id ; otherwise no signal will be emitted.
另请参阅 requestResponse ().
[pure virtual]
QNearFieldTarget::Type
QNearFieldTarget::
type
() const
Returns the type of tag type of this near field target.
[pure virtual]
QByteArray
QNearFieldTarget::
uid
() const
Returns the UID of the near field target.
[虚拟]
QUrl
QNearFieldTarget::
url
() const
Returns the URL of the near field target.
[虚拟]
bool
QNearFieldTarget::
waitForRequestCompleted
(const
QNearFieldTarget::RequestId
&
id
,
int
msecs
= 5000)
等待直到 msecs milliseconds for the request id to complete. Returns true if the request completes successfully and the requestCompeted() signal is emitted; otherwise returns false.
[虚拟]
QNearFieldTarget::RequestId
QNearFieldTarget::
writeNdefMessages
(const
QList
<
QNdefMessage
> &
messages
)
Writes the NDEF messages in messages to the target. Returns a request id which can be used to track the completion status of the request. An invalid request id will be returned if the target does not support reading NDEF messages.
The ndefMessagesWritten () signal will be emitted when the write operation completes successfully; otherwise the error () 信号发射。
Returns the NFC checksum of the first len bytes of data .