简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM