使用 HCE 无法使用 NFC 模块在 Android 手机和 Arduino 之间交换数据 [英] Can't exchange data between Android phone and Arduino with NFC module, using HCE

查看:19
本文介绍了使用 HCE 无法使用 NFC 模块在 Android 手机和 Arduino 之间交换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

拜托,我需要任何帮助来解决我的问题.我无法使用 HCE 在 Android (4.4.2) 手机和带有 NFC 模块的 Arduino 之间正常交换数据.
我以 Android 示例为例,稍作更改以仅返回 IMEI 号码.

public byte[] processCommandApdu(byte[] commandApdu, Bundle extras) {如果(Arrays.equals(SELECT_APDU,commandApdu)){字符串数据 = ((TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE)).getDeviceId();返回 ConcatArrays(data.getBytes(), SELECT_OK_SW);} 别的 {返回 UNKNOWN_CMD_SW;}}

在 Arduino 方面,我的代码是:

void loop(){Serial.println("正在等待一张 ISO14443A 卡");uint8_t 成功;成功 = nfc.inListPassiveTarget();如果(成功){Serial.println("找到东西了!");uint8_t 响应长度 = 32;uint8_t 响应[32];uint8_t selectApdu[] = {0x00,/* CLA */0xA4,/* INS */0x04,/* P1 */0x00,/* P2 */0x05,/* AID 的长度 */0xF2, 0x22, 0x022, 0x22, 0x22,/* AID */0x00/* 乐 */};成功 = nfc.inDataExchange(selectApdu, sizeof(selectApdu), response, &responseLength);Serial.print("EX_RES:");Serial.println(成功);如果(成功){Serial.print("响应长度:");Serial.println(responseLength);for(int i=0; i

最初,我收到发送 SELECT AID 失败",所以我试图找出原因.所以我更新了 PN532.cpp 文件中 inDataExchange 的代码.所以现在看起来像这样:

//最初函数返回 booluint8_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)) {返回2;//最初是假的}int16_t status = HAL(readResponse)(response, *responseLength, 1000);如果(状态<0){返回3;//最初是假的}如果((响应[0]& 0x3f)!= 0){DMSG("状态码表示错误\n");返回 4;//最初是假的}uint8_t 长度 = 状态;长度 -= 1;如果(长度> *响应长度){长度 = *响应长度;//静默截断...}for (uint8_t i = 0; i < length; i++) {响应[i] = 响应[i + 1];}*响应长度=长度;返回 5;//最初是真的}

现在,我收到这样的日志输出:

<前>等待一张 ISO14443A 卡发现了一些东西!EX_RES:5响应长度 1:1835, 51, 53, 55, 50, 52, 54, 48, 53, 52, 49, 57, 50, 55, 49, 57, 144, 0,========================等待一张 ISO14443A 卡发现了一些东西!EX_RES:4响应长度 1:3211, 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,========================等待一张 ISO14443A 卡发现了一些东西!EX_RES:4响应长度 1:321, 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,========================等待一张 ISO14443A 卡发现了一些东西!EX_RES:4响应长度 1:3211, 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响应长度:1811, 219, 13, 51, 8, 187, 181, 0, 2, 54, 1, 1, 2, 140, 0, 7, 72, 1,EX_RES:4响应长度:181, 72, 1, 2, 37, 0, 4, 228, 4, 160, 4, 168, 7, 236, 2, 138, 50, 0,

有什么问题吗?也许有人遇到过这个问题?也许图书馆有问题,或者我做错了什么?
我正在使用:

解决方案

我终于让它工作了.首先想说这个问题的出现是因为我Android编程能力不强(我完全是新手).

  1. responseLength 必须手动设置,理想情况下,必须等于响应缓冲区大小(感谢 arduino 论坛的 Traveller99)

  2. 如果屏幕关闭,HCE 将无法工作(这是有趣的部分:))

    <块引用>

    当前的 Android 实现会在设备屏幕关闭时完全关闭 NFC 控制器和应用处理器.因此,当屏幕关闭时,HCE 服务将无法工作.但是,HCE 服务可以在锁定屏幕中运行.

感谢 Michael 的帮助!

Please, I need any help to solve my problem. I can't exchange data normally between Android (4.4.2) phone and Arduino with NFC module, using HCE.
I took example from Android samples, and slightly changed to return just IMEI number.

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;
    }
}

On Arduino side, my code is:

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);
}

Initially, I was receiving "Failed sending SELECT AID", so I tried to figure out why. So i updated code of inDataExchange in PN532.cpp file. So now it looks like this:

// 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
}

And now, I'm receiving such log output:

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, 
========================

I understand that this result is incorrect, and value is unchanged buffer (because of error), except first digit, which is changing from time to time.
Sometimes I receive such strange log:

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, 

What's the problem? Maybe someone encountered this problem? Maybe there is some problem with library, or I'm doing something wrong?
I'm using:

解决方案

I finally got it working. First of all want to say that the problem arose because of my incompetence in Android programming (I'm totally newbie).

  1. responseLength must be set manually, and ideally, must be equal to response buffer size (thanks to Traveller99 from arduino forum)

  2. HCE will not work if the screen is off (here is the fun part :) )

    Current Android implementations turn the NFC controller and the application processor off completely when the screen of the device is turned off. HCE services will therefore not work when the screen is off. HCE services can function from the lock-screen however.

Thanks to Michael for trying to help!

这篇关于使用 HCE 无法使用 NFC 模块在 Android 手机和 Arduino 之间交换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆