NFC reader “SELECT (by AID)” APDU is not routed to Android device

时光毁灭记忆、已成空白 提交于 2019-11-30 15:38:22

You used InListPassiveTarget to directly instruct the PN532 NFC chip inside the ACR122U to manually poll for tags. This essentially bypasses the abstraction layers of the ACR122U that allow you to automatically poll for tags and use "standard PC/SC" to exchange APDU commands with the enumerated smartcard. Consequently, sending plain APDUs over the PC/SC interface won't work and the SELECT APDU will never arrive at the Android HCE side.

Instead, you will also need to exchange APDU commands by speaking directly with the PN532 transmission module. You can do this by wrapping your APDU command inside an InDataExchange command (or InCommunicateThru if you need control over ISO/IEC 14443-4 header fields). In your case, the wrapped SELECT (by AID) command APDU would look something like:

execute(channel, new byte[] {
    (byte)0xFF, 0x00, 0x00, 0x00,  // direct PN532 command
    16,                // Lc = command length
    (byte)0xD4, 0x40,  // InDataExchange
    0x01,              // Tag #1 (equal to the tag number from the InListPassiveTarget response)
    0x00, (byte)0xA4, 0x04, 0x00,                         // APDU: SELECT (by AID)
          7,                                              // Lc = AID length
          (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, // AID = F0010203040507
          0x00,                                           // Le = max
}, card);

Can it be that this particular reader is flawed?

Yes, this could be the case though I rather doubt that. Note that there are many different versions of the ACR122U firmware and most of them seem to be flawed by design. Particularly the fact that some versions of the reader perform automatic tag enumeration and some don't, and that the available API changed drastically across different versions of that reader make it difficult to program for that device.

UPDATE: Some more observations ...

  • The response to the InListPassiveTarget command does not contain ATS data (after the UID field). Maybe your reader does not perform automatic RATS during tag selection. This can be enabled using the SetParameters command (before InListPassiveTarget):

    execute(channel, new byte[] {
        (byte)0xFF, 0x00, 0x00, 0x00,  // direct PN532 command
        3,                 // Lc = command length
        (byte)0xD4, 0x12,  // InDataExchange
        (1<<4),            // fAutomaticRATS = 1
    }, card);
    

    You could also try to manually send a RATS command using InCommunicateThru:

    execute(channel, new byte[] {
        (byte)0xFF, 0x00, 0x00, 0x00,  // direct PN532 command
        4,                 // Lc = command length
        (byte)0xD4, 0x42,  // InCommunicateThru
        (byte)0xE0, 0x80,  // RATS (FSD = 256, CID = 0)
    }, card);
    

    After that you could try to communicate with the card using InCommunicateThru and raw ISO/IEC 14443-4 blocks:

    execute(channel, new byte[] {
        (byte)0xFF, 0x00, 0x00, 0x00,  // direct PN532 command
        16,                // Lc = command length
        (byte)0xD4, 0x42,  // InCommunicateThru
        0x02,              // PCB (I-block, change to 0x03 for the next block)
        0x00, (byte)0xA4, 0x04, 0x00,                     // APDU: SELECT (by AID)
          7,                                              // Lc = AID length
          (byte)0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x07, // AID = F0010203040507
          0x00,                                           // Le = max
    }, card);
    
  • The ATQA 0803 looks rather odd. Especially the 0x03 in the bit frame anticollision field suggests that there is more than one target in the field (since a standards compliant tag would only set a single bit in the bit frame anticollision field). Note that this is not true. The ATQA in the response to InListPassiveTarget is transmitted little endian. Consequently, the bit frame anticollision value is 0x08 (= valid/compliant) and the value in the proprietary field is 0x03.

  • It's, indeed, strange that your reader does not respond to certain PN532 commands (particularly since the firmware version 32010607 looks fine). I've tested some of the commands that fail for you with another ACR122U and they completed successfully ...

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!