You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

NDEF数据写入Bug:空扇区末尾写入0xFF而非0x00问题

Hey there, let's figure out why your write_ndef function is putting 0xFF instead of 0x00 at the end of empty sectors!

First, let's recap the problem: your function handles formatting NDEF data and writing it to tags just fine, but when a sector has empty space left, it's leaving 0xFF bytes instead of the expected 0x00 (the standard null terminator for NDEF empty regions).

Looking at your code snippet, here's the likely culprit and how to fix it:

The Root Cause

Most NFC tags (like MIFARE variants) come with sectors pre-erased to 0xFF. If you're only writing your NDEF data to the start of a sector and leaving the rest of the bytes untouched, those leftover 0xFF values stay in place instead of being overwritten to 0x00. Additionally, NDEF requires a 0x00 terminator to mark the end of valid data, which your current code might not be handling properly.

Fix Steps & Modified Code

Here's how to adjust your function to ensure empty sector space gets filled with 0x00:

int write_ndef(FreefareTag tag, uint8_t *data, const uint8_t type, size_t isize) {
    uint8_t *ndef_msg = data;
    size_t ndef_msg_len = isize;
    int sector_count;

    // Initialize the entire write buffer to 0x00 first
    // (Adjust the buffer size to match your tag's sector/block size)
    uint8_t write_data[4] = {0x00, 0x00, 0x00, 0x00}; 

    printf("Printing raw message...");

    // Copy your NDEF data into the pre-initialized buffer
    if (ndef_msg_len > sizeof(write_data)) {
        fprintf(stderr, "Error: Data exceeds sector block size!\n");
        return -1;
    }
    memcpy(write_data, ndef_msg, ndef_msg_len);

    // If this is the final block of the NDEF message, ensure we have a 0x00 terminator
    // (Replace is_last_sector_block with your actual logic to check if this is the last block)
    if (is_last_sector_block(sector_count)) {
        // If data fills the block entirely, we need an extra block with just 0x00 to terminate
        if (ndef_msg_len == sizeof(write_data)) {
            uint8_t terminator_block[4] = {0x00};
            freefare_tag_write(tag, sector_count + 1, terminator_block);
        }
    }

    // Write the prepared buffer to the tag
    int write_result = freefare_tag_write(tag, sector_count, write_data);
    return write_result;
}

Key Notes

  • Buffer Initialization: By initializing the entire write_data array to 0x00 upfront, any unused space in the sector will automatically be 0x00 instead of leftover 0xFF.
  • NDEF Terminator: NDEF messages need a 0x00 byte to signal the end of valid data. If your message fills an entire sector block, you'll need to write an extra block with just 0x00 to properly terminate it.
  • Sector Erasure: Some tags require explicit erasure before writing. If initializing the buffer doesn't fix the issue, check if you need to call a sector erase function (like freefare_tag_erase) before writing your data.

内容的提问来源于stack exchange,提问作者Bob

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:19:01