ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何在Android上制作vcard / vcal Ndef消息

2019-07-04 04:11:18  阅读:220  来源: 互联网

标签:android nfc ndef


我是Android编码的新手,我对NFC技术很感兴趣.
我的第一步专注于标签读/写模式,目前我看不到有关如何使用Vcard / Vcal MIME类型和NDEF记录/消息的大量信息.

如果有人可以发布一些示例代码或更好地指向类似教程的资源,那将是很好的!

编辑:这是我一直在使用的代码,它搞砸了.通过一些修改,我已经能够写出纯文本和智能海报/ urs数据.另外要记得的是,我正在使用Type 2 NFC标签,因为存储容量有限,我只想保存名称和放大器. Vcards上的电话号码,或活动名称& Vcal记录的日期.谢谢!

公共类VcardActivity扩展Activity {

NfcAdapter adapter;
PendingIntent pendingIntent;
IntentFilter writeTagFilters[];
Tag tag;
Context ctx;
boolean writeMode;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_text);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    ctx = this;
    Switch swWrite = (Switch) findViewById(R.id.switchWriteText);
    //final TextView msg = (TextView) findViewById(R.id.editText1);
    final String msg = "BEGIN:VCARD\n" +
            "VERSION:2.1\n" +
            "N:Gump;Forrest\n" +
            "FN:Forrest Gump\n" +
            "ORG:Bubba Gump Shrimp Co.\n" +
            "TITLE:Shrimp Man\n" +
            "TEL;WORK;VOICE111) 555-1212\n" +
            "TEL;HOME;VOICE404) 555-1212\n" +
            "ADR;WORK:;;100 Edge;Baytown;United\n" +
            "EMAIL;PREF;INTERNET:forrestgump@example.com\n " +
            "END:VCARD";
    swWrite.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
                boolean isChecked) {
            if (isChecked) {
                try {
                    if (tag == null) {
                        Toast.makeText(ctx,
                                ctx.getString(R.string.error_detected),
                                Toast.LENGTH_SHORT).show();
                    } else {
                        write(msg, tag);
                        Toast.makeText(ctx,
                                ctx.getString(R.string.ok_writing),
                                Toast.LENGTH_LONG).show();
                    }
                } catch (IOException e) {
                    Toast.makeText(ctx,
                            ctx.getString(R.string.error_writing),
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                } catch (FormatException e) {
                    Toast.makeText(ctx,
                            ctx.getString(R.string.error_writing),
                            Toast.LENGTH_LONG).show();
                    e.printStackTrace();
                }

            }
        }
    });

    /* INTENT FILTER */
    adapter = NfcAdapter.getDefaultAdapter(this);
    pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
            getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
    IntentFilter tagDetected = new IntentFilter(
            NfcAdapter.ACTION_TAG_DISCOVERED);
    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);
    writeTagFilters = new IntentFilter[] { tagDetected };
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_text, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

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

    NdefRecord[] records = { createRecord(text) };
    NdefMessage message = new NdefMessage(records);
    // Get an instance of Ndef for the tag.
    Ndef ndef = Ndef.get(tag);
    // Enable I/O
    ndef.connect();
    // Write the message
    ndef.writeNdefMessage(message);
    // Close the connection
    ndef.close();
}

private NdefRecord createRecord(String text)
        throws UnsupportedEncodingException {
    String msg = "BEGIN:VCARD\n" +
            "VERSION:2.1\n" +
            "N:Gump;Forrest\n" +
            "FN:Forrest Gump\n" +
            "ORG:Bubba Gump Shrimp Co.\n" +
            "TITLE:Shrimp Man\n" +
            "TEL;WORK;VOICE:55-1212\n" +
            "TEL;HOME;VOICE:55-1212\n" +
            "ADR;WORK:;;100 Edge;Ban;United\n" +
            "EMAIL;PREF;INTERNET:p@example.com\n " +
            "END:VCARD";

            byte[] textBytes = msg.getBytes();


            NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
            "text/x-vCard".getBytes(), new byte[] {}, textBytes);

    return textRecord;
}

@Override
protected void onNewIntent(Intent intent) {
    if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
        tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
        Toast.makeText(this,
                this.getString(R.string.ok_detection) + tag.toString(),
                Toast.LENGTH_LONG).show();
    }
}

@Override
public void onPause() {
    super.onPause();
    WriteModeOff();
}

@Override
public void onResume() {
    super.onResume();
    WriteModeOn();
}

private void WriteModeOn() {
    writeMode = true;
    adapter.enableForegroundDispatch(this, pendingIntent, writeTagFilters,
            null);
}

private void WriteModeOff() {
    writeMode = false;
    adapter.disableForegroundDispatch(this);
}

}

解决方法:

我写了一个NDEF library,它提供了高级(即非字节数组)的NDEF记录/消息表示,它还附带了一个样板项目,演示了如何在Android上使用.

为了了解NDEF格式本身,我还编写了一个Eclipse plugin,它提供了一个基于文件的图形编辑器.

干杯:-)

标签:android,nfc,ndef
来源: https://codeday.me/bug/20190704/1373385.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有