android實(shí)現(xiàn)NFC讀寫功能
一、NFC是什么?
近距離無線通訊技術(shù),這個(gè)技術(shù)由非接觸式射頻識(shí)別(RFID)演變而來,由飛利浦半導(dǎo)體(現(xiàn)恩智浦半導(dǎo)體公司)、諾基亞和索尼共同研制開發(fā),其基礎(chǔ)是RFID及互連技術(shù)。近場通信(Near Field Communication,NFC)是一種短距高頻的無線電技術(shù),在13.56MHz頻率運(yùn)行于20厘米距離內(nèi)。其傳輸速度有106 Kbit/秒、212 Kbit/秒或者424 Kbit/秒三種。目前近場通信已通過成為ISO/IEC IS 18092國際標(biāo)準(zhǔn)、ECMA-340標(biāo)準(zhǔn)與ETSI TS 102 190標(biāo)準(zhǔn)。NFC采用主動(dòng)和被動(dòng)兩種讀取模式。
NFC通信模式主要有以下幾種(信息來源):
1.讀卡器模式(Reader/writer mode):
作為非接觸讀卡器使用,比如從海報(bào)或者展覽信息電子標(biāo)簽上讀取相關(guān)信息。亦可實(shí)現(xiàn)NFC手機(jī)之間的數(shù)據(jù)交換,對于企業(yè)環(huán)境的中的文件共享,或者對于多玩家的游戲應(yīng)用,都將帶來諸多的便利。
2. 點(diǎn)對點(diǎn)模式(P2Pmode):
此模式和紅外線差不多,可用于數(shù)據(jù)交換,只是傳輸距離較短,傳輸創(chuàng)建速度較快,傳輸速度也快些,功耗低(藍(lán)牙也類似)。將兩個(gè)具備NFC功能的設(shè)備無線鏈接,能實(shí)現(xiàn)數(shù)據(jù)點(diǎn)對點(diǎn)傳輸,如下載音樂、交換圖片或者同步設(shè)備地址薄。因此通過NFC,多個(gè)設(shè)備如數(shù)位相機(jī)、PDA、計(jì)算機(jī)和手機(jī)之間都可以交換資料或者服務(wù)。
3.卡模式(Cardemulation):
這個(gè)模式其實(shí)就是相當(dāng)于一張采用RFID技術(shù)的IC卡,可以替代大量的IC卡(包括信用卡)使用的場合,如商場刷卡、公交卡、門禁管制,車票,門票等等。此種方式下,有一個(gè)極大的優(yōu)點(diǎn),那就是卡片通過非接觸讀卡器的 RF 域來供電,即使寄主設(shè)備(如手機(jī))沒電也可以工作。
二、如何使用與集成到項(xiàng)目?
1、首先在manifests里面聲明NFC和添加相應(yīng)的權(quán)限;
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
<uses-permission android:name="android.permission.NFC" />
2、在Activity標(biāo)簽中聲明識(shí)別NFC標(biāo)簽;
<activity android:name=".Activity.Main.NFCActivity">
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="*/*" />
</intent-filter>
</activity>
3、封裝NFC的讀寫,方便調(diào)用;
public class NfcUtils {
//nfc
public static NfcAdapter mNfcAdapter;
public static IntentFilter[] mIntentFilter = null;
public static PendingIntent mPendingIntent = null;
public static String[][] mTechList = null;
/**
* 構(gòu)造函數(shù),用于初始化nfc
*/
public NfcUtils(Activity activity) {
mNfcAdapter = NfcCheck(activity);
NfcInit(activity);
}
/**
* 檢查NFC是否打開
*/
public static NfcAdapter NfcCheck(Activity activity) {
NfcAdapter mNfcAdapter = NfcAdapter.getDefaultAdapter(activity);
if (mNfcAdapter == null) {
return null;
} else {
if (!mNfcAdapter.isEnabled()) {
Intent setNfc = new Intent(Settings.ACTION_NFC_SETTINGS);
activity.startActivity(setNfc);
}
}
return mNfcAdapter;
}
/**
* 初始化nfc設(shè)置
*/
public static void NfcInit(Activity activity) {
mPendingIntent = PendingIntent.getActivity(activity, 0, new Intent(activity, activity.getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
IntentFilter filter = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
IntentFilter filter2 = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
try {
filter.addDataType("*/*");
} catch (IntentFilter.MalformedMimeTypeException e) {
e.printStackTrace();
}
mIntentFilter = new IntentFilter[]{filter, filter2};
mTechList = null;
}
/**
* 讀取NFC的數(shù)據(jù)
*/
public static String readNFCFromTag(Intent intent) throws UnsupportedEncodingException {
Parcelable[] rawArray = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
if (rawArray != null) {
NdefMessage mNdefMsg = (NdefMessage) rawArray[0];
NdefRecord mNdefRecord = mNdefMsg.getRecords()[0];
if (mNdefRecord != null) {
String readResult = new String(mNdefRecord.getPayload(), "UTF-8");
return readResult;
}
}
return "";
}
/**
* 往nfc寫入數(shù)據(jù)
*/
public static void writeNFCToTag(String data, Intent intent) throws IOException, FormatException {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
Ndef ndef = Ndef.get(tag);
ndef.connect();
NdefRecord ndefRecord = NdefRecord.createTextRecord(null, data);
NdefRecord[] records = {ndefRecord};
NdefMessage ndefMessage = new NdefMessage(records);
ndef.writeNdefMessage(ndefMessage);
}
/**
* 讀取nfcID
*/
public static String readNFCId(Intent intent) throws UnsupportedEncodingException {
Tag tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String id = ByteArrayToHexString(tag.getId());
return id;
}
/**
* 將字節(jié)數(shù)組轉(zhuǎn)換為字符串
*/
private static 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;
}
}
4、在NFCActivity代碼中的使用、使用標(biāo)簽的前臺(tái)調(diào)度系統(tǒng);
@Override
public void initData() {
//nfc初始化設(shè)置
NfcUtils nfcUtils = new NfcUtils(this);
}
@Override
protected void onResume() {
super.onResume();
//開啟前臺(tái)調(diào)度系統(tǒng)
NfcUtils.mNfcAdapter.enableForegroundDispatch(this, NfcUtils.mPendingIntent, NfcUtils.mIntentFilter, NfcUtils.mTechList);
}
@Override
protected void onPause() {
super.onPause();
//關(guān)閉前臺(tái)調(diào)度系統(tǒng)
NfcUtils.mNfcAdapter.disableForegroundDispatch(this);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
//當(dāng)該Activity接收到NFC標(biāo)簽時(shí),運(yùn)行該方法
//調(diào)用工具方法,讀取NFC數(shù)據(jù)
String str = NfcUtils.rendFromTag(intent);
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android編程實(shí)現(xiàn)抽屜效果的方法示例
這篇文章主要介紹了Android編程實(shí)現(xiàn)抽屜效果的方法,結(jié)合具體實(shí)例形式分析了Android抽屜效果的布局、功能實(shí)現(xiàn)及相關(guān)注意事項(xiàng),需要的朋友可以參考下2017-06-06
Android 中利用 ksoap2 調(diào)用 WebService的示例代碼
這篇文章主要介紹了Android 中利用 ksoap2 調(diào)用 WebService的示例代碼,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2017-09-09
實(shí)例探究Android應(yīng)用編寫時(shí)Fragment的生命周期問題
這篇文章主要介紹了Android應(yīng)用編寫時(shí)Fragment的生命周期問題探究,resumed和paused以及stoped三種狀態(tài)的控制需要熟練掌握,需要的朋友可以參考下2016-02-02
Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出)
這篇文章主要介紹了Android Studio finish()方法的使用與解決app點(diǎn)擊“返回”(直接退出),本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04
Android自定義FloatingText仿點(diǎn)贊+1特效
這篇文章主要為大家詳細(xì)介紹了Android自定義FloatingText仿點(diǎn)贊+1特效,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03

