I've created a NFC read/write part of my application code, where:
READ is responsible for processing data and triggering a web browser:
ndef.connect()
val ndefMessage = ndef.ndefMessage
if (ndefMessage.records != null && ndefMessage.records.isNotEmpty()) {
message = String(it.records[0].payload)
}
ndef.close()
WRITE writes a URL onto the NFC tag:
ndef.connect()
val mimeRecord = NdefRecord.createUri(url)
ndef.writeNdefMessage(NdefMessage(mimeRecord))
ndef.close()
For some mysterious reason when I write the URL "http://www.google.com", my my message
variable (after reading a tag with that URL) contains only "google.com". I have no idea why the rest of the URL ("http://www.") is removed/ommited. Can you tell me what is going on? Where is my mistake?
You are trying to decode the payload of the NDEF record as simple text (UTF-8 encoded string):
message = String(it.records[0].payload)
However, you wrote the URL as an NFC Forum URI record:
NdefRecord.createUri(url)
Consequently, you need to interpret the URI record according to the NFC Forum URI Record Type Definition specification (avaibale from NFC Forum). The payload of such a URI record consists of one abbreviation byte (which you can use to lookup the URI prefix in a table of well-defined prefixes) and the URI suffix (UTF-8 encoded).
You could either parse the record payload on your own or let Android to the magic for you by using something like:
uriString = it.records[0].toUri()