如何将NDEF消息写入任何MIME类型的NFC标签? [英] How to write NDEF message to any MIME type of NFC tag?

查看:751
本文介绍了如何将NDEF消息写入任何MIME类型的NFC标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了此活动来创建自己的NFC标签类型

public class WriteTag extends Activity {

    Tag tag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_tag);
    }

    public void onResume()
    {
        super.onResume();
        Intent intent = getIntent();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            try {
                write("my/type", "this is payload text", tag);
                finish();
            }
            catch(Exception e)
            {

            }
        }
    }

    private NdefRecord createRecord(String mimeType, String text) throws UnsupportedEncodingException {

        NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "my/type".getBytes(Charset.forName("US-ASCII")), new byte[0], text.getBytes(Charset.forName("US-ASCII")));
        return recordNFC;
    }

    private void write(String mimeType, String text, Tag tag) throws IOException, FormatException {

        NdefRecord[] records = { createRecord(mimeType, text) };
        NdefMessage message = new NdefMessage(records);
        Ndef ndef = Ndef.get(tag);
        ndef.connect();
        ndef.writeNdefMessage(message);
        ndef.close();
    }
}

然后我在清单中使用此过滤器来触发它以开始写操作

否则我不知道如何获取标签实例

<activity android:name=".WriteTag">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="my/tag" />
    </intent-filter>
</activity>

但是有一个大问题,我必须使用另一个应用我/类型首先写入NFC标签,否则我的应用无法识别该标签. /p>

如何强制应用写入任何NFC标签?以及为什么其他应用程序可以让用户:1点击按​​钮,2等待标签接近,3写标签?

解决方案

NDEF_DISCOVERED意向过滤器只能用于已经包含某些(已知)NDEF数据类型的标记.

相反,您可以使用TECH_DISCOVERED意向过滤器注册发现所有NdefNdefFormatable标签(或最适合您的其他任何标签类型)的发现:

<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
           android:resource="@xml/nfc_tech_filter" />

您的xml/nfc_tech_filter.xml文件如下所示:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
</resources>

然后,在您的代码中,您当然需要替换该行

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

使用

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {


或者(或与上述方法结合使用),您可以使用前台分派方法仅在活动处于前台时接收那些NFC事件(而不是在检测到 any 后启动NDEF(兼容)标记(不是由具有更好匹配的NDEF_DISCOVERED过滤器的活动处理的),可能会使用户烦恼.请参阅此答案以获取有关执行此操作的更多详细信息.

I have created this activity to create my own type of NFC tag

public class WriteTag extends Activity {

    Tag tag;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_tag);
    }

    public void onResume()
    {
        super.onResume();
        Intent intent = getIntent();
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {
            tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
            try {
                write("my/type", "this is payload text", tag);
                finish();
            }
            catch(Exception e)
            {

            }
        }
    }

    private NdefRecord createRecord(String mimeType, String text) throws UnsupportedEncodingException {

        NdefRecord recordNFC = new NdefRecord(NdefRecord.TNF_MIME_MEDIA, "my/type".getBytes(Charset.forName("US-ASCII")), new byte[0], text.getBytes(Charset.forName("US-ASCII")));
        return recordNFC;
    }

    private void write(String mimeType, String text, Tag tag) throws IOException, FormatException {

        NdefRecord[] records = { createRecord(mimeType, text) };
        NdefMessage message = new NdefMessage(records);
        Ndef ndef = Ndef.get(tag);
        ndef.connect();
        ndef.writeNdefMessage(message);
        ndef.close();
    }
}

And I use this filter in manifest to trigger it to start the write action

otherwise I don't know how to get the tag instance

<activity android:name=".WriteTag">
    <intent-filter>
        <action android:name="android.nfc.action.NDEF_DISCOVERED"/>
        <category android:name="android.intent.category.DEFAULT"/>
        <data android:mimeType="my/tag" />
    </intent-filter>
</activity>

But there is a big problem, I must use another app to write the my/type into the NFC tag first, otherwise my app cannot recognize the tag.

How to force the app to write into any NFC tag? and why other app can let user: 1 tap button, 2 wait the tag approach, 3 write into tag?

解决方案

The NDEF_DISCOVERED intent filter can only be used for tags that already contain some (known) NDEF data type.

Instead, you could use the TECH_DISCOVERED intent filter to register for the discovery of just any Ndef or NdefFormatable tag (or any other tag type that best fits your needs):

<intent-filter>
    <action android:name="android.nfc.action.TECH_DISCOVERED" />
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
           android:resource="@xml/nfc_tech_filter" />

With your xml/nfc_tech_filter.xml file looking like this:

<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
    <tech-list>
        <tech>android.nfc.tech.Ndef</tech>
    </tech-list>
    <tech-list>
        <tech>android.nfc.tech.NdefFormatable</tech>
    </tech-list>
</resources>

In your code, you would then, of course, need to replace the line

if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())) {

with

if (NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {


Alternatively (or in combination with the above), you could use the foreground dispatch method to just receive those NFC events while your activity is in the foreground (as opposed to being launched upon detection of any NDEF(-compatible) tag (that is not handled by an activity with a better matching NDEF_DISCOVERED filter), which could be annoying for the user. See this answer for further details on how to do this.

这篇关于如何将NDEF消息写入任何MIME类型的NFC标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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