Home > other >  Android NFC tag record not read-able
Android NFC tag record not read-able

Time:07-12

I have a little project to write data to Ntag 213, 215, and 216.

I success write the data using the MifareUltraLight method. The problem is when I scan using NfcTools, the data format is different than I expected.

Here's the format that I expected.

enter image description here

Here's the format that I got.

Expected format

I want that when the Ntag scan, it will open the browser if the apps not installed.

I'm using Mifare method because the Ntag will be protected by password. I tried two different way to write the data.

  1. I handle the write function manually using the command for Ntag. You can see the code below

     fun writePages(msg: String, mifare: MifareUltralight) {
         val uriString = Uri.parse(msg).normalizeScheme().toString()
         val subsUri = Uri.parse(msg).host.toString()
    
         val encodedPayload2 = uriString.toByteArray()
    
         val encodedData = byteArrayOf(0xD1.toByte(), 0x01.toByte(), (subsUri.length).toByte(), 0x55.toByte())   encodedPayload2
    
         val tlvEncodedData = byteArrayOf(0x03.toByte(), encodedData.size.toByte())   encodedData   byteArrayOf(0xFE.toByte())
    
         var currentPage = 4
         for (i in tlvEncodedData.indices step 4) {
             var currentBlock = i   4
             if (currentBlock > tlvEncodedData.size) {
                 currentBlock = tlvEncodedData.size
             }
    
             val data = tlvEncodedData.sliceArray(i until currentBlock)
             var command = byteArrayOf(0xA2.toByte(), currentPage.toByte())   data
    
             if (command.size < 6) {
                 for (w in command.size until 6) {
                     command  = 0.toByte()
                 }
             }
             mifare.transceive(command)
             currentPage  = 1
         }
     }
    
  2. I tried to write the data using this simple project. But the result is the same.

My question is, did I use the wrong method to write the data or something that I miss?

CodePudding user response:

Yes you used the wrong method to write, an Ntag213 is not an Ultralight C tag (though for some basic stuff they do share similar commands)

As you want the Android OS to automatically launch the URL, the first Ndef Record in the Ndef message must of the right type of Ndef message.

On Android you don't need to encode the Ndef Record and Ndef Message yourself and write to the Tag at the protocol level you are trying to use, as the Ntag21x series are fully Type 2 compliant you can write to it using the Ndef class

An example can be seen at https://stackoverflow.com/a/70943157/2373819

Note as in this example you need to handle both formatted and unformatted Tags

Also another example of writing a Ndef Message https://stackoverflow.com/a/64921434/2373819

For create the right type of Ndef Message and Record you can use NdefRecord.createUri

CodePudding user response:

NTAG213 is the wrong tag: You'd have to purchase MifareUltralight tags to use this class ... https://www.mifare.net/wp-content/uploads/2022/05/NXP-RFIDNFC-Portfolio_May-9-2022.pdf
Also see: How to set and unset password on a MIFARE Ultralight EV1 tag?

  • Related