1. 新建nfc.js,以下代码直接粘贴复制就行
var NfcAdapter;
export default {
// 初始化NFC
NFCInit() {
try {
let main2 = 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 IntentFilter2 = plus.android.importClass('android.content.IntentFilter');
NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
let _nfcAdapter = NfcAdapter.getDefaultAdapter(main2);
let ndef = new IntentFilter2('android.nfc.action.NDEF_DISCOVERED');
let tag = new IntentFilter2('android.nfc.action.TAG_DISCOVERED');
let tech = new IntentFilter2('android.nfc.action.TECH_DISCOVERED');
let intentFiltersArray = [ndef, tag, tech];
let techListsArray = [
['android.nfc.tech.Ndef'],
['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']
];
let _intent = new Intent(main2, main2.getClass());
_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
let pendingIntent = PendingIntent.getActivity(main2, 0, _intent, 0);
if (_nfcAdapter == null) {} else if (_nfcAdapter.isEnabled() == false) {} else {
_nfcAdapter.enableForegroundDispatch(main2, pendingIntent, IntentFilter2, techListsArray);
}
} catch (e) {}
},
// 读取NFC上的数据
NFCReadUID() {
let main3 = plus.android.runtimeMainActivity();
let _intent = main3.getIntent();
let _action = _intent.getAction();
if (NfcAdapter.ACTION_NDEF_DISCOVERED == _action || NfcAdapter.ACTION_TAG_DISCOVERED == _action ||
NfcAdapter.ACTION_TECH_DISCOVERED == _action) {
let Tag = plus.android.importClass('android.nfc.Tag');
let tagFromIntent = _intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
let bind_code = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
bind_code = this.byteArrayToHexString(bind_code);
uni.$emit('nfcCode', {
code: bind_code
})
}
},
// 将转大写格式
byteArrayToHexString(inarray) {
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;
},
}
2. 引入nfc.js,这里根据scanType的值来判断扫描类型,如果类型为二维码扫描,IC卡放到设备上也可触发nfc,但是不能执行,页面也没有变化,点击页面会启动扫描功能;onLoad里面判断如果类型为nfc扫描则初始化nfc,不用点击直接识别IC卡进行业务操作,点击页面也可以通过触发showScan()函数初始化nfc来进行IC卡识别,页面离开需执行NFCReadUID()函数。
<script>
import nfcFun from "../../static/nfc.js";
export default {
data() {
return {
scanType: 0
}
},
onLoad(option) {
// scanType为扫描类型,1二维码扫描,2nfc扫描
this.scanType = uni.getStorageSync('scanType');
if(this.scanType == 2) {
nfcFun.NFCInit(); // 初始化NFC
uni.$on('nfcCode', (data) => {
if (!data.code) {
// 返回数据为空
uni.showToast({
icon: 'none',
title: '未识别到设备!'
});
return false;
} else {
// 返回数据不为空
uni.request({
url: this.$store.state.bussinessUrl,
method: 'post',
success: (data) => {
console.log(data.data.datas)
if(data.data.datas) {
// 执行业务
uni.$off('nfcCode'); //移除监听nfc
} else {
this.messageToggle('暂无该卡号信息,请重新识别!');
return false;
}
},
fail(e) {}
})
}
});
}
},
mounted() {},
methods: {
// 扫描设备
showScan() {
let that = this;
if (that.scanType == 1) {
// 二维码扫描
uni.scanCode({
success: function(res) {
if(res.result) {
// 执行业务
} else {
this.messageToggle('未识别到点位信息!');
return false;
}
}
});
} else if (that.scanType == 2) {
// nfc扫描
uni.showToast({
icon: 'none',
title: '请将IC卡放在设备顶端'
});
// 接收nfc的值
uni.$on('nfcCode', (data) => {
console.log(data.code)
if (!data.code) {
// 返回数据为空
uni.showToast({
icon: 'none',
title: '未识别到设备!'
});
return false;
} else {
// 返回数据不为空
uni.request({
url: that.$store.state.bussinessUrl,
method: 'post',
success: (data) => {
if(data.data.datas) {
// 执行业务
uni.$off('nfcCode'); //移除监听nfc
} else {
that.messageToggle('暂无该卡号信息,请重新识别!');
return false;
}
},
fail(e) {}
})
}
})
}
},
},
//监听离开页面
onHide() {
console.log('监听离开页面');
nfcFun.NFCReadUID();
},
//监听页面销毁
onUnload() {
console.log('监听页面销毁')
}
}
</script>