uni-app實(shí)現(xiàn)NFC讀取功能
本文實(shí)例為大家分享了uni-app實(shí)現(xiàn)NFC讀取功能的具體代碼,供大家參考,具體內(nèi)容如下
好久沒(méi)有寫(xiě)博客了,今天難得有空重新記錄自己學(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; //開(kāi)啟讀 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'] ]; // 要寫(xiě)入的數(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: '請(qǐng)?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() { // 輪詢(xún)調(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) { //這里通過(guò)read方法拿到NFC數(shù)據(jù) const id = that.read(intent); // readyRead = false; //將數(shù)據(jù)返回出去 resolve(id) } } }, read(intent) { // toast('請(qǐng)勿移開(kāi)標(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的頁(yè)面調(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: '未找到對(duì)應(yīng)巡檢點(diǎn)!', duration: 2000 }); } else { uni.showToast({ icon: 'none', title: '識(shí)別成功!', duration: 2000 }); uni.navigateTo({ url: `/pages/application/XunJianGuanLi/XunJianRenWu/KaiShiXunJian3/index?id=${this.id}&spotCode=${nfcId}&delta=${this.delta+1}`, }); } }, }
3、頁(yè)面效果圖
4、總結(jié)
以上就是我讀取NFC數(shù)據(jù)的實(shí)現(xiàn)了,根據(jù)用戶(hù)掃描NFC讀取的數(shù)據(jù)自動(dòng)跳轉(zhuǎn)到對(duì)應(yīng)的簽到巡檢點(diǎn)。代碼還有待完善,請(qǐng)多多指導(dǎo),第一部分中NFC的方法,因?yàn)槲抑恍枰x取代碼,所以很多多余的、不用的代碼已被我刪除了,只留下了需要的代碼。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
JavaScript判斷數(shù)組的方法總結(jié)與推薦
這篇文章主要給大家介紹了關(guān)于JavaScript判斷數(shù)組方法的相關(guān)資料,文中通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2022-02-02基于JS實(shí)現(xiàn)簡(jiǎn)單的樣式切換效果代碼
這篇文章主要介紹了基于JS實(shí)現(xiàn)簡(jiǎn)單的樣式切換效果代碼,涉及簡(jiǎn)單的javascript控制頁(yè)面元素樣式變換的技巧,非常簡(jiǎn)單實(shí)用,需要的朋友可以參考下2015-09-09JS實(shí)現(xiàn)關(guān)閉當(dāng)前頁(yè)而不彈出提示框的方法
這篇文章主要介紹了JS實(shí)現(xiàn)關(guān)閉當(dāng)前頁(yè)而不彈出提示框的方法,結(jié)合實(shí)例形式分析了JS操作頁(yè)面的打開(kāi)、關(guān)閉及父頁(yè)面的關(guān)閉技巧,需要的朋友可以參考下2016-06-06javascript replace()正則替換實(shí)現(xiàn)代碼
javascript-replace()基礎(chǔ),一次完成將"<,>"替換"<>"實(shí)例2010-02-02JavaScript嚴(yán)格模式禁用With語(yǔ)句的原因
看了很多遍JavaScript嚴(yán)格模式,其中有說(shuō)“禁用With語(yǔ)句”今天禁不住想知道為何“嚴(yán)格模式”就容不下with語(yǔ)句呢,如果你也表示疑惑可以看看哦2014-10-10