Android NFC

来源:互联网 发布:软件开发求职简历 编辑:程序博客网 时间:2024/04/20 05:39

1、NFC基础知识

NFC(Near Field Communication):一种无线协议,它连接的距离是4cm以内。广泛用于银行卡,信用卡,公交卡,门禁卡等等。

2、NFC与蓝牙,红外对比

这里写图片描述

3、Android与NFC

Android2.3(api=9)开始支持NFC技术,但在android2.x和3.x对NFC的支持非常有限。从android4.x后可以利用NFC技术传递较大的数据(NFC利用蓝牙传输大量数据)
android sdk api主要支持NFC论坛标准,这个标准称为NDEF(NFC Data Exchange Fromat,NFC数据交换格式)。android sdk api支持如下3种NDEF数据的操作

  • 从NFC标签中取出NDEF格式数据。
  • 向NFC标签中写入NDEF格式数据。
  • 通过Android Beam技术将NDEF数据发送给另一个NFC设备

4、NFC的三种过滤机制

在一个NFC设备读取NFC标签或者另 一个NFC设备中的数据之前会有0.1秒之内建立连接,然后数据会自动从被读取一端读取数据的一端。数据接受端会根据具体数据格式和标签类型调用相应的Activity(也称Tag Dispatch)。Activity需要定义Intent Filter。这些Intent Filter中会有指定不同的过滤机制。这就是NFC的三重过滤机制。

这里写图片描述


android匹配流程如下图:

这里写图片描述

5、NFC程序的编写步骤

1、设置权限

<uses-permission android:name="android.permission.NFC" />

2、设置android sdk版本

<uses-sdk android:minSdkVersion="14"/>

3、限制安装设备,如果该设备不支持,则不允许安装应用

<uses-feature android:name="android.hardware.nfc" android:required="true" />

4、定义接收Tag的Activity


5、实例:手机靠近NFC贴纸自动打开网页

需要有支持NFC的手机,还需要上网买NFC贴纸,价格一般2元左右。

1、在清单文件中添加过滤,并且设置activity的启动模式为singleTop

singleTop:如果某个Activity的Launch mode设置成singleTop,那么当该Activity位于栈顶的时候,再通过Intent跳转到本身这个Activity,则将不会创建一个新的实例压入栈中。例如:现在栈的情况为:A B C D。D的Launch mode设置成了singleTop,那么在D中启动Intent跳转到D,那么将不会新创建一个D的实例压入栈中,此时栈的情况依然为:A B C D。但是如果此时B的模式也是singleTop,D跳转到B,那么则会新建一个B的实例压入栈中,因为此时B不是位于栈顶,此时栈的情况就变成了:A B C D B。

当设置这样启动模式的时候
在Activity第一次启动的时候执行到onCreate()-onStart()-onResume()等生命周期,也就是在第一次启动Activity并不会执行onNewIntent(),当再次启动Activity的时候,就不走onCreate()的生命周期,而会走onNewIntent()-onRestart()-onStart()-onResume

  <activity            android:launchMode="singleTop"            android:name=".NFCWriteActivity"            android:label="@string/app_name"            android:theme="@style/AppTheme.NoActionBar">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />                <action android:name="android.nfc.action.NDEF_DISCOVERED"/>                <data android:mimeType="text/plain" />            </intent-filter>        </activity>

2、在activity中代码

public class NFCWriteActivity extends Activity  {    private NfcAdapter nfcAdapter;    private PendingIntent mPendingIntent;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_wirte);        nfcAdapter = NfcAdapter.getDefaultAdapter(this);        mPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this, getClass()), 0);    }    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        Tag datectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);        wirteNfcTag(datectedTag);    }    private void wirteNfcTag(Tag tag) {        if (tag == null) {            return;        }        //将URL写入到NedfMessage中        NdefMessage ndefMessage = new                NdefMessage(new NdefRecord[]{NdefRecord.                createUri(Uri.parse("http://www.baidu.com"))});        Ndef ndef = Ndef.get(tag);        try {            ndef.connect();            //写入到NFC标签中            ndef.writeNdefMessage(ndefMessage);        } catch (Exception e) {            e.printStackTrace();        }        Toast.makeText(NFCWriteActivity.this, "写入url成功", Toast.LENGTH_SHORT).show();    }    @Override    protected void onResume() {        super.onResume();        if (nfcAdapter != null) {          nfcAdapter.enableForegroundDispatch(this,mPendingIntent,null,null);        }    }    @Override    protected void onStop() {        super.onStop();        if(nfcAdapter!=null){            nfcAdapter.disableForegroundDispatch(this);        }    }}

6、NFC读取编码号

代码如下

package com.example.nfc;import android.app.PendingIntent;import android.content.Context;import android.content.Intent;import android.content.IntentFilter;import android.nfc.NfcAdapter;import android.nfc.Tag;import android.os.Bundle;import android.support.v7.app.AppCompatActivity;import android.util.Log;import android.widget.TextView;public class MainActivity extends AppCompatActivity {    private TextView text;    private NfcAdapter mNfcAdapter;    private PendingIntent pi;    private IntentFilter tagDetected;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        text= (TextView) findViewById(R.id.text);        //初始化NfcAdapter        mNfcAdapter = NfcAdapter.getDefaultAdapter(this);        //初始化PendingIntent        // 初始化PendingIntent,当有NFC设备连接上的时候,就交给当前Activity处理        pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())                .addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);        // 新建IntentFilter,使用的是第二种的过滤机制//    tagDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);//    tagDetected.addCategory(Intent.CATEGORY_DEFAULT);    }    @Override    protected void onNewIntent(Intent intent) {        super.onNewIntent(intent);        // 当前app正在前端界面运行,这个时候有intent发送过来,那么系统就会调用onNewIntent回调方法,将intent传送过来        // 我们只需要在这里检验这个intent是否是NFC相关的intent,如果是,就调用处理方法        if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {            processIntent(intent);        }    }    @Override    protected void onResume() {        super.onResume();        if (mNfcAdapter != null) {            mNfcAdapter.enableForegroundDispatch(this,pi,null,null);        }    }    /**     * Parses the NDEF Message from the intent and prints to the TextView     */    private void processIntent(Intent intent) {        //取出封装在intent中的TAG        Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);        String CardId =ByteArrayToHexString(tagFromIntent.getId());        text.setText(CardId);        Log.i("nfc",CardId);    }    public static void startActivity(Context context){        Intent intent = new Intent();        intent.setClass(context,MainActivity.class);        context.startActivity(intent);    }    private String ByteArrayToHexString(byte[] inarray) {        int i, j, in;        String[] hex = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A",                "B", "C", "D", "E", "F" };        String out = "";        for (j = 0; j < inarray.length; ++j) {            in = (int) inarray[j] & 0xff;            i = (in >> 4) & 0x0f;            out += hex[i];            i = in & 0x0f;            out += hex[i];        }        return out;    }}

demo下载地址as版
http://download.csdn.net/detail/androidxiaogang/9508215

1 0