【问题标题】:Sending Dynamic NdefRecords to the constructor将动态 NdefRecords 发送到构造函数
【发布时间】:2014-07-16 05:13:31
【问题描述】:

我正在尝试使用 NFC 构建一个应用程序,以通过 NFC 标签在手机之间传递数据。 但有一件困扰我的事情是,我怎样才能将各种大小的记录发送到构造函数中? 例如,这个构造函数只接受 2 个 NDEFRecord。

NdefMessage message = new NdefMessage(new NdefRecord[]{
            NfcUtils.createRecord(MIME_TYPE, text.getBytes()),
            NfcUtils.createRecord(MIME_TYPE, text.getBytes()),
            NdefRecord.createApplicationRecord(TARGET_APPLICATION)
    });

我的应用程序可能会发送各种记录,但构造函数似乎接受“静态”记录。

对于可能造成的任何错误/不便,我深表歉意。 我希望你们都明白我想问的。

【问题讨论】:

    标签: java android nfc


    【解决方案1】:

    NdefMessage 构造函数可以使用任意数量的 NdefRecords。

    NdefRecord first = NdefRecord.createUri("http://stackoverflow.com/");
    NdefRecord second = NdefRecord.createMime("text/plain", "content".getBytes());
    NdefRecord third = NdefRecord.createExternal("com.stackoverflow", "nfc", "content".getBytes());
    // and so on ...
    
    NdefMessage message = new NdefMessage(first, second, third);
    

    如果要动态添加记录,可以填充 NdefRecord 数组并将其作为参数传递。

    NdefRecord[] records = new NdefRecord[3];
    records[0] = NdefRecord.createUri("http://stackoverflow.com/");
    records[1] = NdefRecord.createMime("text/plain", "content".getBytes());
    records[2] = NdefRecord.createExternal("com.stackoverflow", "nfc", "content".getBytes());
    
    NdefMessage message = new NdefMessage(records);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多