如何在UWP应用程序中通过NFC写入SRAM

时间:2017-09-25 14:27:21

标签: uwp nfc

我想将数据写入NFC标签的SRAM并从中读取数据。 有没有办法用UWP做到这一点?

我只能找到读/写NDEF消息的类,但这不是我想做的。

1 个答案:

答案 0 :(得分:0)

在SRAM上写就像在EEPROM上写。放我更喜欢使用快速写入命令。 我将在这里分享仅在SRAM上写的方法

private boolean writeOnSRAM(byte[] text, MifareUltralight mu) throws IOException {

    int blocksNumber = (int) Math.ceil(text.length / 64.0);
    int index = 0;
    byte NC_REG;
    long end_time = System.currentTimeMillis() + 3000;
    do {

        NC_REG = mu.readPages(SESSION_REGS_ADDRESS)[NC_REG_POS];

    }

    while (!testNc_Reg(NC_REG, WRITE_MODE) && System.currentTimeMillis() < end_time);

    if (System.currentTimeMillis() >= end_time) {
        Toast.makeText(context, "NC_REG is False", Toast.LENGTH_SHORT).show();
        return false;

    }
    int counter = 0;
    for (int blockCounter = 0; blockCounter < blocksNumber; blockCounter++) {
        byte[] actualBlock = new byte[64];


        if (blockCounter == blocksNumber - 1) {

            byte[] not64ByteBlock = Arrays.copyOfRange(text, index, text.length);
            System.arraycopy(not64ByteBlock, 0, actualBlock, 0, not64ByteBlock.length);

        } else {

            actualBlock = Arrays.copyOfRange(text, index, index + 64);
        }

        boolean isBlockWritten = writeOneBlockOnSram(actualBlock, mu);
        index += 64;
        counter = blockCounter;

    }
    return counter == blocksNumber - 1;

}

并且writeIneBlockOnSram是

    private boolean writeOneBlockOnSram(byte[] block, MifareUltralight mu) {

    byte[] writeFastCommand = {(byte) 0xA6, (byte) 0xF0, (byte) 0xFF};

    byte[] res = appendTwoByteArrays(writeFastCommand, block);

    byte[] answer = new byte[0];
    byte NS_REG = 0;
    long end_time = System.currentTimeMillis() + 3000;
    do {
        try {

            NS_REG = mu.readPages(SESSION_REGS_ADDRESS)[NS_REG_POS];
        } catch (IOException e) {
            Toast.makeText(context, "cannot read NS_REG ", Toast.LENGTH_SHORT).show();
        }


    }

    //test if SRAM_I2C_READY = 0 and I2C_LOCKED = 0
    // and if RF_LOCKED = 1
    while (!testNs_Reg(NS_REG, WRITE_MODE) && System.currentTimeMillis() < end_time);
    if (System.currentTimeMillis() >= end_time) {
        Toast.makeText(context, "NS_REG is False", Toast.LENGTH_SHORT).show();
        return false;
    }

    try {
        answer = mu.transceive(res);

    } catch (IOException e) {

        Toast.makeText(context, "cannot write one Block", Toast.LENGTH_SHORT).show();
    }
    // the answer of writing one block is ACK
    return answer[0] == 0x0A;
}

我希望您能弄清楚代码,我已经对其进行了测试并且可以工作。 最后,我需要再次对其进行编辑,以增强功能