'Zebra write RFID in Android Kotlin preforming correctly with some tags but not others

I am working on a inventory management system where it's required to write asset ids (with length of maximum 17) in to an RFID tag.

First the problem was that when I write on a tag an ID with shorter length than the already written one, it keeps the non overridden characters in the tag. for ex: if the tag has the ID "123456789" written on it and I write id "abc" on the tag. The tag's asset id becomes abc456789. I tried killing and erasing the tag before writing but it didn't work.

After that, I though of appending zeros before the target ID until it reaches the maximum length (17) so that this way no asset id with shorter length will be written on the tag and after reading I remove all preceding zero's. This worked well with a certain tag but not with another one, I figured out that the other tag can't be written on with more than 12 characters but I don't get why and the problem isn't in the RFID tag since it works well in another application and can be written on with more than 12 characters.

I would be really thankful if anyone could help me identify why this tag has only 12 characters written on it and the rest of the asset ID is neglected even though the same code works with another RFID tag.

Here's the write tag method:

    fun writeTag(sourceEPC: String?, targetData: String): TagData? {
    errorMessage = ""
    try {
        val tagData = TagData()
        val tagAccess = TagAccess()
        val writeAccessParams = tagAccess.WriteAccessParams()
        writeAccessParams.accessPassword = 0
        writeAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_EPC
        writeAccessParams.offset = 2
        var paddedTargetData =  padLeftZeros(targetData,17)
        val targetDataInHex = HexStringConverter.getHexStringConverterInstance().stringToHex(if (paddedTargetData.length % 2 != 0) "0$paddedTargetData" else paddedTargetData)//if ODD
        val padded = targetDataInHex + RFID_ADDED_VALUE
        writeAccessParams.setWriteData(padded)
        writeAccessParams.writeRetries = 1
        writeAccessParams.writeDataLength = padded.length / 4 // WORD EQUALS 4 HEX
        reader!!.Actions.TagAccess.writeWait(sourceEPC, writeAccessParams, null, tagData)
        return tagData
    } catch (e: InvalidUsageException) {
        errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
        println(errorMessage)
        return null
    } catch (e: OperationFailureException) {
        errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
        println(errorMessage)
        return null
    } catch (e: UnsupportedEncodingException) {
        errorMessage = if (e.message == null) "" else e.message!!
        println(errorMessage)
        return null
    }
}

Read Full Tag method:

    fun readFullTag(sourceEPC: String): TagData? {
    errorMessage = ""
    try {
        val tagAccess = TagAccess()
        val readAccessParams = tagAccess.ReadAccessParams()
        readAccessParams.accessPassword = 0
        readAccessParams.memoryBank = MEMORY_BANK.MEMORY_BANK_TID
        readAccessParams.offset = 0
        return reader?.Actions?.TagAccess?.readWait(sourceEPC, readAccessParams, null, false)
    } catch (e: InvalidUsageException) {
        errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.info
        println(errorMessage)
        return null
    } catch (e: OperationFailureException) {
        errorMessage = "InvalidUsageException=" + e.vendorMessage + " " + e.results
        println(errorMessage)
        return null
    }
}

Handle Tag Data method:

    override fun handleTagData(tagData: Array<TagData?>?) {
    var readValue = ""

    if (!tagData.isNullOrEmpty()) readValue = tagData[0]!!.tagID.trimIndent().replace("\n", "")

    if (isWritingRFID) {
        isWritingRFID = false
        if (currentRFIDAssetCode.isNotEmpty())
            writeRFID(readValue, currentRFIDAssetCode)
    } else {
        CoroutineScope(Dispatchers.Main).launch {
            if (!pDialog.isShowing) readFullRFID(readValue)
        }
    }
}

Feel free to ask for any additional code or info.



Solution 1:[1]

Another way to solve this problem instead of padding with zeros is use a technique used a lot in NFC and the first byte of your data is the value of the length of the data (in Hex).

Therefore it does not matter if the old data is not zero'd out and you won't have a problem detecting if it is a zero for blanking or a real zero

e.g.

0A 31 32 33 34 35 36 37 38 39 30

or in text

10 1 2 3 4 5 6 7 8 9 0

would be overwritten with

03 41 42 43

or in text

3 A B C

resulting in text in memory of

3 A B C 4 5 6 7 8 9 0

But you would read the first byte to get the length of 3 and then read 3 more bytes to get in text

A B C

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Andrew