NFC Tag is null when more than >1 NdefRecord

…衆ロ難τιáo~ 提交于 2019-12-08 13:50:54

问题


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>

回答1:


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.


来源:https://stackoverflow.com/questions/23741569/nfc-tag-is-null-when-more-than-1-ndefrecord

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!