NFC - 帮助在 RC522 & 之间交换数据安卓 HCE [英] NFC - Help to exchange data between RC522 & Android HCE
问题描述
我会解释我的项目:
我的 RC522 和一扇门连接到了我的 Arduino UNO.
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...
推荐答案
这是一个(评论良好的)开源项目.只需查看源代码,例如如果您使用 MIFARE_Read 函数"nofollow noreferrer">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
函数.
You could called PCD_TransceiveData
or PCD_CommunicateWithPICC
functions.
更新
您为参数设置了 0 个值:backData"、backLen"和validBits".validBits 可以为空.backData 和 backLen 必须定义为字节.
You put 0 values for parameters: "backData", "backLen", and "validBits". validBits could be null. backData and backLen must be defined as byte.
更新2
如果您查看库 支持协议 它支持 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 文档> :
具体来说,Android 4.4 支持模拟基于NFC 论坛 ISO-DEP 规范(基于 ISO/IEC 14443-4)和处理应用协议数据单元 (APDU),如定义在ISO/IEC 7816-4 规范.Android 要求仅模拟 ISO-DEP在 Nfc-A(ISO/IEC 14443-3 Type A)技术之上.支持Nfc-B(ISO/IEC 14443-4 Type B)技术是可选的.的层次感所有这些规格如图 3 所示
Specifically, Android 4.4 supports emulating cards that are based on the NFC-Forum ISO-DEP specification (based on ISO/IEC 14443-4) and process Application Protocol Data Units (APDUs) as defined in the ISO/IEC 7816-4 specification. Android mandates emulating ISO-DEP only on top of the Nfc-A (ISO/IEC 14443-3 Type A) technology. Support for Nfc-B (ISO/IEC 14443-4 Type B) technology is optional. The layering of all these specifications is shown in the figure 3
在这篇文章中:HCE 支持 ISO/IEC 14443-3 B 型?
看看现场的设备,一些设备使用 A 型进行 HCE 和有些似乎使用 B 型.所以基本上是设备制造商决定使用 A 型还是 B 型.Android API 没有提供任何手段以便应用开发者影响这一点.
Looking at devices in the field, some devices use Type A for HCE and some seem to use Type B. So it's basically the device manufacturer who decides if Type A or Type B is used. The Android API provides no means for the app developer to influence this.
因此,如果您的设备模拟 ISO/IEC 14443-3(A 型)或 ISO/IEC 14443-4(B 型),则必须与另一台 Android NFC 设备进行核对.您可以使用 NfcTagInfo 应用程序其他安卓设备来检查这个.
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 & 之间交换数据安卓 HCE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!