NFC - 帮助在RC522和RC之间交换数据。 Android HCE

时间:2016-06-20 11:44:05

标签: android arduino nfc apdu hce

我将解释我的项目:

我的RC522和我的Arduino UNO连接了一扇门。

我现在可以用MIFARE经典打开门。

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

但问题是:

我不知道如何使用RC522向我的Arduino发送APDU命令。

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

我的测试代码:

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不兼容......

1 个答案:

答案 0 :(得分:0)

这是一个(评论很好)的开源项目。只需查看源代码,例如,如果您使用MIFARE_Read

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

您可以看到调用函数/** * 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或{{1}}个函数。

<强>更新

您为参数设置了0个值:&#34; backData&#34;,&#34; backLen&#34;和&#34; validBits&#34;。 validBits可以为null。 backData和backLen必须定义为byte。

<强> UPDATE2

如果查看库MFRC522.cpp它支持ISO / IEC 14443-3(类型A),不支持ISO / IEC 14443-4(类型B)。

如果您查看support protocols

  

具体来说,Android 4.4支持模拟基于的卡   NFC论坛ISO-DEP规范(基于ISO / IEC 14443-4)和   处理应用程序协议数据单元(APDU)中定义的   ISO / IEC 7816-4规范。 Android要求仅模拟ISO-DEP   在Nfc-A(ISO / IEC 14443-3 A型)技术之上。支持   Nfc-B(ISO / IEC 14443-4 B类)技术是可选的。分层   所有这些规格如图3所示

在这篇文章中:Android HCE documentation

  

查看现场的设备,有些设备使用A型进行HCE和   有些人似乎使用B型。所以它基本上就是设备制造商   决定是使用A型还是B型。 Android API无法提供   让应用程序开发人员对此产生影响。

因此,如果您的设备模拟ISO / IEC 14443-3(A类)或ISO / IEC 14443-4(B类),则必须检查其他Android NFC设备。您可以在其他Android设备上使用HCE support for ISO/IEC 14443-3 Type B?进行检查。