配色: 字号:
Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
2016-10-15 | 阅:  转:  |  分享 
  
AndroidNFC技术(三)——初次开发AndroidNFC你须知道NdefMessage和NdefRecord

今天,咋们就来用一个小栗子做药引,一起进入AndroidNFC开发的世界,首先,你必须要知道的是这两个类

NdefMessage

NdefRecord

NdefMessage



主要是描述NDEF格式的信息

NdefRecord



这个是秒速NDEF信息的一个信息段

这两个都是AndroidNCF技术的核心类,无论是读写NFC标签还是通过AndroidBeam技术传递数据都需要这两个类

开发步骤



1.获取Tag对象



Tagtag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);



2.判断NFC标签的数格式



Ndefndef=Ndef.get(tag);



3.写入数据



ndef.wrriteNdefMessage(ndefMessage);



准备工作都做好了,我们就直接来写程序了,我们新建一个程序——NFCDemo



我们的需求是这样的:我们的软件把手机上所有安装好的应用排列,然后我们点击一个就开始拿着这个软件,等我们的NFC标签靠近,就把软件写进去,然后,我们每次只要把NFC标签开进有NFC的手机上就会直接运行我们写入的程序了,这个原理有点儿类似门卡,你的先买一个NFC标签



然后我们就开始写了,大致的情况是这样的,我们主页有一个按钮,点击之后跳转到一个界面,是我们手机安装程序的包名列表,我们选中一个回到主Activity,然后等待NFC标签刷入,成功之后,我们就直接用NFC靠近手机就能启动这个程序了,跟门卡登记,然后开门的道理是一样的



主页是这样的



我们点击之后跳转到ListActivity

ListActivity



packagecom.lgl.nfcdemo;



importandroid.content.Intent;

importandroid.content.pm.PackageInfo;

importandroid.content.pm.PackageManager;

importandroid.os.Bundle;

importandroid.view.View;

importandroid.widget.AdapterView;

importandroid.widget.ArrayAdapter;



importjava.util.ArrayList;

importjava.util.List;



/

读取手机软件列表



@authorLGL

/

publicclassListActivityextendsandroid.app.ListActivityimplements

AdapterView.OnItemClickListener{

//返回码

privatestaticfinalintCODE=1;

//封装所有软件

privateListmPackage=newArrayList();





@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

//这里继承了ListActivity显示列表,不需要加载layout



//获取手机上所有的包

PackageManagermanager=getPackageManager();

//把他们装起来

ListpackageInfos=manager.getInstalledPackages(PackageManager.GET_ACTIVITIES);

//遍历

for(PackageInfopi:packageInfos){

//添加软件名和包名

mPackage.add(pi.applicationInfo.loadLabel(manager)+"\n"+pi.packageName);

}



//官方的适配器

ArrayAdapterarrayAdapter=newArrayAdapter(this,android.R.layout.simple_list_item_1,android.R.id.text1,mPackage);

setListAdapter(arrayAdapter);

//设置单击事件

getListView().setOnItemClickListener(this);



}



@Override

publicvoidonItemClick(AdapterViewadapterView,Viewview,inti,longl){



Intentintent=newIntent();

intent.putExtra("package",mPackage.get(i));

setResult(CODE,intent);

finish();

}

}

这段代码应该清晰易懂吧,继承lListActivity,获取手机的应用排列,点击之后携带包名finish();



MainActivity



packagecom.lgl.nfcdemo;



importandroid.app.PendingIntent;

importandroid.content.Intent;

importandroid.nfc.FormatException;

importandroid.nfc.NdefMessage;

importandroid.nfc.NdefRecord;

importandroid.nfc.NfcAdapter;

importandroid.nfc.Tag;

importandroid.nfc.tech.Ndef;

importandroid.os.Bundle;

importandroid.support.v7.app.AppCompatActivity;

importandroid.view.View;

importandroid.widget.Button;

importandroid.widget.Toast;



importcom.google.android.gms.common.api.GoogleApiClient;



importjava.io.IOException;



/

NFC读写

Createdbylglon16/3/1.

/

publicclassMainActivityextendsAppCompatActivity{



privateButtonbtn_list;

//选中的包名

privateStringmPackNmae;

privateNfcAdaptermNfcAdapter;

privatePendingIntentmPendingIntent;



privateGoogleApiClientclient;



@Override

publicvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedIwww.shanxiwang.netnstanceState);

setContentView(R.layout.activity_main);



btn_list=(Button)findViewById(R.id.btn_list);

//初始化NfcAdapter

mNfcAdapter=NfcAdapter.getDefaultAdapter(this);

//初始化PendingIntent

mPendingIntent=PendingIntent.getActivity(this,0,newIntent(this,getClass()),0);





//点击跳转

btn_list=(Button)findViewById(R.id.btn_list);

btn_list.setOnClickListener(newView.OnClickListener(){

@Override

publicvoidonClick(Viewview){

Intentintent=newIntent(MainActivity.this,ListActivity.class);

startActivityForResult(intent,1);

}

});



}



//当设置android:launchMode="singleTop"时调用

@Override

protectedvoidonNewIntent(Intentintent){

super.onNewIntent(intent);

//没有选择的话就不执行操作了

if(mPackNmae==null){

return;

}



//1.获取Tag对象

Tagtag=intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);

//写入程序

writeNFC(tag);





}



//NFC写入

privatevoidwriteNFC(Tagtag){

//null不执行操作,强调写程序的逻辑性

if(tag==null){

return;

}



NdefMessagendefMessage=newNdefMessage(newNdefRecord[]{NdefRecord.createApplicationRecord(mPackNmae)});

//获得写入大小

intsize=ndefMessage.toByteArray().length;

//2.判断是否是NDEF标签

try{

Ndefndef=Ndef.get(tag);

if(ndef!=null){

//说明是NDEF标签,开始连接

ndef.connect();

//判断是否可写

if(!ndef.isWritable()){

Toast.makeText(this,"当前设备不支持写入",Toast.LENGTH_LONG).show();

return;

}

//判断大小

if(ndef.getMaxSize()
Toast.makeText(this,"容量太小了",Toast.LENGTH_LONG).show();

return;

}

//写入

try{

ndef.writeNdefMessage(ndefMessage);

Toast.makeText(this,"写入成功",Toast.LENGTH_LONG).show();

}catch(FormatExceptione){

e.printStackTrace();

}



}

}catch(IOExceptione){

e.printStackTrace();



}

}



//使当前窗口置顶,权限高于三重过滤

@Override

protectedvoidonResume(){

super.onResume();



if(mNfcAdapter!=null){

//设置当前activity为栈顶

mNfcAdapter.enableForegroundDispatch(this,mPendingIntent,null,null);

}

}



@Override

protectedvoidonPause(){

super.onPause();

//恢复栈

if(mNfcAdapter!=null){

mNfcAdapter.disableForegroundDispatch(this);

}

}



}

这里东西多了点,不过仔细看会发现,也就是一些判断,真正的关键代码就那几句,

layout_main.xml






android:layout_width="match_parent"

android:layout_height="match_parent"

android:fitsSystemWindows="true"

android:orientation="vertical">




android:layout_marginTop="15dp"

android:id="@+id/btn_list"

android:layout_width="200dp"

android:layout_height="45dp"

android:text="绑定应用程序"

android:layout_gravity="center_horizontal"/>




android:gravity="center_horizontal"

android:textSize="14sp"

android:text="你可以将NFC标签靠近手机背部"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>




android:layout_weight="1"

android:background="@mipmap/nfc"

android:layout_width="match_parent"

android:layout_height="0dp"/>



我们来简单的运行一下



后我们靠近NFC就算写入成功了,以后我们只要拿着NFC靠近手机就自动打开我们写入的那个程序了,原理和门卡有点类似,下节接着讲NFC和网络交互的那些事

献花(0)
+1
(本文系网络学习天...首藏)