【发布时间】:2014-05-19 15:25:35
【问题描述】:
我是 Android 上 NFC 的新手,但我设法让应用程序读取和写入 NFC 标签。
最初,我在标签上的 NdefMessage 中有一个 NdefRecord,基本上是一些数据。每当我扫描它时,我都能成功地从标签中检索数据。
然后我想添加一个应用程序记录,这样如果用户扫描我的标签并且没有我的应用程序,他们就会被重定向到 PlayStore。
当我将应用程序记录引入标签时,每次我扫描标签时,我的活动都由onCreate() 或onNewIntent() 开始/恢复,我尝试获取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();
}
这是我阅读标签的地方(onCreate() 和 onNewIntent());
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
if(tag!=null) {
readTag(tag);
}
我在 AndroidManifest.xml 中的 IntentFilters
<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>
【问题讨论】:
-
您在 AndroidManifest.xml 中使用了哪些意图过滤器?
-
@MichaelRoland 已更新 :)
标签: android nfc ndef android-applicationrecord