uni-app實(shí)現(xiàn)NFC讀取功能
本文實(shí)例為大家分享了uni-app實(shí)現(xiàn)NFC讀取功能的具體代碼,供大家參考,具體內(nèi)容如下
好久沒有寫博客了,今天難得有空重新記錄自己學(xué)習(xí)的點(diǎn)點(diǎn)滴滴。
1、NFC方法.js
// 包路徑
const package_NdefRecord = 'android.nfc.NdefRecord';
const package_NdefMessage = 'android.nfc.NdefMessage';
const package_TECH_DISCOVERED = 'android.nfc.action.TECH_DISCOVERED';
const package_Intent = 'android.content.Intent';
const package_Activity = 'android.app.Activity';
const package_PendingIntent = 'android.app.PendingIntent';
const package_IntentFilter = 'android.content.IntentFilter';
const package_NfcAdapter = 'android.nfc.NfcAdapter';
const package_Ndef = 'android.nfc.tech.Ndef';
const package_NdefFormatable = 'android.nfc.tech.NdefFormatable';
const package_Parcelable = 'android.os.Parcelable';
const package_String = 'java.lang.String';
let NfcAdapter;
let NdefRecord;
let NdefMessage;
let readyRead = true; //開啟讀
let noNFC = false;
let techListsArray = [
['android.nfc.tech.IsoDep'],
['android.nfc.tech.NfcA'],
['android.nfc.tech.NfcB'],
['android.nfc.tech.NfcF'],
['android.nfc.tech.Nfcf'],
['android.nfc.tech.NfcV'],
['android.nfc.tech.NdefFormatable'],
['android.nfc.tech.MifareClassi'],
['android.nfc.tech.MifareUltralight']
];
// 要寫入的數(shù)據(jù)
let text = '{id:8888,name:nfc,stie:wangqin.com}';
let readResult = '';
export default {
listenNFCStatus: function() {
console.log("---------listenNFCStatus--------------")
let that = this;
try {
let main = plus.android.runtimeMainActivity();
let Intent = plus.android.importClass('android.content.Intent');
let Activity = plus.android.importClass('android.app.Activity');
let PendingIntent = plus.android.importClass('android.app.PendingIntent');
let IntentFilter = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
let nfcAdapter = NfcAdapter.getDefaultAdapter(main);
if (nfcAdapter == null) {
uni.showToast({
title: '設(shè)備不支持NFC!',
icon: 'none'
})
noNFC = true;
return;
}
if (!nfcAdapter.isEnabled()) {
uni.showToast({
title: '請?jiān)谙到y(tǒng)設(shè)置中先啟用NFC功能!',
icon: 'none'
});
noNFC = true;
return;
} else {
noNFC = false;
}
let intent = new Intent(main, main.getClass());
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
let pendingIntent = PendingIntent.getActivity(main, 0, intent, 0);
let ndef = new IntentFilter("android.nfc.action.TECH_DISCOVERED");
ndef.addDataType("*/*");
let intentFiltersArray = [ndef];
//重點(diǎn)部分代碼
const promise = new Promise((resolve, reject) => {
plus.globalEvent.addEventListener('newintent', function() {
// 輪詢調(diào)用 NFC
// setTimeout(that.nfcRuning(resolve), 1000);
setTimeout(() => {
that.nfcRuning(resolve)
}, 1000);
});
})
nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
return promise
} catch (e) {
}
},
nfcRuning: function(resolve) {
// console.log("--------------nfcRuning---------------")
NdefRecord = plus.android.importClass("android.nfc.NdefRecord");
NdefMessage = plus.android.importClass("android.nfc.NdefMessage");
let main = plus.android.runtimeMainActivity();
let intent = main.getIntent();
let that = this;
if (package_TECH_DISCOVERED == intent.getAction()) {
if (readyRead) {
//這里通過read方法拿到NFC數(shù)據(jù)
const id = that.read(intent);
// readyRead = false;
//將數(shù)據(jù)返回出去
resolve(id)
}
}
},
read(intent) {
// toast('請勿移開標(biāo)簽正在讀取數(shù)據(jù)');
let that = this;
// NFC id
let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
let nfc_id = that.byteArrayToHexString(bytesId);
return nfc_id;
},
byteArrayToHexString: function(inarray) { // converts byte arrays to string
let i, j, inn;
let hex = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F"];
let out = "";
for (j = 0; j < inarray.length; ++j) {
inn = inarray[j] & 0xff;
i = (inn >>> 4) & 0x0f;
out += hex[i];
i = inn & 0x0f;
out += hex[i];
}
return out;
},
}
function toast(content) {
uni.showToast({
title: content,
icon: 'none'
})
}
2、在用NFC的頁面調(diào)用方法
<view class="flex nfc-ewm">
<view class="nfc-ewm-item" style="border-right: 1px solid #ccc;" @click="testNFC">
<image src="@/assets/images/application/icon-nfc.png" alt=""></image>NFC感應(yīng)
</view>
<view class="nfc-ewm-item" @click="openScanCode">
<image src="@/assets/images/application/icon-ewm.png" alt=""></image>二維碼掃描
</view>
</view>
import testtest from "../../../../../components/hexiii-nfc/hexiii-nfc.js"
methods:{
async testNFC() {
//這里用異步獲取讀取到的NFC數(shù)據(jù)
const nfcId = await testtest.listenNFCStatus();
//console.log(nfcId )
//以下數(shù)據(jù)是我的業(yè)務(wù)邏輯代碼,如果只要讀取NFC數(shù)據(jù)上面那一行代碼即可了。
const arr = []
this.list2.forEach(e => {
arr.push(e.code)
})
if(!nfcId) return
if ( arr.indexOf(nfcId) === -1) {
uni.showToast({
icon: 'none',
title: '未找到對應(yīng)巡檢點(diǎn)!',
duration: 2000
});
} else {
uni.showToast({
icon: 'none',
title: '識別成功!',
duration: 2000
});
uni.navigateTo({
url: `/pages/application/XunJianGuanLi/XunJianRenWu/KaiShiXunJian3/index?id=${this.id}&spotCode=${nfcId}&delta=${this.delta+1}`,
});
}
},
}
3、頁面效果圖

4、總結(jié)
以上就是我讀取NFC數(shù)據(jù)的實(shí)現(xiàn)了,根據(jù)用戶掃描NFC讀取的數(shù)據(jù)自動(dòng)跳轉(zhuǎn)到對應(yīng)的簽到巡檢點(diǎn)。代碼還有待完善,請多多指導(dǎo),第一部分中NFC的方法,因?yàn)槲抑恍枰x取代碼,所以很多多余的、不用的代碼已被我刪除了,只留下了需要的代碼。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JS實(shí)現(xiàn)換膚功能的方法實(shí)例詳解
這篇文章主要介紹了JS實(shí)現(xiàn)換膚功能的方法,結(jié)合實(shí)例形式分析了javascript針對頁面元素屬性與樣式動(dòng)態(tài)操作相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2019-01-01
javascript實(shí)現(xiàn)循環(huán)廣告條效果
這篇文章主要為大家詳細(xì)介紹了javascript實(shí)現(xiàn)循環(huán)廣告條效果,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
在線編輯器的實(shí)現(xiàn)原理(兼容IE和FireFox)
在線編輯器的實(shí)現(xiàn)原理(兼容IE和FireFox)...2007-03-03
js 數(shù)值轉(zhuǎn)換為3位逗號分隔的示例代碼
本篇文章主要是對js將數(shù)值轉(zhuǎn)換為3位逗號分隔的示例代碼進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-02-02
JavaScript如何實(shí)現(xiàn)數(shù)組內(nèi)的值累加
我們會經(jīng)常在開發(fā)過程中,需要獲取數(shù)組中的值累加,所以下面這篇文章主要給大家介紹了關(guān)于JavaScript如何實(shí)現(xiàn)數(shù)組內(nèi)的值累加的相關(guān)資料,文中給出了詳細(xì)的代碼示例,需要的朋友可以參考下2023-11-11
input輸入框限制只能輸入數(shù)字的方法實(shí)例(個(gè)人認(rèn)為最好的)
在很多業(yè)務(wù)中需要對輸入框進(jìn)行字符限制,比如金額輸入框、手機(jī)號碼輸入框等,下面這篇文章主要給大家介紹了關(guān)于input輸入框限制只能輸入數(shù)字的相關(guān)資料,文中介紹的方法個(gè)人認(rèn)為最好的,需要的朋友可以參考下2022-10-10
js Firefox 加入收藏夾功能代碼 兼容Firefox 和 IE
最近改用Firefox后,發(fā)現(xiàn)很多網(wǎng)站的“加入收藏”鏈接點(diǎn)擊無效了,后來發(fā)現(xiàn)原來是IE瀏覽器和Firefox瀏覽器的“加入收藏夾”的寫法是不同的。2009-12-12
如何使用require.context實(shí)現(xiàn)優(yōu)雅的預(yù)加載
這篇文章主要介紹了使用require.context實(shí)現(xiàn)優(yōu)雅的預(yù)加載?,需要的朋友可以參考下2023-05-05

