1.我们先找到这个nfc的插件,:下载地址,也可以直接拿出里面的hexiii-nfc.js文件,楼主就是拿的js文件
2.在js文件中使用uniApp的uni.$emit事件把值传递出去
read(intent) {
// toast('请勿移开标签正在读取数据');
let that = this;
// NFC id
let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
let nfc_id = that.byteArrayToHexString(bytesId);
console.log('nfc_id:', nfc_id);
let Parcelable = plus.android.importClass("android.os.Parcelable");
// let rawmsgs = intent.getParcelableArrayExtra("android.nfc.extra.NDEF_MESSAGES");
let rawmsgs = intent.getParcelableArrayExtra();
// toast('NFC 数据:' + nfc_id);
if(nfc_id){
// 通过uniApp的 $emit方法 将读取到的id广播出去
uni.$emit('nfc_id',{
nfc_id:nfc_id
})
}else{
toast('数据读取失败');
}
2.在需要的页面引入一次
import nfc from '../../static/until/hexiii-nfc.js';
3.在onLoad 函数中使用开启监听
onLoad(options) {
// 开启监听
uni.$on('nfc_id', id => {
if (id.nfc_id !== '') {
// 逻辑代码
this.searchStudent(0, id.nfc_id);
}
});
}
4.在卸载当前页面的时候关闭监听
onUnload() {
// 页面卸载的时候 卸载监听刷ic卡
uni.$off('nfc_id');
},