NFC标签为空时,比&GT以上; 1 NdefRecord [英] NFC Tag is null when more than >1 NdefRecord

查看:365
本文介绍了NFC标签为空时,比&GT以上; 1 NdefRecord的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IM NFC Android上的一个新手,但设法有一个应用程序读取和放大器;写入NFC标签。

我本来在NdefMessage 1 NdefRecord标签上,基本上一些数据。我成功地从标记检索数据,每当我扫描了。

然后我想添加一个应用程序的记录,因此,如果用户的扫描我的标签,并没有我的应用程序,它们将被重定向到Play商店中。

在我公司推出的应用程序记录到标签,我每次扫描我的活动开始的标签/由要么恢复的onCreate() onNewIntent (),我尝试获得标签,但它始终是零。这是为什么?

下面是我写的标签;

 标签detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
NdefRecord AP precord = NdefRecord.createApplicationRecord(com.myorg.myapp);
NdefRecord纪录=新NdefRecord(NdefRecord.TNF_MIME_MEDIA,
        新的String(应用程序/ com.myorg.myapp)
        .getBytes(Charset.forName(US-ASCII))
        空,StringData是.getBytes());
NdefMessage消息=新NdefMessage(新NdefRecord [] {AP precord,记录});
如果(writeTag(消息,detectedTag)){
Toast.makeText(这一点,成功:写placeid到NFC标签,Toast.LENGTH_LONG).show();
}

和这里是我读标签(无论是在的onCreate() onNewIntent()

 标签标记= intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
如果(标记!= NULL){
    readTag(标签);
}

我在AndroidManifest.xml IntentFilters

 <意向滤光器>
                <作用机器人:名字=android.nfc.action.NDEF_DISCOVERED/>
                <数据机器人:mime类型=应用/ com.myorg.myapp/>
                <类机器人:名字=android.intent.category.DEFAULT/>
 &所述; /意图滤光器>


解决方案

该NDEF_DISCOVERED意图只能过滤的第一条记录在标签的NDEF消息的类型。您N​​DEF消息是这样的:

  + ------------------------------- ---------- +
| EXT:机器人:COM:PKG | com.myorg.myapp |
+ ----------------------------------------------- +
| MIME类型:应用程序/ com.myorg.myapp | StringData是|
+ ----------------------------------------------- +

所以,你会需要修改你的意图过滤器是外部类型敏感的android:COM:PKG 。或者,您将Android应用程序记录(AAR)的NDEF消息的结束(这是prefered方式):

  NdefMessage消息=新NdefMessage(新NdefRecord [] {纪录,美联社precord});

这将导致这个消息:

  + ------------------------------- ---------- +
| MIME类型:应用程序/ com.myorg.myapp | StringData是|
+ ----------------------------------------------- +
| EXT:机器人:COM:PKG | com.myorg.myapp |
+ ----------------------------------------------- +

这使得应用程序/ com.myorg.myapp <​​/ code>记录(您在清单筛选)用于意图过滤匹配的记录。

最后,我强烈建议您使用NFC论坛外围类型,而不是使用自定义MIME类型。这有两个好处:


  1. 在外部类型名称类型名称可以是显著更紧凑(小于字节),比MIME类型开头为应用程序/\".

  2. 您是(由于基于域的命名空间概念)保护,命名,注册MIME类型的冲突。

Im a newbie at NFC on Android, but have managed to have an app read & write to a NFC tag.

Originally, I had one NdefRecord in a NdefMessage on the tag, basically some data. I was successful at retrieving the data from the tag whenever I scanned it.

I then wanted to add an application record, so that if user's scan my tag and do not have my app, they are redirected to the PlayStore.

When I introduced the application record to the tag, every time I scan the tag my activity starts/resumes by either onCreate() or onNewIntent(), I attempt to get the Tag, but it is always null. Why is this?

Here is what I write to the tag;

Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);      
NdefRecord appRecord = NdefRecord.createApplicationRecord("com.myorg.myapp");
NdefRecord record = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
        new String("application/com.myorg.myapp")
        .getBytes(Charset.forName("US-ASCII")),
        null, "StringData".getBytes());
NdefMessage message = new NdefMessage(new NdefRecord[] { appRecord, record });
if (writeTag(message, detectedTag)) {
Toast.makeText(this, "Success: Wrote placeid to nfc tag", Toast.LENGTH_LONG).show();
} 

And here is where I read the tag (both in onCreate() and onNewIntent());

Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag!=null) {
    readTag(tag);
}

My IntentFilters in AndroidManifest.xml

 <intent-filter>
                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <data android:mimeType="application/com.myorg.myapp" />
                <category android:name="android.intent.category.DEFAULT" />
 </intent-filter>

解决方案

The NDEF_DISCOVERED intent can only filter for the type of the first record in a tag's NDEF message. Your NDEF message looks like this:

+-----------------------------------------------+
| EXT:android:com:pkg | com.myorg.myapp         |
+-----------------------------------------------+
| MIME:application/com.myorg.myapp | StringData |
+-----------------------------------------------+

So you would need to modify your intent filter to be sensitive to the external type android:com:pkg. Or, you move the Android Application Record (AAR) to the end of the NDEF message (that's the prefered way):

NdefMessage message = new NdefMessage(new NdefRecord[] { record, appRecord });

Which results into this message:

+-----------------------------------------------+
| MIME:application/com.myorg.myapp | StringData |
+-----------------------------------------------+
| EXT:android:com:pkg | com.myorg.myapp         |
+-----------------------------------------------+

This makes the application/com.myorg.myapp record (that you filter for in your manifest) the record used for intent filter matching.

Finally, I would strongly suggest that you use an NFC Forum external type instead of using a custom MIME type. This would have two advantages:

  1. The type name in external type names can be significantly more compact (less bytes) than MIME types starting with "application/".
  2. You are (due to the domain-based namespace concept) protected from naming conflicts with registered MIME types.

这篇关于NFC标签为空时,比&GT以上; 1 NdefRecord的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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