android 模拟nfc tag,How to read from detected NFC tag (NDEF message). Android NFC

开发者遇到一个问题,在Android应用中无法从NFC标签读取NDEF消息。已经设置了AndroidManifest.xml来检测NFC标签,但代码中'nfcintent'无法解析,导致无法获取NDEF消息。问题集中在如何正确地从Intent中获取Tag对象以读取NDEF数据。解决方案可能涉及到正确处理Intent并访问NFC标签的数据。
摘要由CSDN通过智能技术生成

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值