【问题标题】:NFC - Help to exchange data between RC522 & Android HCENFC - 帮助在 RC522 和 Android HCE 之间交换数据
【发布时间】:2016-06-20 11:44:05
【问题描述】:

我将解释我的项目:

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

我目前可以用 MIFARE 经典版开门。

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

但问题是:

我不知道如何使用我的 Arduino 使用 RC522 发送 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不兼容......

【问题讨论】:

  • 请通过向我们展示您最好的诚意尝试并就您的困惑提出更具体的问题来尝试改进此问题。
  • 我的问题不能再具体了:如何使用 RC522 在 Arduino 上发送 APDU 命令?谢谢帮助! :))
  • 我不懂C++语言,给validBits参数传0或者null一样吗?它是这样使用的: byte txLastBits = validBits ? *validBits : 0;
  • @LaurentY 我尝试了这两种方法,但没有任何效果......你确定用 RC522 发送 APDU 是可能/兼容的吗?

标签: android arduino nfc apdu hce


【解决方案1】:

这是一个(好评如潮的)开源项目。只要看看源代码,例如如果你使用MFRC522.cppMIFARE_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_TransceiveDataPCD_CommunicateWithPICC 函数。

更新

您为参数设置了 0 个值:“backData”、“backLen”和“validBits”。 validBits 可以为空。 backData 和 backLen 必须定义为字节。

更新2

如果您查看库 support protocols 它支持 ISO/IEC 14443-3(A 型)而不支持 ISO/IEC 14443-4(B 型)。

如果你看看Android HCE documentation

具体来说,Android 4.4 支持模拟基于 NFC-Forum 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 Type B)技术是可选的。的层次感 所有这些规格如图3所示

在这篇文章中:HCE support for ISO/IEC 14443-3 Type B?

查看现场设备,一些设备使用 A 型用于 HCE 和 有些似乎使用 B 型。所以基本上是设备制造商 决定是使用 A 型还是 B 型。 Android API 不提供任何手段 让应用开发者对此施加影响。

因此,如果您的设备模拟 ISO/IEC 14443-3(A 类)或 ISO/IEC 14443-4(B 类),您必须检查其他 Android NFC 设备。您可以在其他安卓设备上使用NfcTagInfo application 来检查。

【讨论】:

  • 感谢Laurent的帮助,看看我上面的回答就行了。
  • 谢谢,但是定义它们有什么好处呢?毕竟我不需要它们..我再次更新我的测试代码,但它再次不起作用
  • 你交出我的案子?
  • 最后一个答案请@LaurentY
  • 这是一个很好的信息,我没有。不幸的是,我的手机是 ISO 14443 Type A。所以,它似乎可以工作? @LaurentY
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多