在Activity中获取TAGID;示例代码:;Intentintent=this.getInt;//确保你的Activity是被扫描到的NFC的;StringintentActionStr=in;if(NfcAdapter.ACTION_NDE;||NfcAdapter.ACTION_TECH;||NfcAdapter.ACTION_TAG_;//在int
在Activity中获取TAG ID
示例代码:
Intent intent =this.getIntent();//在activity中获取Intent对象
// 确保你的Activity是被扫描到的NFC的Intent对象启动的
String intentActionStr = intent.getAction();// 获取到本次启动的action
if (NfcAdapter.ACTION_NDEF_DISCOVERED.equals(intentActionStr)// NDEF类型
|| NfcAdapter.ACTION_TECH_DISCOVERED.equals(intentActionStr)// 其他类型
|| NfcAdapter.ACTION_TAG_DISCOVERED.equals(intentActionStr)) {// 未知类型
//在intent中读取Tag id
byte[] bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)
/*
//Android NFC API 10 以后对Tag 对象加入方法getID来获取id,所以还可以 // 在intent中通过附加信息读取Tag标签对象,即获取扫描到的标签 //然后通过Tag对象读取id Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);// // 获取id数组
byte[] bytesId = tag.getId(); */ // 字符串id String strId = new String(dataId);
}else{
//activity不是NFC intent启动的后者收到的intent不是NFC扫描到Tag后传入的
}
使用主要到的api:
intent.getByteArrayExtra(NfcAdapter.EXTRA_ID)// 在intent中读取Tag Id 数组
或
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);//在intent中读取Tag
tag.getId();// 获取Tag Id数组
介绍:
当NFC检测到NFC标签(Tag)时,同过使用Intent启动适应的Activity。在Acitivity中可以从这个Intent中获取到被扫描到的NFC标签的信息。根据被扫描到的标签,Intent对象能够以下额外的信息:
1. EXTRA_TAG(必须的):它是一个代表了被扫描到的标签的Tag对象;
2. EXTRA_NDEF_MESSAGES(可选):它是一个解析来自标签中的NDEF消息的数组。这个附加信息是强制在Intent对象上的;
3. EXTRA_ID(可选):标签的低级ID。(注:这个id是一个低级别的编号,用于防碰撞和鉴定, 大多数标签都有一个稳定的唯一标识符(UID),但有一些标签在每次发现他们时将生成一个随机的ID(RID),还有一
些根本就没有ID的标签(字节数组将是零大小)。这个值不可能为NULL。)
配置Activity处理相应NFC 标签(Tag)
NFC标签的类型有很少种,需要指出你的activity处理的nfc标签类型,配置的方法有两种:
1. Activity不在前台
当NFC标签被扫描到系统会依照顺序NDEF_DISCOVERED, TECH_DISCOVERED,
TAG_DISCOVERED匹配标签类型,直到找到适合的Acitivty处理或者没有处理为止,如果有传递带有标签信息的Intent到处理的Activity. (NFC标签调度系统)
1) NDEF_DISCOVERED 在AndroidManifest.xml对Activity配置如下:
<intent-filter>
<action android:name="android.nfc.action.NDEF_DISCOVERED"/> </intent-filter>
2) TECH_DISCOVERED
TECH_DISCOVERED要求你在一个资源文件里(xml)里为你的Activity指定你要支持technologies列表。
A. 配置需要处理的technologies列表,并以xml文件格式保存在res/xml下.
如:文件名/res/ nfc_tech_filter.xml,文件支持所有格式的文件内容列表为:
<resources xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<tech-list>
<tech>android.nfc.tech.IsoDep</tech>
<tech>android.nfc.tech.NfcA</tech>
<tech>android.nfc.tech.NfcB</tech>
<tech>android.nfc.tech.NfcF</tech>
<tech>android.nfc.tech.NfcV</tech>
<tech>android.nfc.tech.Ndef</tech>
<tech>android.nfc.tech.NdefFormatable</tech>
<tech>android.nfc.tech.MifareClassic</tech>
<tech>android.nfc.tech.MifareUltralight</tech>
</tech-list>
</resources>
B. 在AndroidManifest.xml对Activity配置,
<intent-filter>
<action android:name="android.nfc.action.TECH_DISCOVERED"/>
</intent-filter>
<meta-data android:name="android.nfc.action.TECH_DISCOVERED"
android:resource="@xml/nfc_tech_filter" />
<!—此xml资源为定义的支持的technologies列表--!>
3) TAG_DISCOVERED在AndroidManifest.xml对Activity配置如下:
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED"/>
</intent-filter>
2. Activity在前台
当Activity在前台时,可直接拦截一个tag Intent 获得最高优先级的处理.( 前台调度系统)
示例代码:
private NfcAdapter mAdapter;//NFC适配器
private PendingIntent mPendingIntent;//
private IntentFilter[] mFilters;//Intent过滤器
private String[][] mTechLists;// technologies列表
@Override
public void onCreate(Bundle savedState) {
super.onCreate(savedState);
…
//获取NFC适配器
mAdapter = NfcAdapter.getDefaultAdapter(this);
mPendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
//intent过滤器,过滤类型为NDEF_DISCOVERED
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
mFilters = new IntentFilter[] {ndef,};
//存放支持technologies的数组
mTechLists = new String[][] { new String[] { NfcF.class.getName() } };
}
@Override
public void onResume() {
super.onResume();
... //打开功能,使用上面设置的值
mAdapter.enableForegroundDispatch(this, mPendingIntent, mFilters, mTechLists);
}
@Override
public void onPause() {
super.onPause();
…
//关闭功能
mAdapter.disableForegroundDispatch(this);
}
使用主要到的api:
NfcAdapter.getDefaultAdapter(Content content) 本地默认的NFC适配器的引用
NfcAdapter .enableForegroundDispatch(Activity, PendingIntent, IntentFilter[], String[][]) 打开前台发布系统,主线程里调用并且Activity在前台
NfcAdapter.disableForegroundDispatch(Content content) 关闭前台发布系统
声明Android Manifest.xml的元素
使用NFC需要申明权限:
<uses-permission android:name="android.permission.NFC" />
如需要在应用商店过滤:
<uses-feature android:name="android.hardware.nfc" android:required="true" />
三亿文库3y.uu456.com包含各类专业文献、幼儿教育、小学教育、生活休闲娱乐、中学教育、文学作品欣赏、Android NFC Tag ID 读取18等内容。