【问题标题】:How to write NDEF message to any MIME type of NFC tag?如何将 NDEF 消息写入任何 MIME 类型的 NFC 标签?
【发布时间】:2014-08-03 17:42:12
【问题描述】:

我创建了这个活动来创建我自己类型的 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>

但是有一个大问题,我必须先用另一个应用程序my/type写入NFC标签,否则我的应用程序无法识别标签。

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

【问题讨论】:

    标签: android nfc ndef


    【解决方案1】:

    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())) {
    

    或者(或结合上述方法),您可以使用前台调度方法在您的 Activity 处于前台时仅接收那些 NFC 事件(而不是在检测到 any 时启动NDEF(-compatible) 标记(不由具有更好匹配 NDEF_DISCOVERED 过滤器的活动处理),这可能会让用户感到烦恼。有关如何执行此操作的更多详细信息,请参阅this answer

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-11-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多