android NFC的读写

旧城冷巷雨未停 提交于 2019-12-07 11:34:18

一、NFC知识

1、NFC是什么?

NFC,即Near Field Communication,近距离无线通讯技术,是一种短距离的(通常<=4cm或更短)高频(13.56M Hz)无线通信技术,它提供了一种简单、触控式的解决方案,可以让消费者简单直观地交换信息、访问内容与服务 。
2、NFC的技术优势?
与蓝牙相比:NFC操作简单,配对迅速
与RFID相比:NFC适用范围广泛、可读可写,能直接集成在手机中
与红外线相比:数据传输较快、安全性高、能耗低
与二维码相比:识别迅速、信息类型多样
将来与移动支付相结合,势必简化支付的购买流程,重塑消费者的购物模式。

二、android下读写NFC

1、在AndroidManifest.xml中申明NFC权限

<uses-permission android:name="android.permission.NFC" /> 
<!-- 这项不一定需要,可以在android market中显示有NFC硬件 -->
<uses-feature android:name="android.hardware.nfc" android:required="true" />

2、NFC TAG的发布系统

当android设备扫描到一个NFC标签时,会自动寻找最适合的activity来处理这个TAG,如果有多个activity满足条件的话,会让用户选择使用哪个activity来处理,可理解为简单的事件响应与时间处理。
activity使用intentfilter来监听扫描到NFC标签事件。

IntentFilter ifilters = new IntentFilter();
ifilters.addAction(NfcAdapter.ACTION_NDEF_DISCOVERED);//NDEF
ifilters.addAction(NfcAdapter.ACTION_TAG_DISCOVERED);//TAG
ifilters.addAction(NfcAdapter.ACTION_TECH_DISCOVERED);//TECH

3、检测到标签后在Activity中的处理流程

3.1 在onCreate()中获取NfcAdapter对象

NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);

3.2 在onNewIntent()中获取Tag对象或者NdefMessage信息

//获取Tag对象
Tag tag = intent.getParcelableExra(NfcAdapter.EXTRA_TAG);
//获取NdefMessage信息
Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

3.3 也可以通过Tag创建Ndef对象等,以实现标签的属性和I/O操作。

Ndef ndef = Ndef.get(tag);

4、NDEF格式标签的读取流程

4.1 在onCreate()中获取NfcAdapter对象
4.2 在onNewIntent()中判断是否为NDEF格式标签(ACTION_NDEF_DISCOVERED),若是则获取NdefMessage信息;(需要强制转换成NdefMessage对象)

Parcelable[] rawMsgs = getIntent().getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);

4.3 对NdefMessage对象进行解析,获取相关的文本信息或Uri等

5、NDEF格式标签的写入流程

5.1 在onCreate()中获取NfcAdapter对象
5.2 在onNewIntent()中获取Tag对象

Tag tag = intent.getParcelableExra(NfcAdapter.EXTRA_TAG);

5.3 通过Tag创建Ndef对象

Ndef ndef = Ndef.get(tag);

5.4 将文本等数据封装成NdefMessage
5.5 判断是否为NDEF格式标签
若是NDEF格式:
(1)允许进行标签操作:ndef.connect();
(2)调用ndef.writeNdefMessage(NdefMessage)方法写入。
若非NDEF格式:
(1)NdefFromatable format = NdefFromatable.get();
(2)允许进行标签操作:format.connect();
(3)调用format.format(NdefMessage)方法写入。

三、测试代码

import java.io.UnsupportedEncodingException;

import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.os.Parcelable;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

/**
 * 注:谷歌推荐的数据格式为NDEF,所以本次DEMO的读写都是采用该格式.
 */
public class MainActivity extends Activity {
    private TextView ifo_NFC;
    private NfcAdapter nfcAdapter;
    private String readResult = "";
    private PendingIntent pendingIntent;
    private IntentFilter[] mFilters;
    private String[][] mTechLists;
    private boolean isFirst = true;
    private Button toWBtn;
    private IntentFilter ndef;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //该方法完成接收到Intent时的初始化工作
        init(); 
    }

    /**
     * 检测工作,判断设备的NFC支持情况
     * @return
     */
    private Boolean ifNFCUse() {
        if (nfcAdapter == null) {
            ifo_NFC.setText("设备不支持NFC!");
            return false;
        }
        if (nfcAdapter != null && !nfcAdapter.isEnabled()) {
            ifo_NFC.setText("请在系统设置中先启用NFC功能!");
            return false;
        }
        return true;
    }

    /**
     * 初始化过程
     */
    private void init() {
        toWBtn=(Button)findViewById(R.id.toWBtn);
        toWBtn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View arg0) {
                Intent intent=new Intent(MainActivity.this,Write2Nfc.class);
                startActivity(intent);
            }
        });
        ifo_NFC = (TextView) findViewById(R.id.ifo_NFC);
        //NFC适配器,所有的关于NFC的操作从该适配器进行
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        if(!ifNFCUse()){
            return;
        }
        //将被调用的Intent,用于重复被Intent触发后将要执行的跳转
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        //设定要过滤的标签动作,这里只接收ACTION_NDEF_DISCOVERED类型
        ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        ndef.addCategory("*/*");
        mFilters = new IntentFilter[] { ndef };// 过滤器
        mTechLists = new String[][] { new String[] { NfcA.class.getName() },
                new String[] { NfcF.class.getName() },
                new String[] { NfcB.class.getName() },
                new String[] { NfcV.class.getName() } };// 允许扫描的标签类型

        if (isFirst) {
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(getIntent()
                    .getAction())) {
                if (readFromTag(getIntent())) {
                    ifo_NFC.setText(readResult);
                } else {
                    ifo_NFC.setText("标签数据为空");
                }
            }
            isFirst = false;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        nfcAdapter.disableForegroundDispatch(this);
    }

    /* 
     * 重写onResume回调函数的意义在于处理多次读取NFC标签时的情况
     */
    @Override
    protected void onResume() {
        super.onResume();
        // 前台分发系统,这里的作用在于第二次检测NFC标签时该应用有最高的捕获优先权.
        nfcAdapter.enableForegroundDispatch(this, pendingIntent, mFilters,
                mTechLists);
    }

    /*
     * 有必要要了解onNewIntent回调函数的调用时机,请自行上网查询
     */
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())||
                NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
            if (readFromTag(intent)) {
                ifo_NFC.setText(readResult);
            } else {
                ifo_NFC.setText("标签数据为空");
            }
        }
    }

    /**
     * 读取NFC标签数据的操作
     */
    private boolean readFromTag(Intent intent) {
        Parcelable[] rawArray = intent
                .getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
        if (rawArray != null) {
            NdefMessage mNdefMsg = (NdefMessage) rawArray[0];
            NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];
            try {
                if (mNdefRecord != null) {
                    readResult = new String(mNdefRecord.getPayload(), "UTF-8");
                    return true;
                }
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return false;
        }
        return false;
    }

}
import java.io.IOException;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.nfc.FormatException;
import android.nfc.NdefMessage;
import android.nfc.NdefRecord;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.nfc.tech.Ndef;
import android.nfc.tech.NfcA;
import android.nfc.tech.NfcB;
import android.nfc.tech.NfcF;
import android.nfc.tech.NfcV;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 将数据写入NFC标签
 * 注:写入数据关键是弄清数据的封装方法,即NdefRecord和NdefMessage几个关键类,具体可参见开发文档或网上资料
 */
public class Write2Nfc extends Activity {
    private LinearLayout layout;
    private EditText etBqh;
    private EditText etKzh;
    private EditText etXlbw;
//  private EditText editText;
    private TextView noteText;
    private Button wButton;
    private IntentFilter[] mWriteTagFilters;
    private NfcAdapter nfcAdapter;
    PendingIntent pendingIntent;
    String[][] mTechLists;
    private Boolean ifWrite;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.write_ifo);
        init();
        displayControl(false);
    }

    private void init() {
        ifWrite = false;
    //  editText = (EditText) findViewById(R.id.editText);
        layout = (LinearLayout) findViewById(R.id.ll_input);
        etBqh = (EditText) findViewById(R.id.et_bqh);
        etKzh = (EditText) findViewById(R.id.et_kzh);
        etXlbw = (EditText) findViewById(R.id.et_xlbw);
        wButton = (Button) findViewById(R.id.writeBtn);
        noteText=(TextView)findViewById(R.id.noteText);
        wButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                getWriteData();
                String text = getWriteData();
                if (text == "") {
                    Toast.makeText(getApplicationContext(), "数据不能为空!",
                            Toast.LENGTH_SHORT).show();
                    displayControl(false);
                    return;
                }
                ifWrite = true;
                displayControl(true);
            }
        });
        nfcAdapter = NfcAdapter.getDefaultAdapter(this);
        pendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,
                getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
        IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
        ndef.addCategory("*/*");
        mWriteTagFilters = new IntentFilter[] { ndef };
        mTechLists = new String[][] { new String[] { NfcA.class.getName() },
                new String[] { NfcF.class.getName() },
                new String[] { NfcB.class.getName() },
                new String[] { NfcV.class.getName() } };
    }
    public void displayControl(Boolean ifWriting){
        if(ifWriting){          
            noteText.setVisibility(View.VISIBLE);
            layout.setVisibility(View.INVISIBLE);
            wButton.setVisibility(View.INVISIBLE);
            return;
        }
        etBqh.setText("");
        etKzh.setText("");
        etXlbw.setText("");
        noteText.setVisibility(View.INVISIBLE);
        layout.setVisibility(View.VISIBLE);
        wButton.setVisibility(View.VISIBLE);
    }
    @Override
    protected void onResume() {
        super.onResume();
        nfcAdapter.enableForegroundDispatch(this, pendingIntent,
                mWriteTagFilters, mTechLists);
    }

    private String getWriteData() {
        String bqh = etBqh.getText().toString();
        String kzh = etKzh.getText().toString();
        String xlbw = etXlbw.getText().toString();
        if(TextUtils.isEmpty(bqh) || TextUtils.isEmpty(kzh)){
            return "";
        }
        JSONObject json = new JSONObject();
        try {
            json.put("tabNumber", bqh);
            json.put("extensionNumber", kzh);
            json.put("leakLocation", xlbw);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return json.toString();

    }
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        getWriteData();
        String text = getWriteData();
        if (text == "") {
            Toast.makeText(getApplicationContext(), "数据不能为空!",
                    Toast.LENGTH_SHORT).show();
            displayControl(false);
            return;
        }
        if (ifWrite == true) {
            if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intent.getAction())||
                    NfcAdapter.ACTION_TECH_DISCOVERED.equals(intent.getAction())) {
                Tag tag =intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
                Ndef ndef = Ndef.get(tag);
                try {
                    //数据的写入过程一定要有连接操作
                    ndef.connect();
                    //构建数据包,也就是你要写入标签的数据

                    NdefRecord ndefRecord = new NdefRecord(
                            NdefRecord.TNF_MIME_MEDIA, "text/plain".getBytes(),
                            new byte[] {}, text.getBytes());
                    NdefRecord[] records = { ndefRecord };
                    NdefMessage ndefMessage = new NdefMessage(records);
                    ndef.writeNdefMessage(ndefMessage);
                    Toast.makeText(getApplicationContext(), "数据写入成功!",
                            Toast.LENGTH_SHORT).show();
                    displayControl(false);
                } catch (IOException e) {
                    e.printStackTrace();
                } catch (FormatException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/ifo_NFC"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

    <Button
        android:id="@+id/toWBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ifo_NFC"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:text="写入数据" />

</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">
    <LinearLayout 
        android:id="@+id/ll_input"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:gravity="top">
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="标签号:"/>
            <EditText 
                android:id="@+id/et_bqh"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="标签号"/>
        </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="扩展号:"/>
            <EditText 
                android:id="@+id/et_kzh"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="扩展号"/>
        </LinearLayout>
        <LinearLayout 
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="top">
            <TextView 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="泄漏部位:"/>
            <EditText 
                android:id="@+id/et_xlbw"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="泄漏部位"/>
        </LinearLayout>
    </LinearLayout>


    <TextView
        android:id="@+id/noteText"
        android:textSize="30dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="  现在请将设备背面靠近标签" 
        />
    <Button
        android:id="@+id/writeBtn"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/ll_input"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="5dp"
        android:text="开始打入标签" />

</RelativeLayout>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!