How to properly encode a URL onto an NFC tag?

坚强是说给别人听的谎言 提交于 2021-01-04 18:40:48

问题


I have a Mifare ULC card. When I tap this card to an NFC enabled device, it should open the default browser in phone without any additional NFC application.

I have encoded the below NDEF URL data to the tag, but when i scan the tag, it does not open the browser. Can anyone guide me where I did the mistake?

03 - tag for the NDEF
12 - length of the NDEF msg (18 Bytes)
D3    Record header (of first and only record)
      Bit 7 = MB = 1: first record of NDEF message
      Bit 6 = ME = 1: last record of NDEF message
      Bit 5 = CF = 0: last or only record of chain
      Bit 4 = SR = 1: short record length field
      Bit 3 = IL = 0: no ID/ID length fields
      Bit 2..0 = 011 = 0x3: Absolute URI Record type
01    Type Length = 1 byte
0E    Payload length = 14 bytes
55    Type field "U" (in US-ASCII) = for URI record
02656E02676F6F676C652E636F6D    Payload field (decoded according to the value of the Type field)- 14 Bytes

02    Status byte
      Bit 7 = 0: Text is UTF-8 encoded
      Bit 6 = 0: Not used
      Bit 5..0 = 0x02: Length of IANA language code field
656E  IANA language code field
      "en" (in US-ASCII) = Text is in English
02676F6F676C652E636F6D URL 0x02 = https://www. (URI identifier code) + 676F6F676C652E636F6D = google.com
      "https://www.google.com" (in UTF-8)

回答1:


You are mixing a few different record types here:

  • The absolute URI record type,
  • the NFC Forum well-known type URI, and
  • the NFC Forum well-known type Text.

You record header declares the record to be an absolute URI record type (TNF = 3). This record type uses are URI for the type name field (the field that tells applications how to interpret the record payload). Hence, the URI is not the actual record payload in that case but only the descriptor for the record contents. In your case, such a record could look like this:

+-------------------------+----------------------------------------------------------------
| D3                      | Record header (MB = ME = 1, CF = 0, SR = 1, IL = 0, TNF = 0x3)
+-------------------------+----------------------------------------------------------------
| 16                      | Type Length (22 bytes)
+-------------------------+----------------------------------------------------------------
| 00                      | Payload Length (0 bytes)
+-------------------------+----------------------------------------------------------------
| 68 74 74 70 73 3A 2F 2F | Type Name ("https://www.google.com")
| 77 77 77 2E 67 6F 6F 67 |
| 6C 65 2E 63 6F 6D       |
+-------------------------+----------------------------------------------------------------

While Android would still treat this record as a URI and should open it in a web browser, this is certainly not what the creators of the NDEF specification intended absolute URI records to be used for.

Instead, the NFC Forum specified the URI well-known type for this purpose. You already used parts of that since your type name ("U") and parts of the format of your payload match those of the URI well-known record type. However, in order to declare your record to be a well-known type record, you need to set the TNF field to 1. Moreover, the payload of the URI record type consists of one identifier byte (abbriviated URI prefix) and the truncated URI.

+-------------------------+----------------------------------------------------------------
| D1                      | Record header (MB = ME = 1, CF = 0, SR = 1, IL = 0, TNF = 0x1)
+-------------------------+----------------------------------------------------------------
| 01                      | Type Length (1 byte)
+-------------------------+----------------------------------------------------------------
| 0B                      | Payload Length (11 bytes)
+-------------------------+----------------------------------------------------------------
| 55                      | Type Name ("U")
+-------------------------+----------------------------------------------------------------
| 02 67 6F 6F 67 6C 65 2E | Payload: Identifier code = 2 (prefix "https://www."),
| 63 6F 6D                |          truncated URI = "google.com"
+-------------------------+----------------------------------------------------------------



回答2:


You need to remove your 'language code field' and the byte you have called the 'status byte'.

The first byte after the Record Type (55h - which you called the type field), should be the URI identifier (02h, which defines 'https://www.'). Then your URI.

Check your new payload length as well as I've quickly calculated it as 12 characters (0Ch).



来源:https://stackoverflow.com/questions/52942151/how-to-properly-encode-a-url-onto-an-nfc-tag

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!