首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何主动读取NFC标签-无需意图

如何主动读取NFC标签-无需意图
EN

Stack Overflow用户
提问于 2014-01-23 20:16:22
回答 3查看 1.6K关注 0票数 2

我想在不使用意图的情况下扫描NFC标签。换句话说,我想强制扫描。我已经读过了:

http://developer.android.com/guide/topics/connectivity/nfc/index.html

https://code.google.com/p/ndef-tools-for-android/

但两者都使用意图。

附注:我的情况是NFC标签是永久贴在设备上的,所以我不能使用intents。

EN

回答 3

Stack Overflow用户

发布于 2016-01-05 23:58:40

使用foreground dispatchhttp://developer.android.com/guide/topics/connectivity/nfc/advanced-nfc.html#foreground-dispatch

代码语言:javascript
运行
复制
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        nfcAdapter = NfcAdapter.getDefaultAdapter(this);

        if (nfcAdapter == null || !nfcAdapter.isEnabled()) {
            Log.e(TAG, "No NFC Adapter found.");
            //finish();
        }

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

        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        try {
            // Handles all MIME based dispatches. You should specify only the ones that you need.
            ndef.addDataType("*/*");
        } catch (IntentFilter.MalformedMimeTypeException e) {
            throw new RuntimeException("failed to add MIME type", e);
        }
        //intentFiltersArray = new IntentFilter[]{ndef,};

        //Use no intent filters to accept all MIME types
        intentFiltersArray = new IntentFilter[]{};

        // The tech list array can be set to null to accept all types of tag
        techListsArray = new String[][]{new String[]{
                IsoDep.class.getName(),
                NfcA.class.getName(),
                NdefFormatable.class.getName()
        }};
    }

    public void onPause() {
        nfcAdapter.disableForegroundDispatch(this);

        super.onPause();
    }

    public void onResume() {
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, intentFiltersArray, techListsArray);

        super.onResume();
    }

    public void onNewIntent(Intent intent) {
        Tag nfcTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        //do something with tagFromIntent
}
票数 2
EN

Stack Overflow用户

发布于 2014-11-22 22:53:36

很简单。当你达到了你的“灵活性”时间...你真的改变了。使用INTENt,读取标签,并将信息保存在您的SD卡、共享首选项、云中的任何位置。并在每次要读取标记时使用此信息。取而代之的是读取在附加标签时创建的文件。下次删除标记并添加另一个标记时,将重新创建您的文件。

不读取标签,读取由附加到设备的标签创建的文件。

票数 0
EN

Stack Overflow用户

发布于 2014-11-23 00:27:29

你不能随心所欲。由于Android NFC子系统的构建方式,在没有意图的情况下读取标签是不可能的。

此外,将标签粘在手机背面也是一个非常糟糕的主意。只要没有检测到标签,NFC就会周期性地检查标签的存在。一旦检测到标签,它将通过空中供电,直到标签不再应答。

如果标签总是在手机的范围内,它将疯狂地耗尽电池寿命。

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

https://stackoverflow.com/questions/21307898

复制
相关文章

相似问题

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