The QNearFieldTarget class provides an interface for communicating with a target device. 更多...
头: | #include <QNearFieldTarget> |
CMake: |
find_package(Qt6 REQUIRED COMPONENTS Nfc)
target_link_libraries(mytarget PRIVATE Qt6::Nfc) |
qmake: | QT += nfc |
继承: | QObject |
class | RequestId |
enum | AccessMethod { UnknownAccess, NdefAccess, TagTypeSpecificAccess, AnyAccess } |
flags | AccessMethods |
enum | Error { NoError, UnknownError, UnsupportedError, TargetOutOfRangeError, NoResponseError, …, TimeoutError } |
enum | Type { ProprietaryTag, NfcTagType1, NfcTagType2, NfcTagType3, NfcTagType4, …, MifareTag } |
QNearFieldTarget (QObject * parent = nullptr) | |
virtual | ~QNearFieldTarget () |
QNearFieldTarget::AccessMethods | accessMethods () const |
bool | disconnect () |
bool | hasNdefMessage () |
int | maxCommandLength () const |
QNearFieldTarget::RequestId | readNdefMessages () |
QVariant | requestResponse (const QNearFieldTarget::RequestId & id ) const |
QNearFieldTarget::RequestId | sendCommand (const QByteArray & 命令 ) |
QNearFieldTarget::Type | type () const |
QByteArray | uid () const |
bool | waitForRequestCompleted (const QNearFieldTarget::RequestId & id , int msecs = 5000) |
QNearFieldTarget::RequestId | writeNdefMessages (const QList<QNdefMessage> & messages ) |
void | disconnected () |
void | error (QNearFieldTarget::Error error , const QNearFieldTarget::RequestId & id ) |
void | ndefMessageRead (const QNdefMessage & message ) |
void | requestCompleted (const QNearFieldTarget::RequestId & id ) |
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.
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 (). |
QNearFieldTarget::AnyAccess
|
0xff
|
The target supports any of the known access types. |
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::ConnectionError
|
7
|
Failed to connect to the target. |
QNearFieldTarget::NdefReadError
|
8
|
Failed to read NDEF messages from the target. |
QNearFieldTarget::NdefWriteError
|
9
|
Failed to write NDEF messages to the target. |
QNearFieldTarget::CommandError
|
10
|
Failed to send a command to the target. |
QNearFieldTarget::TimeoutError
|
11
|
The request could not be completed within the time specified in waitForRequestCompleted (). |
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. This value is used if the NfcTagType4 cannot be further refined by NfcTagType4A or NfcTagType4B below. |
QNearFieldTarget::NfcTagType4A
|
5
|
An NFC tag type 4 target based on ISO/IEC 14443-3A. |
QNearFieldTarget::NfcTagType4B
|
6
|
An NFC tag type 4 target based on ISO/IEC 14443-3B. |
QNearFieldTarget::MifareTag
|
7
|
A Mifare target. |
[explicit]
QNearFieldTarget::
QNearFieldTarget
(
QObject
*
parent
= nullptr)
Constructs a new near field target with parent .
[虚拟]
QNearFieldTarget::
~QNearFieldTarget
()
Destroys the near field target.
Returns the access methods supported by this near field target.
Closes the connection to the target to enable communication with the target from a different instance. The connection will also be closed, when the QNearFieldTarget is destroyed. A connection to the target device is (re)created to process a command or read/write a NDEF messages.
返回
true
only if an existing connection was successfully closed; otherwise returns
false
.
[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.
返回
true
if at least one NDEF message is stored on the near field target; otherwise returns
false
.
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.
另请参阅 sendCommand ().
[signal]
void
QNearFieldTarget::
ndefMessageRead
(const
QNdefMessage
&
message
)
This signal is emitted when a complete NDEF message has been read from the target.
另请参阅 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.
注意: An attempt to read an NDEF message from a tag, that is in INITIALIZED state as defined by NFC Forum, will fail with the NdefReadError , as the tag is formatted to support NDEF but does not contain a message yet.
[signal]
void
QNearFieldTarget::
requestCompleted
(const
QNearFieldTarget::RequestId
&
id
)
This signal is emitted when a request id completes.
另请参阅 sendCommand ().
Returns the decoded response for request id . If the request is unknown or has not yet been completed an invalid QVariant 被返回。
发送 command 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 ().
Returns the type of tag type of this near field target.
Returns the UID of the near field target.
注意: On iOS, this function returns an empty QByteArray for a near field target discovered using NdefAccess 方法。
另请参阅 QNearFieldTarget::AccessMethod .
等待直到
msecs
milliseconds for the request
id
to complete. Returns
true
if the request completes successfully and the requestCompeted() signal is emitted; otherwise returns
false
.
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 requestCompleted () signal will be emitted when the write operation completes successfully; otherwise the error () 信号被发射。