【问题标题】:Android HCE with Arduino and Galaxy S3 cyanogenmod 11带有 Arduino 和 Galaxy S3 cyanogenmod 11 的 Android HCE
【发布时间】:2014-03-27 15:54:18
【问题描述】:

我目前正在尝试使用我的 arduino uno 设备运行 HCE,该设备安装有 Seeed NFC Shield V 2.0

我正在使用来自https://github.com/Seeed-Studio/PN532/blob/master/PN532/examples/android_hce/android_hce.ino的示例代码

#include <SPI.h>
#include <PN532_SPI.h>
#include <PN532Interface.h>
#include <PN532.h>

PN532_SPI pn532spi(SPI, 10);
PN532 nfc(pn532spi);


void setup()
{
    Serial.begin(115200);
    Serial.println("-------Peer to Peer HCE--------");

    nfc.begin();

    uint32_t versiondata = nfc.getFirmwareVersion();
    if (! versiondata) {
      Serial.print("Didn't find PN53x board");
      while (1); // halt
    }

    // Got ok data, print it out!
    Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
    Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
    Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

    // Set the max number of retry attempts to read from a card
    // This prevents us from waiting forever for a card, which is
    // the default behaviour of the PN532.
    //nfc.setPassiveActivationRetries(0xFF);

    // configure board to read RFID tags
    nfc.SAMConfig();
}

void loop()
{
  bool success;

  uint8_t responseLength = 32;

  Serial.println("Waiting for an ISO14443A card");

  // set shield to inListPassiveTarget
  success = nfc.inListPassiveTarget();

  if(success) {

     Serial.println("Found something!");

    uint8_t selectApdu[] = { 0x00, /* CLA */
                              0xA4, /* INS */
                              0x04, /* P1  */
                              0x00, /* P2  */
                              0x07, /* Length of AID  */
                              0xF0, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, /* AID defined on Android App */
                              0x00  /* Le  */ };

    uint8_t response[32];

    success = nfc.inDataExchange(selectApdu, sizeof(selectApdu), response, &responseLength);

    if(success) {

      Serial.print("responseLength: "); Serial.println(responseLength);

      nfc.PrintHexChar(response, responseLength);

      do {
        uint8_t apdu[] = "Hello from Arduino";
        uint8_t back[32];
        uint8_t length = 32;

        success = nfc.inDataExchange(apdu, sizeof(apdu), back, &length);

        if(success) {

          Serial.print("responseLength: "); Serial.println(length);

          nfc.PrintHexChar(back, length);
        }
        else {

          Serial.println("Broken connection?");
        }
      }
      while(success);
    }
    else {

      Serial.println("Failed sending SELECT AID");
    }
  }
  else {

    Serial.println("Didn't find anything!");
  }

  delay(1000);
}

void printResponse(uint8_t *response, uint8_t responseLength) {

   String respBuffer;

    for (int i = 0; i < responseLength; i++) {

      if (response[i] < 0x10)
        respBuffer = respBuffer + "0"; //Adds leading zeros if hex value is smaller than 0x10

      respBuffer = respBuffer + String(response[i], HEX) + " ";
    }

    Serial.print("response: "); Serial.println(respBuffer);
}

void setupNFC() {

  nfc.begin();

  uint32_t versiondata = nfc.getFirmwareVersion();
  if (! versiondata) {
    Serial.print("Didn't find PN53x board");
    while (1); // halt
  }

  // Got ok data, print it out!
  Serial.print("Found chip PN5"); Serial.println((versiondata>>24) & 0xFF, HEX);
  Serial.print("Firmware ver. "); Serial.print((versiondata>>16) & 0xFF, DEC);
  Serial.print('.'); Serial.println((versiondata>>8) & 0xFF, DEC);

  // configure board to read RFID tags
  nfc.SAMConfig();
}

我的android代码取自HCE开发指南,我也尝试了https://github.com/grundid/host-card-emulation-sample的代码形式

我的问题是,arduino 代码甚至无法识别标签。所以我得到的只是:

Waiting for an ISO14443A card
Didn't find anything!
Waiting for an ISO14443A card
Didn't find anything!
Waiting for an ISO14443A card 

事实上,在Android端,我没有收到任何数据包,也没有播放花哨的NFC声音,这通常表明至少发生了一些事情。 所以我的问题是,是否有人尝试将 Galaxy S3 与 cyanogenmod 10 以及 HCE 支持一起使用。我真的没有任何想法,我三次检查了我的代码,在这里阅读了很多关于 SO 的问题,但所有人都至少从 NFC Shield 收到了一些东西。

我知道常见的 NFC 模式可以正常工作,因为我有一个在“正常”模式下使用 NFC 的应用程序。当我在我的 arduino 上运行这个 NFC 协议时,所有应用程序都会收到一个数据包,(尽管它们无法正确路由它们,因为它们不是 adpu 数据包)

【问题讨论】:

  • 我只是想在这里提供更新。它是具有 NXP NFC 芯片组的设备。我用 Nexus 4(它有一个 Broadcom NFC 芯片组)尝试了相同的代码,一切都完美无缺。

标签: android arduino nfc cyanogenmod hce


【解决方案1】:

Android 4.4 HCE 无法在配备 NXP 芯片的三星 Galaxy S3 上运行。 我测试过 Galaxy Nexus、Nexus 7(2012) 和 Galaxy S3。所有这 3 款设备都无法在 Kitkat 上运行 HCE。

您可以检查设备是否支持 HCE:

布尔 isHceSupported = getPackageManager().hasSystemFeature("android.hardware.nfc.hce");

【讨论】:

  • 您可以使用NFC Check by Tapkey App 查看手机支持的NFC功能详情。这仅检查官方 HCE API,而不检查 Cyanogen API。
  • 嗨 Makus,支持 nfc,但不支持 HCE。原因是android路由消息到内置SE,和SE保持回复LLC长度不匹配。结果,阅读器将不断发出哔哔声。对于那些启用 HCE 的设备,阅读器只会发出一次哔声,并且设备将等待来自阅读器的 SELECT AID 请求,而不是报告和 LLC 错误。
【解决方案2】:

CyanogenMod 10.* 不支持 Android HCE(由 Android 4.4 HCE API 提供)。该 API 仅从 CyanogenMod 11 开始可用,即便如此,我也不确定 Android HCE API 的 CyanogenMod 实现是否适用于配备 NXP 芯片组的设备(例如 Galaxy S3)。

CyanogenMod 10.* 具有不同的 HCE API。有关如何使用该 API 的信息,请参阅 Nikolay's blog 以获取 example

基本上,要使您的 Arduino 程序正常工作,您必须基于 IsoPcdA 技术实施 HCE。

另请注意,如果您从 CyanogenMod HCE 切换到 Android HCE,则必须使用符合 ISO 7816-4 的 APDU。因此,像您在上面的代码中使用的“APDU”,

apdu[] = "Hello from Arduino";

行不通。但是,CyanogenMod HCE 不需要在 ISO 14443-4 传输协议之上使用 ISO 7816-4 APDU,因此会很乐意接受此字符串而不是 APDU。

【讨论】:

  • 嗨,迈克尔。感谢您的回答。当我提到 cyanogenmod 11 时,我改变了主题。是我的错字。所以我读到的是,即使使用 cm 11 nxp 芯片组也不支持对吗?在我坐火车回家的路上,我也在网上发现了这个。所以我想我必须使用另一部手机。
  • 您仍然可以尝试上述方法(CyanogenMod HCE)。我会假设这种方法在 CM 11 中仍然可用,尽管我没有检查过。
猜你喜欢
  • 1970-01-01
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-25
  • 2013-01-24
  • 1970-01-01
相关资源
最近更新 更多