本文介绍了NFC-帮助在RC522& amp;之间进行数据交换Android HCE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将解释我的项目:

我在Arduino UNO上连接了RC522和一扇门.

I have my RC522 and a door connected on my Arduino UNO.

我现在可以用MIFARE classic打开门.

I can currently open the door with a MIFARE classic.

但是现在我想用我的Android智能手机打开它,这就是为什么我开发一个HCE小程序以接受具有所选AID的良好APDU的原因,然后我的手机将传输数据以打开门.

But now I want to open it with my Android smartphone, this is why I develop a HCE applet to accept the good APDU with the selected AID, then my phone will transfer the data in order to open the door.

但是问题是:

我不知道如何使用RC522通过Arduino发送APDU命令.

I don't know how to send an APDU command with my Arduino using the RC522.

当前,对于我的MIFARE卡,我使用 https://github.com/miguelbalboa/rfid库.

Currently, for my MIFARE Cards, I use the https://github.com/miguelbalboa/rfid library.

我的测试代码:

byte selectApdu[] = {
  0x00, /* CLA */
  0xA4, /* INS */
  0x04, /* P1  */
  0x00, /* P2  */
  0x05, /* Length of AID  */
  0xF2, 0x22, 0x22, 0x22, 0x22,
};
byte * backData = (byte *)malloc(16*sizeof(byte));
byte * dataLen = (byte *)16;

status = mfrc522.PCD_TransceiveData(selectApdu,10,backData,dataLen,NULL,0,false);
if ( status != MFRC522::STATUS_OK) {
    Serial.print(F("PCD_TransceiveData() failed: "));
    Serial.println(mfrc522.GetStatusCodeName(status));
    return;
}
else
{
  Serial.println(F("PICC_TransceiveData() success "));
}

从不,它不起作用(通信超时"),我慢慢地认为RC522不兼容...

Neverless, it doesn't work ( "Timeout in communication"), and I slowly need to think that the RC522 is not compatible...

推荐答案

这是一个(受到充分评论的)开源项目.仅查看源代码,例如,如果您使用 MFRC522.cpp

It's an (well commented) open-source project. Just have a look to source code, for instance If you use MIFARE_Read function of MFRC522.cpp

MFRC522::StatusCode MFRC522::MIFARE_Read(   byte blockAddr,     ///< MIFARE Classic: The block (0-0xff) number. MIFARE Ultralight: The first page to return data from.
                                            byte *buffer,       ///< The buffer to store the data in
                                            byte *bufferSize    ///< Buffer size, at least 18 bytes. Also number of bytes returned if STATUS_OK.
                                        ) {
    MFRC522::StatusCode result;

    // Sanity check
    if (buffer == NULL || *bufferSize < 18) {
        return STATUS_NO_ROOM;
    }

    // Build command buffer
    buffer[0] = PICC_CMD_MF_READ;
    buffer[1] = blockAddr;
    // Calculate CRC_A
    result = PCD_CalculateCRC(buffer, 2, &buffer[2]);
    if (result != STATUS_OK) {
        return result;
    }

    // Transmit the buffer and receive the response, validate CRC_A.
    return PCD_TransceiveData(buffer, 4, buffer, bufferSize, NULL, 0, true);
} // End MIFARE_Read()

您可能会看到函数 PCD_TransceiveData 被调用并检查此函数的源代码:

You could see function PCD_TransceiveData is called and check source of this function:

/**
 * Executes the Transceive command.
 * CRC validation can only be done if backData and backLen are specified.
 *
 * @return STATUS_OK on success, STATUS_??? otherwise.
 */
MFRC522::StatusCode MFRC522::PCD_TransceiveData(    byte *sendData,     ///< Pointer to the data to transfer to the FIFO.
                                                    byte sendLen,       ///< Number of bytes to transfer to the FIFO.
                                                    byte *backData,     ///< NULL or pointer to buffer if data should be read back after executing the command.
                                                    byte *backLen,      ///< In: Max number of bytes to write to *backData. Out: The number of bytes returned.
                                                    byte *validBits,    ///< In/Out: The number of valid bits in the last byte. 0 for 8 valid bits. Default NULL.
                                                    byte rxAlign,       ///< In: Defines the bit position in backData[0] for the first bit received. Default 0.
                                                    bool checkCRC       ///< In: True => The last two bytes of the response is assumed to be a CRC_A that must be validated.
                                 ) {
    byte waitIRq = 0x30;        // RxIRq and IdleIRq
    return PCD_CommunicateWithPICC(PCD_Transceive, waitIRq, sendData, sendLen, backData, backLen, validBits, rxAlign, checkCRC);
} // End PCD_TransceiveData()

您可以调用 PCD_TransceiveData PCD_CommunicateWithPICC 函数.

更新

您为参数"backData","backLen"和"validBits"输入了0个值.validBits可以为null.backData和backLen必须定义为字节.

You put 0 values for parameters: "backData", "backLen", and "validBits".validBits could be null.backData and backLen must be defined as byte.

UPDATE2

如果您看一下库支持协议它支持ISO/IEC 14443-3(A型),不支援ISO/IEC 14443-4(B型).

If you have a look at library support protocols It supports ISO/IEC 14443-3 (type A) and not supports ISO/IEC 14443-4 (type B).

如果您查看 Android HCE文档:

在此文章中: HCE对ISO/IEC 14443的支持-3 B型?

因此,如果您的设备模拟ISO/IEC 14443-3(A型)或ISO/IEC 14443-4(B型),则必须与另一台Android NFC设备进行检查.您可以在 NfcTagInfo应用程序上使用其他Android设备对此进行检查.

So you have to check with another Android NFC device if your device emulate ISO/IEC 14443-3 (Type A) or ISO/IEC 14443-4 (Type B). You could use NfcTagInfo application on other android device to check this.

这篇关于NFC-帮助在RC522&amp; amp;之间进行数据交换Android HCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 16:57