QML Poster Example

The QML Poster example displays the contents of specifically formatted NFC Data Exchange Format (NDEF) messages read from an NFC Tag. The NDEF message must contain a URI record, an optional image/* MIME record, and one or more localized Text records.

运行范例

要运行范例从 Qt Creator ,打开 欢迎 模式,然后选择范例从 范例 。更多信息,拜访 构建和运行范例 .

Applying NDEF Filters

The example is designed to display the content of a very specific type of NFC tag. The tag must contain at least one URI record and one text record. If those two record types do not exist, nothing will happen. Such filtering is applied via the NearField type's filter property. The property accepts a list of NdefFilter 对象。

filter: [
    NdefFilter { type: "U"; typeNameFormat: NdefRecord.NfcRtd; maximum: 1 },
    NdefFilter { type: "T"; typeNameFormat: NdefRecord.NfcRtd },
    NdefFilter { typeNameFormat: NdefRecord.Mime; minimum: 0; maximum: 1 }
]
					

Processing Found NDEF Messages

Once an appropriate tag is found, the NearField::messageRecords property reflects the content. It transports the list of found NDEF records. The QML snippet below demonstrates how these records can be accessed:

onMessageRecordsChanged: {
    for (i = 0; i < messageRecords.length; ++i) {
        switch (messageRecords[i].typeNameFormat) {
        case NdefRecord.NfcRtd:
            if (messageRecords[i].type === "T") {
                if (messageRecords[i].localeMatch > currentLocaleMatch) {
                    currentLocaleMatch = messageRecords[i].localeMatch;
                    posterText.text = messageRecords[i].text;
                    found = true;
                }
            } else if (messageRecords[i].type === "U") {
                 posterUrl.text = messageRecords[i].uri;
                found = true;
            }
            break;
        case NdefRecord.Mime:
            if (messageRecords[i].type.indexOf("image/") === 0 ) {
                posterImage.source = messageRecords[i].uri;
                found = true;
            }
            break;
        }
        if (!found)
            console.warn("Unknown NFC tag detected. Cannot display content.")
    }
}
					

文件:

另请参阅 Qt NFC .