欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

uni-app實現(xiàn)NFC讀取功能

 更新時間:2021年09月18日 11:58:37   作者:Rayong有分享  
這篇文章主要為大家詳細介紹了uni-app實現(xiàn)NFC讀取功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下

本文實例為大家分享了uni-app實現(xiàn)NFC讀取功能的具體代碼,供大家參考,具體內容如下

好久沒有寫博客了,今天難得有空重新記錄自己學習的點點滴滴。

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ù)據
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: '設備不支持NFC!',
     icon: 'none'
    })
    noNFC = true;
    return;
   }
   if (!nfcAdapter.isEnabled()) {
    uni.showToast({
     title: '請在系統(tǒng)設置中先啟用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];

   //重點部分代碼
   const promise = new Promise((resolve, reject) => {
    plus.globalEvent.addEventListener('newintent', function() {
     // 輪詢調用 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ù)據
    const id = that.read(intent);
    // readyRead = false;
    //將數(shù)據返回出去
    resolve(id)
   }
  }
 },
 
 read(intent) {
  // toast('請勿移開標簽正在讀取數(shù)據');
  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的頁面調用方法

<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感應
  </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ù)據
   const nfcId = await testtest.listenNFCStatus();
   //console.log(nfcId )

   //以下數(shù)據是我的業(yè)務邏輯代碼,如果只要讀取NFC數(shù)據上面那一行代碼即可了。
   const arr = []
   this.list2.forEach(e => {
    arr.push(e.code)
   })
   if(!nfcId) return
   if ( arr.indexOf(nfcId) === -1) {
    uni.showToast({
     icon: 'none',
     title: '未找到對應巡檢點!',
     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、總結

以上就是我讀取NFC數(shù)據的實現(xiàn)了,根據用戶掃描NFC讀取的數(shù)據自動跳轉到對應的簽到巡檢點。代碼還有待完善,請多多指導,第一部分中NFC的方法,因為我只需要讀取代碼,所以很多多余的、不用的代碼已被我刪除了,只留下了需要的代碼。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • 第六篇Bootstrap表格樣式介紹

    第六篇Bootstrap表格樣式介紹

    這篇文章主要介紹了第六篇Bootstrap表格樣式介紹的相關資料,非常不錯,具有參考借鑒價值,需要的朋友可以參考下
    2016-06-06
  • JavaScript判斷數(shù)組的方法總結與推薦

    JavaScript判斷數(shù)組的方法總結與推薦

    這篇文章主要給大家介紹了關于JavaScript判斷數(shù)組方法的相關資料,文中通過實例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2022-02-02
  • 基于JS實現(xiàn)簡單的樣式切換效果代碼

    基于JS實現(xiàn)簡單的樣式切換效果代碼

    這篇文章主要介紹了基于JS實現(xiàn)簡單的樣式切換效果代碼,涉及簡單的javascript控制頁面元素樣式變換的技巧,非常簡單實用,需要的朋友可以參考下
    2015-09-09
  • Js獲取數(shù)組最大和最小值示例代碼

    Js獲取數(shù)組最大和最小值示例代碼

    做項目的時候遇到一個返回查詢內容里面,只取最大和最小值問題,下面有個不錯的示例,感興趣的朋友可以參考下
    2013-10-10
  • JS實現(xiàn)關閉當前頁而不彈出提示框的方法

    JS實現(xiàn)關閉當前頁而不彈出提示框的方法

    這篇文章主要介紹了JS實現(xiàn)關閉當前頁而不彈出提示框的方法,結合實例形式分析了JS操作頁面的打開、關閉及父頁面的關閉技巧,需要的朋友可以參考下
    2016-06-06
  • 手機瀏覽器喚起微信分享(JS)

    手機瀏覽器喚起微信分享(JS)

    這篇文章主要介紹了手機瀏覽器喚起微信分享功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-10-10
  • javascript replace()正則替換實現(xiàn)代碼

    javascript replace()正則替換實現(xiàn)代碼

    javascript-replace()基礎,一次完成將"<,>"替換"&lt;&gt;"實例
    2010-02-02
  • 淺談javascript原型鏈與繼承

    淺談javascript原型鏈與繼承

    這篇文章主要介紹了淺談javascript原型鏈與繼承的相關資料,需要的朋友可以參考下
    2015-07-07
  • JavaScript嚴格模式禁用With語句的原因

    JavaScript嚴格模式禁用With語句的原因

    看了很多遍JavaScript嚴格模式,其中有說“禁用With語句”今天禁不住想知道為何“嚴格模式”就容不下with語句呢,如果你也表示疑惑可以看看哦
    2014-10-10
  • JavaScript常用事件介紹

    JavaScript常用事件介紹

    今天小編就為大家分享一篇關于JavaScript常用事件介紹,小編覺得內容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧
    2019-01-01

最新評論