简体   繁体   English

只需阅读NFC标签即可

[英]Just read an NFC tag

I am developing an app that will use NFC tags for identification. 我正在开发一个将使用NFC标签进行识别的应用程序。 All examples I find however, are about starting an app when a certain card is read. 然而,我发现的所有示例都是关于在读取某张卡时启动应用程序。 I tried looking for an example or documentation on how to do it differently, to no avail. 我试着寻找一个关于如何以不同方式做到这一点的示例或文档,但无济于事。

What I want is: 我想要的是:

  1. user starts my app 用户启动我的应用程序
  2. user scans NFC card 用户扫描NFC卡
  3. app decides next step app决定下一步

I got some code working now, I just don't get the tag data: 我现在有一些代码工作,我只是没有得到标签数据:

In onCreate : onCreate

pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
              getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

tech = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
    tech.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
   throw new RuntimeException("fail", e);
}
intentFiltersArray = new IntentFilter[] { tech };

And in onResume : 并在onResume

nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techList);

The intent only gets there when the app is active, but the Intent I receive is the PendingIntent I defined myself, not the ACTION_TECH_DISCOVERED intent I want. 当应用程序处于活动状态时,意图才会到达,但我收到的Intent是我自己定义的PendingIntent ,而不是我想要的ACTION_TECH_DISCOVERED意图。

I found part of the answer here: NFC broadcastreceiver problem 我在这里找到了部分答案: NFC广播接收器问题

That solution doesn't provide a complete working example, so I tried a little extra. 该解决方案没有提供完整的工作示例,所以我尝试了一点额外的。 To help future visitors, I'll post my solution. 为了帮助未来的访问者,我将发布我的解决方案。 It is a NfcActivity which subclasses Activity , if you subclass this NfcActivity , all you have to do is implement its NfcRead method, and you're good to go: 它是一个NfcActivity ,它是Activity子类,如果你NfcActivity这个NfcActivity ,你所要做的就是实现它的NfcRead方法,你很高兴:

public abstract class NfcActivity extends Activity  {
    // NFC handling stuff
    PendingIntent pendingIntent;
    NfcAdapter nfcAdapter;

    @Override
    public void onResume() {        
        super.onResume();

        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);

         nfcAdapter = NfcAdapter.getDefaultAdapter(this);
         nfcAdapter.enableForegroundDispatch(this, pendingIntent, null, null);
    }

    @Override
    protected void onPause() {
        super.onPause();
        nfcAdapter.disableForegroundDispatch(this);
    }

    // does nothing, has to be overridden in child classes
    public abstract void NfcRead(Intent intent);

    @Override
    public void onNewIntent(Intent intent) {
        String action = intent.getAction();

        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(action)) {
            NfcRead(intent);            
        } 
    }
}

If you want that the user first starts your app and only then scans an NFC card, I would recommend using NFC foreground dispatch . 如果您希望用户首先启动您的应用程序,然后只扫描NFC卡,我建议使用NFC前台调度 In that way, your Activity does not need to have any intent filters in the manifest (and thus will never be called by accident when the user scans another NFC card). 这样,您的Activity不需要在清单中有任何意图过滤器(因此当用户扫描另一张NFC卡时,不会被意外调用)。 With foreground dispatch enabled, your Activity can receive all NFC intents directly (without any app chooser pop-ups) and decide what to do with it (eg pass it on to another Activity). 启用前台调度后,您的活动可以直接接收所有NFC意图(没有任何应用程序选择器弹出窗口)并决定如何处理它(例如将其传递给另一个活动)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM