android實(shí)現(xiàn)通過NFC讀取卡號
本文實(shí)例為大家分享了android通過NFC讀取卡號的具體代碼,供大家參考,具體內(nèi)容如下
1.獲取權(quán)限
<uses-permission android:name="android.permission.NFC" />
<uses-feature
android:name="android.hardware.nfc"
android:required="true" />
2.設(shè)置NFC活動(dòng)頁
<intent-filter>
<action android:name="android.nfc.action.TAG_DISCOVERED" />
<data android:mimeType="text/plain" />
</intent-filter>
3.Activity代碼
//NFC對象 private NfcAdapter mNfcAdapter; private PendingIntent pi;
1.主方法中:
//獲取默認(rèn)的NFC控制器
//初始化NfcAdapter
mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
if (mNfcAdapter == null) {
Toast.makeText(AddPointActivity.this, "設(shè)備不支持NFC!", Toast.LENGTH_LONG).show();
finish();
return;
}
if (!mNfcAdapter.isEnabled()) {
Toast.makeText(AddPointActivity.this, "請?jiān)谙到y(tǒng)設(shè)置中先啟用NFC功能!", Toast.LENGTH_LONG).show();
finish();
return;
}
//初始化PendingIntent
// 初始化PendingIntent,當(dāng)有NFC設(shè)備連接上的時(shí)候,就交給當(dāng)前Activity處理
pi = PendingIntent.getActivity(this, 0, new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
2.方法
//獲取數(shù)據(jù)
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
// 當(dāng)前app正在前端界面運(yùn)行,這個(gè)時(shí)候有intent發(fā)送過來,那么系統(tǒng)就會(huì)調(diào)用onNewIntent回調(diào)方法,將intent傳送過來
// 我們只需要在這里檢驗(yàn)這個(gè)intent是否是NFC相關(guān)的intent,如果是,就調(diào)用處理方法
if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
processIntent(intent);
}
}
//啟動(dòng)
@Override
protected void onResume() {
super.onResume();
mNfcAdapter.enableForegroundDispatch(this, pi, null, null);
}
//解析
private void processIntent(Intent intent) {
//取出封裝在intent中的TAG
Tag tagFromIntent = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
String CardId =ByteArrayToHexString(tagFromIntent.getId());
Toast.makeText(AddPointActivity.this, CardId, Toast.LENGTH_LONG).show();
}
//轉(zhuǎn)為16進(jìn)制字符串
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;
}
運(yùn)行結(jié)果:

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android中TextureView與SurfaceView用法區(qū)別總結(jié)
TextureView和SurfaceView都是繼承自View類的,TextureView在Andriod4.0之后才引入的,SurfaceView不能加上動(dòng)畫、平移、縮放,TextureView可以但有1-3幀的延遲2018-04-04
Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法
這篇文章主要介紹了Android實(shí)現(xiàn)通過手勢控制圖片大小縮放的方法,結(jié)合實(shí)例形式分析了Android控制圖片縮放的原理、實(shí)現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
Android?RecyclerLineChart實(shí)現(xiàn)圖表繪制教程
這篇文章主要為大家介紹了Android?RecyclerLineChart實(shí)現(xiàn)圖表繪制教程,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Android launcher中模擬按home鍵的實(shí)現(xiàn)
這篇文章主要介紹了Android launcher中模擬按home鍵的實(shí)現(xiàn)的相關(guān)資料,需要的朋友可以參考下2017-05-05
React?Native之在Android上添加陰影的實(shí)現(xiàn)
這篇文章主要介紹了React?Native之在Android上添加陰影的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03
Android ListView實(shí)現(xiàn)上拉加載下拉刷新和滑動(dòng)刪除功能
這篇文章主要為大家詳細(xì)介紹了Android ListView實(shí)現(xiàn)上拉加載下拉刷新和滑動(dòng)刪除功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-12-12

