QML 海報範例

A QML example about reading and displaying NFC Data Exchange Format (NDEF) messages.

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 ,打開 歡迎 模式,然後選擇範例從 範例 。更多信息,拜訪 構建和運行範例 .

應用 NDEF 過濾器

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 }
]
					
					

處理發現的 NDEF 消息

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 .