获取NFC标签与NDEF Android应用程序记录(AAR) [英] Get NFC tag with NDEF Android Application Record (AAR)

查看:884
本文介绍了获取NFC标签与NDEF Android应用程序记录(AAR)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的NFC应用。要,我使用的是NDEF标签与AAR NDEF记录里开始我的应用程序。

I am working on an NFC-application. To start my app, I am using a NDEF-tag with an AAR NDEF Record inside.

这工作得很好。但现在我想读取标签的内容直接与应用程序。我怎样才能做到这一点?

This works fine. But now I want to read the tag content with the app directly. How can I do this?

(它已经工作,当我删除标签从手机,再触摸它,但我想要消除此步骤。)

(It already works, when I remove the tag from the phone and touch it again, but I want to eliminate this step.)

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="nfctagscanner.mobileapps.lt.nfctagscanner" >
    <uses-feature android:name="android.hardware.nfc" android:required="true"/>
    <uses-permission android:name="android.permission.NFC" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />


            </intent-filter>


        </activity>
        <activity
            android:name=".NFCScanActivity"
            android:label="@string/app_name" >
            <intent-filter>

                <action android:name="android.nfc.action.NDEF_DISCOVERED" />
                <category android:name="android.intent.category.DEFAULT" />
                <data android:scheme="nfctagscanner.mobileapps.lt.nfctagscanner" android:host="ext"
                    android:pathPrefix="/android.com:pkg" />
            </intent-filter>
        </activity>
    </application>

</manifest>

和我只是想获得NDEF_DISCOVERED意图。但是,我总是得到action.MAIN / category.LAUNCHER意向与我的调试器。

And I just want to get NDEF_DISCOVERED intent. But I always get action.MAIN/category.LAUNCHER Intent with my debugger.

任何帮助将AP preciated。我在做我的工作,在此基础上:<一href="http://stackoverflow.com/questions/21166441/android-nfc-get-tag-in-oncreate-without-new-intent">Android / NFC:获取标签中的onCreate(),而新的意向

Any help will be appreciated. I was doing my work based on this: Android / NFC: Get Tag in onCreate() without new Intent

推荐答案

如果你希望你的应用程序,以你的标签开始,并希望接收解析NDEF消息,你的标签应包含以下内容:

If you want your app to be started by your tag and want to receive the parsed NDEF message, your tag should contain the following:

  1. 要在作为NDEF消息的第一个记录来过滤NDEF记录(你只能创建意图过滤器在一个NFC标签的第一个NDEF记录)。鉴于自己的答案,这将是一个文字实录(或MIME类型文本/纯MIME类型记录)在外壳。但是,你可能要考虑使用与您的自定义应用程序特定的类型名称的NFC论坛外部类型的记录,而不是(见的这里,因为这将使您的应用程序,以更好地distingush你的标签与其他应用程序使用的标签。只能用于文本记录,如果它包含了人类可读的不跨preTED 文字
  2. 如果你想确保的只有的您的应用程序开始标记你的应用的Play商店的网页,如果用户不具有应用程序自动打开安装,那么你也应该使用一个Android应用程序记录(AAR)。该记录应在最后一个在NDEF消息记录。 (请注意,一个AAR将无论在NDEF消息的位置的兑现,但用它作为第一个记录将导致的发表你提问的链接。)
  1. An NDEF record that you want to filter upon as the first record of the NDEF message (you can only create intent-filters for the very first NDEF record on an NFC tag). Given your own answer, this would be a Text record (or a MIME type record with MIME type text/plain) in your case. However, you might want to consider using an NFC Forum external type record with your custom application-specific type name instead (see here as this would allow your app to better distingush your tags from tags used with other apps. The Text record should only be used if it contains human-readable non-interpreted text.
  2. If you want to make sure that only your app is started with the tag and that your app's Play Store page is automatically opened if the user does not have the app installed, then you should also use an Android Application Record (AAR). This record should be the last record in the NDEF message. (Note that an AAR will be honored regardless of the position in the NDEF message, but using it as the first record will cause the problem described in the post you linked in your question.)

有一次,有一个包含您的标记正确的NDEF消息准备好了,你必须创建一个合适的意图过滤器的活动应该接收标签发现事件和NDEF消息触发它:

Once, you have your tag containing a proper NDEF message ready, you have to create an appropriate intent filter for the activity that should receive the tag discovery event and the NDEF message that triggered it:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="text/plain" />
</intent-filter>

或者,如果你使用你的自定义NFC论坛外部类型:

Or if you used your custom NFC Forum external type:

<intent-filter>
    <action android:name="android.nfc.action.NDEF_DISCOVERED" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:scheme="vnd.android.nfc"
          android:host="ext"
          android:pathPrefix="/yourdomain.com:yourtypename"/>
</intent-filter>

如果您的应用程序的做的不可以有无的的 NDEF_DISCOVERED 意图过滤器的匹配第一在你的标签NDEF消息的记录您标记的包含了一个Android应用程序记录的,在你的清单中声明的​​第一个活动,有意向过滤器 android.intent.action.MAIN android.intent.category.LAUNCHER 将启动。作为该活动,因此,不声明预期的NFC意图的将会通过了 android.intent.action.MAIN (它没有的包含NDEF消息/标签手柄)。

If your app does not have an NDEF_DISCOVERED intent filter that matches the first record of the NDEF message on your tag and your tag contains an Android Application Record, the first activity declared in your manifest that has an intent filter for android.intent.action.MAIN and with android.intent.category.LAUNCHER will be started. As that activity, therefore, does not declare to expect an NFC intent it will be passed the android.intent.action.MAIN (which does not contain the NDEF message/tag handle).

这篇关于获取NFC标签与NDEF Android应用程序记录(AAR)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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