首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >基于主机的卡仿真或HCE

基于主机的卡仿真或HCE
EN

Stack Overflow用户
提问于 2015-06-28 16:54:16
回答 1查看 714关注 0票数 0

我想写一个程序,可以存储一个唯一的ID,然后分享它作为NFC标签。总而言之,我想写程序,可以使用移动设备,而不是智能卡。

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

我不知道code below是做什么的,它们的功能是什么

代码语言:javascript
复制
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);
    }
  }

}
EN

回答 1

Stack Overflow用户

发布于 2015-06-29 19:35:26

我建议你多读一些关于NFC和Android的ForegroundDispatcher的知识。作为开始,我将在baseline中描述这段代码正在做什么。

代码语言:javascript
复制
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方法。在这里您可以读取(和写入)标记。

代码语言:javascript
复制
@Override
public void onNewIntent(Intent intent)
{
    // Get the tag from the given intent
    Tag t = (Tag)intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);   
}       

祝好运!

顺便说一句:这不是HCE!上面的代码是一个读取器/写入器模式的示例,您可以在该模式下读取(或写入)标签。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31097799

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档