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

本文是小编为大家收集整理的关于如何从检测到的NFC标签(NDEF信息)读取。安卓NFC的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

类似的问题 - 如何在Android中读取检测到的NFC标签(NDEF内容)详细信息?

我希望我的Android应用程序能够读取和解析检测到的ndef消息.

我已经编辑了androidmanifest.xml来检测nfc标签,我添加了它看起来像这个

的意图过滤器
            <intent-filter>
            <action android:name="android.nfc.action.TAG_DISCOVERED"/>
            <category android:name="android.intent.category.DEFAULT"/>
    </intent-filter>

我相信这很好,因为当我使用sdk附带的nfcdemo示例应用程序来创建mockndeftags时,当我可以选择处理这些生成的标签时,我的应用程序会出现. 然后,我单击我的应用程序,它会在没有问题的情况下打开,我只需要一种方法来读取在ndef消息中传递给它的数据.代码:

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

 // get NDEF tag details
 Ndef ndefTag = Ndef.get(myTag);
 ...
 NdefMessage ndefMesg = ndefTag.getCachedNdefMessage();
在类似的问题和整个网络中建议我找到许多类似的答案. 我的问题是代码行

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

我收到错误"无法解析nfcintent" 我意识到代码的作者可能会将nfcintent作为特定于我的应用的意图的占位符,但我不确定我应该放在它的位置.

我的喜动性开始我的应用程序看起来像这个

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,我已经能够使用来自Android示例应用程序的NdefMessageParser解析它.我想从NdefMessage解析信息,最终将通过应用程序中的每个选项卡可访问的信息.

推荐答案

试用此代码段以从标记中提取消息:

Parcelable[] rawMsgs = intent
            .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    extractMessage(msg);

private void extractMessage(NdefMessage msg) {
        byte[] array = null;
        array = msg.getRecords()[0].getPayload();
}

还查看此示例 nfc reader/writer

其他推荐答案

获取传递NFC标记的意图以在我的情况下启动活动"nfcintent"只是使用

Intent nfcintent = getIntent();

本文地址:https://www.itbaoku.cn/post/102320.html

问题描述

Similar question - How to read detected NFC tag (NDEF content) details in android?

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

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>

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();

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);"

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.

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

}

}

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.

推荐答案

Try this Snippet to extract message from Tag :

Parcelable[] rawMsgs = intent
            .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
    NdefMessage msg = (NdefMessage) rawMsgs[0];
    extractMessage(msg);

private void extractMessage(NdefMessage msg) {
        byte[] array = null;
        array = msg.getRecords()[0].getPayload();
}

Also Check out this Sample for NFC Reader/Writer

其他推荐答案

To get the intent that passes the NFC tag to start the activity in my case "nfcintent" just use

Intent nfcintent = getIntent();