代码之家  ›  专栏  ›  技术社区  ›  evildead

Android HCE与Arduino和Galaxy S3 cyanogenmod 11

  •  0
  • evildead  · 技术社区  · 12 年前

    我目前正在尝试让我的arduino uno设备运行HCE,该设备安装了一个看起来不错的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声音播放,这通常表明至少发生了什么。 所以我的问题是,是否有人尝试使用cyanogenmod 10和HCE支持的Galaxy S3。我真的没有任何想法,我三次检查了我的代码,在SO上读了很多问题,但所有人都至少从NFC屏蔽收到了一些东西。

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

    2 回复  |  直到 12 年前
        1
  •  3
  •   arghtype Castaldi    12 年前

    Android 4.4 HCE不在带有NXP芯片的三星Galaxy S3上运行。 我已经测试了Galaxy Nexus、Nexus 7(2012)和Galaxy S3。所有这三个设备都无法在Kitkat上运行HCE。

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

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

        2
  •  2
  •   Michael Roland    12 年前

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

    CyanogenMod 10.*具有不同的HCE API。看见 Nikolay's blog 对于 example 如何使用该API。

    基本上,为了让Arduino程序工作,您必须基于 IsoPcdA 技术

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

    apdu[] = "Hello from Arduino";
    

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