uniapp调用nfc

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档


uniapp调用nfc读取uid

一、使用步骤

1.引入库

代码如下(示例):

// 包路径
	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 readyWriteData = false; //开启写
	let readyRead = false; //开启读
	let noNFC = false;
	let techListsArray = [
		['android.nfc.tech.IsoDep'],
		['android.nfc.tech.NfcA'],
		['android.nfc.tech.NfcB'],
		['android.nfc.tech.NfcF'],
		['android.nfc.tech.NfcV'],
		['android.nfc.tech.NdefFormatable'],
		['android.nfc.tech.MifareClassi'],
		['android.nfc.tech.MifareUltralight']
	];

2.读入数据

代码如下(示例):

const reads = () => {
		// 开启nfc监听
		listenNFCStatus();
		//读取nfc里面的方法
		readData();
	}
const listenNFCStatus = () => {
		console.log("---------监听NFC状态--------------")
		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: '请在系统设置中先启用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];

			plus.globalEvent.addEventListener('newintent', function() {
				console.log('newintent running');
				// 轮询调用 NFC
				// setTimeout(that.nfcRuning(), 1000);
				nfcRuning()
			});
			nfcAdapter.enableForegroundDispatch(main, pendingIntent, intentFiltersArray, techListsArray);
		} catch (e) {
			console.error(e);
		}
	}

	const 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 action = intent.getAction();
		console.log("action type:" + intent.getAction());
		read(intent)
	}

	function read(intent) { // 读代码
		// NFC id
		let bytesId = intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
		console.log("bytesId:", bytesId)
		let nfc_id = byteArrayToHexString(bytesId);
		console.log("bytesId:", nfc_id)
		dataList.nfc = nfc_id
		// 卡内信息
		// let rawmsgs = intent.getParcelableArrayExtra(NfcAdapter.EXTRA_NDEF_MESSAGES);
		// let tag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
		// let isoDep = plus.android.importClass('android.nfc.tech.IsoDep')
		// let fa = isoDep.get(tag)
		// let SELECT = [0x63,0x64,0x63,0x00,0x00,0x00,0x00,0x32,0x32,0x31];
		// isoDep.connect()
		// // 发送APDU指令
		// let result = isoDep.transceive(SELECT)
		// console.log(result,"=======")
		// let fa = get(tag)
		// fa.connect()
		// let miclass = MifareClassic.get(tag)
		// console.log('实例获取miclass---',rawmsgs)
		// miclass.connect()
		// let count = miclass.getSectorCount()
		// console.log('====:',count,this.HexStringToByteArray('FFFFFFFFFFFF'))
		if (nfc_id != null || nfc_id != "") {
			plus.globalEvent.removeEventListener('newintent');
			searchElderIsBind()
			// payRequest()
			dataList.promptMessage = "支付中..."
			return;
		}
		return nfc_id;

	}

	function 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;
	}

	function readData() { // 更改读状态
		if (true) {
			uni.showToast('请检查设备是否支持并开启 NFC 功能!');
			return;
		}
		// 轮询条件
		readyRead = true;
		uni.showToast('请将NFC标签靠近!');
	}

总结

提示:这里对文章进行总结:
例如:以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值