Get NFC tag with NDEF Android Application Record (AAR)

前端 未结 2 1107
有刺的猬
有刺的猬 2020-11-28 16:16

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 wi

相关标签:
2条回答
  • 2020-11-28 16:27

    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. 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.)

    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>
    

    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>
    

    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).

    0 讨论(0)
  • 2020-11-28 16:39

    I don't know if this is correct answer but I found a fix:

            <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:mimeType="text/plain" />
                </intent-filter>
            </activity>
        </application>
    
    </manifest>
    
    0 讨论(0)
提交回复
热议问题