如何发送NfcA命令到MIFARE卡? [英] How send NfcA command to the MIFARE card?

查看:125
本文介绍了如何发送NfcA命令到MIFARE卡?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个Android应用程序.我正在尝试向我的Mifare Plus S卡发送NfcA低级命令(在我的情况下:HALT和WAKE-UP). NfcA技术用于低级"访问ISO 14443 Type A标签(即 ISO 14443-3中提到的专有协议.

I'm writing an Android application. I'm trying to send NfcA low-level commands (in my case: HALT and WAKE-UP) to my Mifare Plus S card. The NfcA tech is for "low-level" access to ISO 14443 Type A tags (i.e. the proprietary protocol as mentioned in ISO 14443-3).

这是我的代码的一部分:

This is part of my code:

protected void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        UID = Utils.byteArrayToHexString(intent.getByteArrayExtra(NfcAdapter.EXTRA_ID));
    }
    NfcA nfca = null;
    try {
        Log.e(TAG, "WakeUpCMD and HaltCMD");
        nfca = NfcA.get(tagFromIntent);
        nfca.connect();
        Short s = nfca.getSak();
        byte[] a = nfca.getAtqa();
        byte[] HaltCMD = {0x35, 0x30, 0x30,0x30, 0x00};
        byte[] WakeUpCMD = {0x35, 0x32, 0x00};
        byte[] data = null;
        try {
            data = nfca.transceive(HaltCMD);
            length = data.length;
        }
        catch (Exception e){
            Log.e(TAG, "HALT complete "+Utils.byteArrayToHexString(data));
        }
        Log.e(TAG, "Tag is connected: "+nfca.isConnected());
        //Log.e(TAG, "Response from tag "+Utils.byteArrayToHexString(data));
        nfca.setTimeout(100);
        data = nfca.transceive(WakeUpCMD);
        Log.e(TAG, "Response from tag"+Utils.byteArrayToHexString(data));
        nfca.close();
    } catch (Exception e) {
        Log.e(TAG, "Error when reading tag");

    }
} 

PC发出

唤醒命令,将进入HALT状态的PICC放回READY 状态.然后,他们应参加进一步的防撞和选择程序.

WAKE-UP Command is sent by the PCD to put PICCs which have entered the HALT State back into the READY State. They shall then participate in further anticollision and selection procedures.

|b7| |b6| |b5| |b4| |b3| |b2| |b1|

|1  |  | 0 |  | 1|  | 0|  | 0|  | 1|  | 0| {‘52’} = WAKE-UP

HALT 命令由四个字节组成,应使用标准帧进行传输. 传输的第一位

HALT Command consists of four bytes and shall be transmitted using the Standard Frame. First bit transmitted

S | ‘50’ | ‘00’ | CRC_A | E

如果PICC在HALT帧结束后的1毫秒内以任何调制方式做出响应,则该响应应被解释为未确认".

If the PICC responds with any modulation during a period of 1 ms after the end of the HALT Frame, this response shall be interpreted as 'not acknowledge'.

这是我尝试发送到卡上的ISO 14443-3中的两个命令的说明.

This is the descriptions of the two commands from ISO 14443-3 which I try to send to my card.

启动我的应用程序时,出现标签丢失"异常.你能帮助我吗?怎么了?如何发送这些命令?

When I start my app, I get a "Tag Lost" Exception. Can you help me? What's wrong? How can I send these commands?

推荐答案

在将命令代码发送给NfcA.transceive()之前,似乎要将命令代码转换为以空字符结尾的ASCII字符串:

It seems as if you are converting the command codes into null-terminated ASCII character strings before sending them with NfcA.transceive():

byte[] HaltCMD = {0x35, 0x30, 0x30,0x30, 0x00};
byte[] WakeUpCMD = {0x35, 0x32, 0x00};

  • 0x35 0x30 0x30 0x30给出"5000"
  • 0x35 0x32给出"52"
    • 0x35 0x30 0x30 0x30 gives "5000"
    • 0x35 0x32 gives "52"
    • 这没有任何意义,因为命令(对于HLTA是50 00,对于WUPA是52)已经是命令值的十六进制表示.

      This does not make any sense as the commands (50 00 for HLTA, and 52 for WUPA) are hexadecimal representations of the command values already.

      因此,对于HLTA命令,您需要发送50 00:

      For the HLTA command, you would therefore need to send 50 00:

      data = nfca.transceive(new byte[] { (byte)0x50, (byte)0x00 });
      

      请注意,NFC控制器(或NFC堆栈)将自动添加S(开始通信),E(结束通信)和CRC_A.

      Note that S (start of communication), E (end of communication), and CRC_A will be automatically added by the NFC controller (or the NFC stack).

      对于WUPA命令,您可能尝试发送52:

      For the WUPA command, you could probably try to send 52:

      data = nfca.transceive(new byte[] { (byte)0x52 });
      

      但是,很可能NFC堆栈不允许您使用收发方法发送7位命令.相反,堆栈可能会自动将此命令值用作一个完整字节并添加CRC_A.

      However, it is very likely that the NFC stack does not permit you to send 7-bit commands using the transceive method. Instead, the stack may automatically use this command value as one full byte and add a CRC_A.

      发送此类命令可能有效或无效(取决于NFC堆栈实现和NFC控制器).通常,我强烈建议您发送此类命令.特别是HLTA命令会混淆某些设备上NFC堆栈的内部状态,并会导致意外结果. 通常,您不需要交换此类命令,因为NFC设备会自动处理防冲突,初始化和激活.

      Sending such commands may or may not work (depending on the NFC stack implementation and the NFC controller). In general I would strongly recommend that you do not send such commands. Particularly the HLTA command will confuse the internal state keeping of the NFC stack on some devices and will lead to unexpected results. Normally, you don't need to exchange such commands, as anti-collision, initialization and activation are handled automatically by the NFC device.

      这篇关于如何发送NfcA命令到MIFARE卡?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆