【问题标题】:Host-based Card Emulation or HCE基于主机的卡仿真或 HCE
【发布时间】:2015-06-28 08:54:16
【问题描述】:

我想编写一个可以存储唯一 ID 的程序,然后将其作为 NFC 标签共享。我完全想编写可以使用移动设备而不是智能卡的程序。

我在许多网站和这里都读到过它,但我不明白如何将 ID 推送到我的应用程序以及如何通过 NFC 读取器设备共享此 ID

我不知道code below 是做什么的,他们的特点是什么

public class MainActivity extends Activity {

  public void onCreate(Bundle savedInstanceState) {
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    filters = new IntentFilter[] { new IntentFilter(
                NfcAdapter.ACTION_TECH_DISCOVERED) };
    techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
  }

  public void onResume() {
    super.onResume();
    if (adapter != null) {
      adapter.enableForegroundDispatch(this, pendingIntent, filters,
                    techLists);
    }
  }

  public void onPause() {
    super.onPause();
    if (adapter != null) {
      adapter.disableForegroundDispatch(this);
    }
  }

}

【问题讨论】:

    标签: android nfc hce


    【解决方案1】:

    我建议您阅读有关 NFC 和 Android 的 ForegroundDispatcher 的更多信息。 首先,我将在基线中描述这段代码在做什么。

    public class MainActivity extends Activity {
    
      public void onCreate(Bundle savedInstanceState) {
    
        //Here you define an intent that will be raised when a tag is received.
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                    getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    
        //These are the tag conditions to throw the intent of above.
        //ACTION_TECH_DISCOVERED means the tag need to be of the type defined in the techlist.      
        filters = new IntentFilter[] { new IntentFilter(
                    NfcAdapter.ACTION_TECH_DISCOVERED) };
    
        //As the name already says, this is your techlist
        techLists = new String[][] { { "android.nfc.tech.IsoPcdA" } };
    
    
      }
    
      public void onResume() {
        super.onResume();
        if (adapter != null) {
    
          //This enabled the foreground dispatching
          //It means that when a tag is detected of the type `IsoPcdA`. The `pendingIntent` will be given to the current activity
          adapter.enableForegroundDispatch(this, pendingIntent, filters,
                        techLists);
        }
      }
    
      public void onPause() {
        super.onPause();
        if (adapter != null) {
    
          //This is to disable the foreground dispatching. You don't want to send this intents to this activity when it isn't active
          adapter.disableForegroundDispatch(this);
        }
      }
    
    }
    

    由于这个ForegroundDispatching 抛出了一个新的intent,你需要重写onNewIntent 方法。 在这里您可以读取(和写入)标签。

    @Override
    public void onNewIntent(Intent intent)
    {
        // Get the tag from the given intent
        Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);   
    }       
    

    祝你好运!

    顺便说一句:这不是 HCE!上面的代码是一个读/写模式的例子,你可以读(或写)标签。

    【讨论】:

    • 这个应用程序是否也必须包含 AID 才能运行?
    • 您的 HCE 应用必须在清单中包含 AID。但是您应该在读写器应用程序中使用该 AID 发送一个 SELECT APDU 命令,该命令将触发正确的应用程序(您的 HCE 应用程序)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 2013-11-14
    • 2015-09-19
    • 1970-01-01
    • 2014-04-27
    • 2013-12-09
    相关资源
    最近更新 更多