How to write NDEF message to any MIME type of NFC tag?

早过忘川 提交于 2019-12-09 23:41:24

问题


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?


回答1:


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.



来源:https://stackoverflow.com/questions/25107354/how-to-write-ndef-message-to-any-mime-type-of-nfc-tag

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