Android NFC标签添加联系人

这是一个NFC应用程序,功能是当程序在带有NFC功能的手机上运行时,它既能够把联系人信息写入NFC标签,也能在检测到有NFC标签靠近时,自动读取标签中的联系人信息并添加到手机里。

因为记录得会比较详细,所以代码可能会比较多。


首先,贴上这个工程截图:



这个应用程序分为两个包,一个包是所有Activity的集合包,一个包是util即工具包。


首先介绍一下工具类的作用:


BobNdefMessage.java:用来把各种类型的数据组合成一个NdefMessage的工具类。

代码如下:

<span style="font-size:18px;">package com.example.util;

import com.google.common.collect.BiMap;
import com.google.common.collect.ImmutableBiMap;
import android.annotation.SuppressLint;
import android.net.Uri;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import java.nio.charset.Charset;
import java.util.Locale;


public class BobNdefMessage {
 
    /**
     * @About:create a TNF_WELL_KNOW NDEF record as RTD_URI
     * @param uriFiledStr , The rest of the URI, or the entire URI (if
     *            identifier code is 0x00).
     * @param identifierCode = prefixes(URI identifier code), 0x01=http://www.
     *            ,0x02=https://www. , 0x03=http://
     * @param flagAddAAR, true means add AAR
     * @return NdefMessage
     * @Ref: NFCForum-TS-RTD_URI_1.0
     * @By SkySeraph-2013
     */
    public static NdefMessage getNdefMsg_from_RTD_URI(String uriFiledStr, byte identifierCode,
            boolean flagAddAAR) {
        byte[] uriField = uriFiledStr.getBytes(Charset.forName("US-ASCII"));
        byte[] payLoad = new byte[uriField.length + 1]; // add 1 for the URI
                                                        // Prefix
        payLoad[0] = identifierCode; // 0x01 = prefixes http://www. to the URI
        // appends URI to payload
        System.arraycopy(uriField, 0, payLoad, 1, uriField.length);

        // Method1:
        NdefRecord rtdUriRecord1 = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_URI,
                new byte[0], payLoad);

        // Method2:only in API 14
        String prefix = URI_PREFIX_MAP.get(identifierCode);
        NdefRecord rtdUriRecord2 = NdefRecord.createUri(prefix + uriFiledStr);

        // Method3:only in API 14
        NdefRecord rtdUriRecord3 = NdefRecord.createUri(Uri.parse(prefix + uriFiledStr));

        if (flagAddAAR) {
            // note: returns AAR for different app (nfcreadtag)
            return new NdefMessage(new NdefRecord[] {
                    rtdUriRecord1, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
            }); // packageName
        } else {
            return new NdefMessage(new NdefRecord[] {
                rtdUriRecord1
            });
        }
    }

    /**
     * @About:create a TNF_WELL_KNOW NDEF record as RTD_TEXT
     * @param text , the really text data
     * @param encodeInUtf8 , false means TEXT encoded by UTF-8
     * @param flagAddAAR , true means add AAR
     * @return NdefMessage
     * @Ref: NFCForum-TS-RTD_Text_1.0
     * @By SkySeraph-2013
     */
    public static NdefMessage getNdefMsg_from_RTD_TEXT(String text, boolean encodeInUtf8,
            boolean flagAddAAR) {

        Locale locale = new Locale("en", "US"); // a new Locale is created with
                                                // US English.
        byte[] langBytes = locale.getLanguage().getBytes(Charset.forName("US-ASCII"));
        // boolean encodeInUtf8 = false;
        Charset utfEncoding = encodeInUtf8 ? Charset.forName("UTF-8") : Charset.forName("UTF-16");
        int utfBit = encodeInUtf8 ? 0 : (1 << 7);
        char status = (char)(utfBit + langBytes.length);
        // String text = "This is an RTD_TEXT exp";
        byte[] textBytes = text.getBytes(utfEncoding);
        byte[] data = new byte[1 + langBytes.length + textBytes.length];
        data[0] = (byte)status;
        System.arraycopy(langBytes, 0, data, 1, langBytes.length);
        System.arraycopy(textBytes, 0, data, 1 + langBytes.length, textBytes.length);
        NdefRecord textRecord = new NdefRecord(NdefRecord.TNF_WELL_KNOWN, NdefRecord.RTD_TEXT,
                new byte[0], data);

        if (flagAddAAR) {
            // note: returns AAR for different app (nfcreadtag)
            return new NdefMessage(new NdefRecord[] {
                    textRecord, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
            });
        } else {
            return new NdefMessage(new NdefRecord[] {
                textRecord
            });
        }
    }

    /**
     * @About: create a TNF_ABSOLUTE_URI NDEF record
     * @param absoluteUri ,the absolute Uri
     * @param flagAddAAR , true means add AAR
     * @return NdefMessage
     * @Note: TNF_ABSOLUTE_URI indicates the absolute form of a URI that follows
     *        the absolute-URI rule defined by RFC 3986
     * @Note: Recommend that you use the RTD_URI type instead of
     *        TNF_ABSOLUTE_URI, because it is more efficient
     * @By SkySeraph-2013
     */
    public static NdefMessage getNdefMsg_from_ABSOLUTE_URI(String absoluteUri, boolean flagAddAAR) {
        // String absoluteUri = "http://developer.android.com/index.html";
        byte[] absoluteUriBytes = absoluteUri.getBytes(Charset.forName("US-ASCII"));
        NdefRecord uriRecord = new NdefRecord(NdefRecord.TNF_ABSOLUTE_URI, new byte[0],
                new byte[0], absoluteUriBytes);
        if (flagAddAAR) {
            // note: returns AAR for different app (nfcreadtag)
            return new NdefMessage(new NdefRecord[] {
                    uriRecord, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
            });
        } else {
            return new NdefMessage(new NdefRecord[] {
                uriRecord
            });
        }
    }

    /**
     * @About:create a TNF_MIME_MEDIA NDEF record
     * @param payLoad,the MIME data
     * @param mimeType,the MIME Type
     * @param flagAddAAR, true means add AAR
     * @return NdefMessage
     * @By SkySeraph-2013
     */
    @SuppressLint("NewApi")
    public static NdefMessage getNdefMsg_from_MIME_MEDIA(String payLoad, String mimeType,
            boolean flagAddAAR) {
        byte[] payLoadBytes = payLoad.getBytes(Charset.forName("US-ASCII"));
        // String mimeType = "application/skyseraph.nfc_demo";

        // method1:Creating the NdefRecord manually
        NdefRecord mimeRecord1 = new NdefRecord(NdefRecord.TNF_MIME_MEDIA,
                mimeType.getBytes(Charset.forName("US-ASCII")), new byte[0], payLoadBytes);
        // the identfier of the record is given as 0, since it will be the first
        // record in the NdefMessage

        // method2:Using the createMime() method, in API-16
        NdefRecord mimeRecord2 = NdefRecord.createMime(mimeType, payLoadBytes);

        if (flagAddAAR) {
            // note: returns AAR for different app (nfcreadtag)
            return new NdefMessage(new NdefRecord[] {
                    mimeRecord1, NdefRecord.createApplicationRecord("skyseraph.nfc_demo")
            });
        } else {
            return new NdefMessage(new NdefRecord[] {
                mimeRecord1
            });
        }
    }

    /**
     * @About:create a TNF_EXTERNAL_TYPE NDEF record
     * @param payLoad,the EXTERNAL data
     * @param flagAddAAR, true means add AAR
     * @return NdefMessage
     * @By SkySeraph-2013
     */
    @SuppressLint("NewApi")
    public static NdefMessage getNdefMsg_from_EXTERNAL_TYPE(String payLoad, boolean flagAddAAR) {
        byte[] payLoadBytes = payLoad.getBytes();
        String domain = "skyseraph.nfc_demo"; // usually your app's package name
        String type = "externalType";
        String externalType = domain + ":" + type;

        // method1:Creating the NdefRecord manually
        NdefRecord exteralRecord1 = new NdefRecord(NdefRecord.TNF_EXTERNAL_TYPE,
                externalType.getBytes(), new byte[0], payLoadBytes);

        // method2:Using the createExternal() method, in API-16
        NdefRecord exteralRecord2 = NdefRecord.createExternal(domain, type, payLoa
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值