을 사용하여 Android 폰과 Arduino간에 NFC 모듈로 데이터를 교환 할 수 없습니다. 제 문제를 해결하는 데 도움이 필요합니다. HCE를 사용하여 Android (4.4.2) 전화와 Arduino가 NFC 모듈간에 데이터를 교환 할 수 없습니다.
Android 샘플에서 예제를 취하고 IMEI 번호 만 반환하도록 약간 변경되었습니다.HCE
public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {
if (Arrays.equals(SELECT_APDU, commandApdu)) {
String data = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();
return ConcatArrays(data.getBytes(), SELECT_OK_SW);
} else {
return UNKNOWN_CMD_SW;
}
}
아두 이노 측면에서, 내 코드는 다음과 같습니다 처음
void loop(){
Serial.println("Waiting for an ISO14443A card");
uint8_t success;
success = nfc.inListPassiveTarget();
if(success){
Serial.println("Found something!");
uint8_t responseLength = 32;
uint8_t response[32];
uint8_t selectApdu[] = {
0x00, /* CLA */
0xA4, /* INS */
0x04, /* P1 */
0x00, /* P2 */
0x05, /* Length of AID */
0xF2, 0x22, 0x022, 0x22, 0x22, /* AID */
0x00 /* Le */};
success = nfc.inDataExchange(selectApdu, sizeof(selectApdu), response, &responseLength);
Serial.print("EX_RES:");
Serial.println(success);
if(success) {
Serial.print("responseLength: ");
Serial.println(responseLength);
for(int i=0; i<responseLength; i++){
Serial.print(response[i]);
Serial.print(", ");
}
Serial.println();
Serial.println("========================");
}
else {
Serial.println("Failed sending SELECT AID");
}
}
else {
Serial.println("Didn't find anything!");
}
delay(1000);
}
, 나는 수신했다 "SELECT AID를 전송 실패"그래서 이유를 알아 내기 위해 노력했다. 그래서 inDataExchange 코드를 PN532.cpp 파일에 업데이트했습니다.
// initially function was returning bool
uint8_t PN532::inDataExchange(uint8_t *send, uint8_t sendLength, uint8_t *response, uint8_t *responseLength){
uint8_t i;
pn532_packetbuffer[0] = 0x40; // PN532_COMMAND_INDATAEXCHANGE;
pn532_packetbuffer[1] = inListedTag;
if (HAL(writeCommand)(pn532_packetbuffer, 2, send, sendLength)) {
return 2; // initially was false
}
int16_t status = HAL(readResponse)(response, *responseLength, 1000);
if (status < 0) {
return 3; // initially was false
}
if ((response[0] & 0x3f) != 0) {
DMSG("Status code indicates an error\n");
return 4; // initially was false
}
uint8_t length = status;
length -= 1;
if (length > *responseLength) {
length = *responseLength; // silent truncation...
}
for (uint8_t i = 0; i < length; i++) {
response[i] = response[i + 1];
}
*responseLength = length;
return 5; // initially was true
}
을 그리고 지금, 나는 그런 로그 출력을 수신하고 있습니다 : 그래서 지금은 다음과 같다 나는이 결과가 잘못이며, 값이 (오류로 인해) 변경되지 않은 버퍼 것을 이해
Waiting for an ISO14443A card Found something! EX_RES:5 responseLength1: 18 35, 51, 53, 55, 50, 52, 54, 48, 53, 52, 49, 57, 50, 55, 49, 57, 144, 0, ======================== Waiting for an ISO14443A card Found something! EX_RES:4 responseLength1: 32 11, 51, 53, 55, 50, 52, 54, 48, 53, 52, 49, 57, 50, 55, 49, 57, 144, 0, 0, 36, 0, 0, 3, 5, 17, 2, 117, 0, 194, 1, 6, 7, ======================== Waiting for an ISO14443A card Found something! EX_RES:4 responseLength1: 32 1, 51, 53, 55, 50, 52, 54, 48, 53, 52, 49, 57, 50, 55, 49, 57, 144, 0, 0, 36, 0, 0, 3, 5, 17, 2, 117, 0, 194, 1, 6, 7, ======================== Waiting for an ISO14443A card Found something! EX_RES:4 responseLength1: 32 11, 51, 53, 55, 50, 52, 54, 48, 53, 52, 49, 57, 50, 55, 49, 57, 144, 0, 0, 36, 0, 0, 3, 5, 17, 2, 117, 0, 254, 0, 0, 0, ========================
을 , 수시로 변경되는 첫 번째 자릿수를 제외하고
EX_RES:4 responseLength: 18 11, 219, 13, 51, 8, 187, 181, 0, 2, 54, 1, 1, 2, 140, 0, 7, 72, 1, EX_RES:4 responseLength: 18 1, 72, 1, 2, 37, 0, 4, 228, 4, 160, 4, 168, 7, 236, 2, 138, 50, 0,
문제가 무엇 :
때때로 나는 그런 이상한 로그를받을? 어쩌면 누군가가이 문제를 겪었을 것입니까? 어쩌면 도서관에 문제가 있거나 뭔가 잘못하고있는 걸까요?
내가 사용 :
- Elechouse NFC 모듈 2.0
- 아두 이노 UNO SMD R3
- NFC 라이브러리를 https://github.com/elechouse/PN532
코드와 로그 출력이 일치하지 않습니다 (예 : 게시 한 코드에 EX_RES2 및 응답 길이 2 사용 안함). 일치하는 코드/로그 출력을 포함하도록 질문을 업데이트하십시오. –
나는 여전히 로그 출력이 위 코드에서 나온다고 생각하지 않습니다. 특히 3 단계에서'response' 버퍼의 첫번째 바이트는 마술처럼 다시 '35'로 바뀝니다.실제로 inDataExchange가 에러 코드 3과 함께 반환된다면 그 바이트는'11' (어레이가 이전 라운드와 동일한 메모리에 할당 된 경우)이거나 전체 응답 버퍼에 임의의 가비지가 포함될 것입니다 (배열 이전과 다른 메모리 영역에 할당 됨). –
당신이 실제로하고있는 것을 보지 않고는 말하기가 다소 어렵습니다 (실제 코드/실제 출력). –