Android實(shí)現(xiàn)讀取NFC卡的編號(hào)
本文實(shí)例為大家分享了Android讀取NFC卡的編號(hào)具體代碼,供大家參考,具體內(nèi)容如下
NFC相關(guān)androidManifest文件設(shè)置:
一、權(quán)限:<uses-permission android:name="android.permission.NFC"/>
二、sdk級(jí)別限制:<uses-sdk android:minSdkVersion="10"/>
三、特殊功能限制<uses-feature android:name="android.hardware.nfc" android:required="true" />這個(gè)生命可以讓你的應(yīng)用在google play上被聲明使用者必須擁有nfc功能。
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nfc"
android:versionCode="1"
android:versionName="1.0" >
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="17" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:launchMode="singleTask"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.nfc.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<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" />
</activity>
</application>
</manifest>
上面的android:resource="@xml/nfc_tech_filter"是對(duì)tech類(lèi)型的過(guò)濾條件,在res文件夾新建一個(gè)xml文件夾,新建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>
下面是封裝了讀取NFC卡號(hào)的一個(gè)類(lèi):
class NFCCard {
private Activity context;
private PendingIntent pendingIntent;
private NfcAdapter adapter;
private IntentFilter[] intentFilters;
private String[][] techLists;
private final char[] HEX_EXCHANGE = { '0', '1', '2', '3', '4', '5', '6',
'7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
public NFCCard(Context context) {
this.context = (Activity) context;
}
void init() {
adapter = NfcAdapter.getDefaultAdapter(context);
// 創(chuàng)建一個(gè)PendingIntent對(duì)象,當(dāng)Android系統(tǒng)掃描到標(biāo)簽時(shí),則會(huì)填充到這個(gè)對(duì)象。
pendingIntent = PendingIntent.getActivity(context, 0, new Intent(
context, context.getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter ndef = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
try {
ndef.addDataType("*/*");
} catch (MalformedMimeTypeException e) {
throw new RuntimeException("fail", e);
}
intentFilters = new IntentFilter[] { ndef, };
techLists = new String[][] { new String[] { MifareClassic.class
.getName() } };
}
public boolean open() {
// TODO 打開(kāi)NfcAdapter,已打開(kāi)則返回true。應(yīng)在onResume()中調(diào)用
if (adapter != null)
adapter.enableForegroundDispatch(context, pendingIntent,
intentFilters, techLists);
return adapter.isEnabled();
}
public boolean close() {
// TODO 關(guān)閉NfcAdapter,已關(guān)閉則返回true
if (adapter != null)
adapter.disableForegroundDispatch(context);
return !adapter.isEnabled();
}
public String getId(Intent intent) {
// TODO 獲取NFC卡的編號(hào)。應(yīng)在onNewIntent()中調(diào)用
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
return toHexString(tagFromIntent.getId(), 0,
tagFromIntent.getId().length);
}
private String toHexString(byte[] d, int s, int n) {
// TODO 轉(zhuǎn)換為十六進(jìn)制形式的字符串
final char[] ret = new char[n * 2];
final int e = s + n;
int x = 0;
for (int i = s; i < e; ++i) {
final byte v = d[i];
ret[x++] = HEX_EXCHANGE[0x0F & (v >> 4)];
ret[x++] = HEX_EXCHANGE[0x0F & v];
}
return new String(ret);
}
需要注意的是,上面配置文件中有一個(gè)“ android:launchMode="singleTask"”,這是設(shè)置應(yīng)用為單任務(wù)。代碼中的getId(Intent intent)必須要在Activity的onNewIntent(Intent intent)中執(zhí)行。因?yàn)橄到y(tǒng)檢測(cè)到NFC卡的時(shí)候,會(huì)自動(dòng)生成封裝了相應(yīng)Tag的Intent,當(dāng)應(yīng)用在接收到Intent的時(shí)候,默認(rèn)情況下是啟動(dòng)自己的Activity,這樣就會(huì)致使每次接收到Intent都會(huì)啟動(dòng)新的Activity。把應(yīng)用設(shè)置為單任務(wù)后,就可以避免這種情況的發(fā)生。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android設(shè)備之間通過(guò)Wifi通信的示例代碼
本篇文章主要介紹了Android設(shè)備之間通過(guò)Wifi通信的示例代碼,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2018-01-01
詳解Android中Intent傳遞對(duì)象給Activity的方法
這篇文章主要介紹了Android中Intent傳遞對(duì)象給Activity的方法,文章中對(duì)Activity的生命周期等知識(shí)先作了簡(jiǎn)要的介紹,需要的朋友可以參考下2016-04-04
Android Fragment動(dòng)態(tài)創(chuàng)建詳解及示例代碼
這篇文章主要介紹了Android Fragment動(dòng)態(tài)創(chuàng)建詳解的相關(guān)資料,并附實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2016-11-11
Android_RecyclerView實(shí)現(xiàn)上下滾動(dòng)廣告條實(shí)例(帶圖片)
本篇文章主要介紹了Android_RecyclerView實(shí)現(xiàn)上下滾動(dòng)廣告條實(shí)例(帶圖片),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
Android實(shí)現(xiàn)志愿者系統(tǒng)詳細(xì)步驟與代碼
這篇文章主要介紹了Android實(shí)現(xiàn)志愿者系統(tǒng),本系統(tǒng)采用MVC架構(gòu)設(shè)計(jì),SQLite數(shù)據(jù)表有用戶表、成員表和活動(dòng)表,有十多個(gè)Activity頁(yè)面。打開(kāi)應(yīng)用,進(jìn)入歡迎界面,3s后跳轉(zhuǎn)登錄界面,用戶先注冊(cè)賬號(hào),登錄成功后進(jìn)入主界面2023-02-02
android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸(1)
這篇文章主要為大家詳細(xì)介紹了android自定義控件實(shí)現(xiàn)簡(jiǎn)易時(shí)間軸,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-01-01
android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,支持多種機(jī)型
這篇文章主要介紹了android實(shí)現(xiàn)藍(lán)牙文件發(fā)送的實(shí)例代碼,有需要的朋友可以參考一下2014-01-01
android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻
這篇文章主要為大家詳細(xì)介紹了android實(shí)現(xiàn)播放網(wǎng)絡(luò)視頻,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-04-04
安卓(Android)實(shí)現(xiàn)選擇時(shí)間功能
安卓開(kāi)發(fā)過(guò)程中難免會(huì)碰到需要選擇日期時(shí)間的情況,當(dāng)然不可能讓用戶自己輸入日期時(shí)間,小編收集整理了一些資料,總結(jié)了一下如何實(shí)現(xiàn)android選擇時(shí)間的功能,方便后來(lái)者參考2016-08-08

