如何从检测到的NFC标签(NDEF消息)读取。 Android的NFC [英] How to read from detected NFC tag (NDEF message). Android NFC

查看:525
本文介绍了如何从检测到的NFC标签(NDEF消息)读取。 Android的NFC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类似的问题 - <一个href="http://stackoverflow.com/questions/9971820/how-to-read-detected-nfc-tag-ndef-content-details-in-android/9976094#9976094">How读取检测NFC标签(NDEF内容)在Android的细节?

我希望我的Andr​​oid应用程序,以便能够读取和分析检测的NDEF消息。

I want my android app to be able to read and parse a detected NDEF message.

我已经编辑了AndroidManifest.xml中检测到的NFC标签,我已经添加了意图过滤器,它看起来像这样

I have already edited the AndroidManifest.xml to detect nfc tags and I have added the intent filter it looks like this

            <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

我相信这是好的,当我使用NFCDemo示例应用程序自带的SDK创建MockNDEFtags,当应用程序列表,我可以选择处理这些生成的标签我的应用程序出现。 然后我点击我的应用程序,它开辟了没有问题,我只是需要一种方法来读取传递给它的NDEF消息中的数据。在code:

I believe this is fine as when I use the NFCDemo sample app that comes with the SDK to create MockNDEFtags, when the list of applications I can choose to handle these generated tags my app appears. I then click on my app and it opens up without problem, I just need a way to read the data that was passed to it in the NDEF message. The code:

 Tag myTag = (Tag) nfcintent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

 // get NDEF tag details
 Ndef ndefTag = Ndef.get(myTag);
 ...
 NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();

建议在类似的问题和整个网络我发现很多类似的答案。 我的问题是与code行

was suggested in a similar question and throughout the web I find many similar answers. My problem is with the line of code

"Tag myTag = (Tag) nfcintent.getParcelableExtra(NfcAdapter.EXTRA_TAG);"

我得到的错误nfcintent无法得到解决 我知道的code作者可能把nfcintent作为一个占位符意图具体到我的应用程序但林不知道什么IM应该把它的位置。

I get the error "nfcintent cannot be resolved" I realize that the author of the code likely put nfcintent as a placeholder for an intent specific to my app however im not sure what im supposed to put in its place.

我mainactivity启动我的应用程序看起来像这样

My mainactivity that starts my app looks like this

public class TabsActivity extends TabActivity {

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    TabHost tabHost = getTabHost();

    // Tab for Graph
    TabSpec graphspec = tabHost.newTabSpec("Graph");
    // setting Title and Icon for the Tab
    graphspec.setIndicator("Graph");
    Intent graphIntent = new Intent(this, GraphActivity.class);
    graphspec.setContent(graphIntent);

    // Tab for Intro
    TabSpec introspec = tabHost.newTabSpec("Intro");
    introspec.setIndicator("Intro");
    Intent introIntent = new Intent(this, IntroActivity.class);
    introspec.setContent(introIntent);


    // Adding all TabSpec to TabHost
    tabHost.addTab(introspec); // Adding intro tab
    tabHost.addTab(graphspec); // Adding graph tab

}

}

我想,因为这将启动应用程序,这就是NFC标签必须处理。如果我可以从标签访问NDEFMessage我已经与NdefMessageParser从Android示例应用程序解析它的能力。我想从NDEFmessage解析信息,并最终必须通过在应用程序的每个选项卡访问这些信息。

I assume as this starts the app it is where the NFC tag must be dealt with. If I can just access the NDEFMessage from the tag I already have the ability to parse it with the NdefMessageParser from the android sample app. I want to parse the information from the NDEFmessage and ultimately have that information accessible by each tab in the app.

推荐答案

阅读:

<一个href="http://mifareclassicdetectiononandroid.blogspot.in/2011/04/reading-mifare-classic-1k-from-android.html" rel="nofollow">http://mifareclassicdetectiononandroid.blogspot.in/2011/04/reading-mifare-classic-1k-from-android.html

这篇关于如何从检测到的NFC标签(NDEF消息)读取。 Android的NFC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆