uniapp中NFC功能的实现

uniapp 中实现NF功能

一、前期准备

  • 1、在manifest.json文件中开发NCF权限

  • 2、需要注意这里更改了manifest.json中的内容需要整包更新客户端包才能生效,如果是服务器热更新下载的包不生效
    20201023144229642

二、实现示例

<template>
	<!-- 标签绑定-明细 -->
	<view id="page">
			<view class="list-item flex align-items justify-between">
				<view class="title flex align-items"><text class="gray">标签编码</text></view>
				<text class="gray">{
    
    {
    
     formData.bind_code }}</text>
			</view>
	</view>
</template>

<script>

var NfcAdapter;
export default {
    
    
	data() {
    
    
		return {
    
    
			
		};
	},
	onLoad(options) {
    
    
		
	},
	onShow() {
    
    
		this.NFCInit();
	},
	onHide() {
    
    
		this.NFCReadUID();
	},
	methods: {
    
    
       // nfc入口
		NFCInit() {
    
    
			try {
    
    
				var main = plus.android.runtimeMainActivity();
				//console.log(main);
				var Intent = plus.android.importClass('android.content.Intent');
				// console.log(Intent);
				var Activity = plus.android.importClass('android.app.Activity');
				//console.log(Activity);
				var PendingIntent = plus.android.importClass('android.app.PendingIntent');
				// console.log(PendingIntent);
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				// console.log(IntentFilter);
				// var Uri = plus.android.importClass('android.net.Uri');
				// var Bundle = plus.android.importClass('android.os.Bundle');
				// var Handler = plus.android.importClass('android.os.Handler');
				//console.log(Handler);
				NfcAdapter = plus.android.importClass('android.nfc.NfcAdapter');
				//console.log(NfcAdapter);
				var _nfcAdapter = NfcAdapter.getDefaultAdapter(main);
				// console.log(_nfcAdapter);

				var ndef = new IntentFilter('android.nfc.action.NDEF_DISCOVERED'); //NfcAdapter.ACTION_NDEF_DISCOVERED
				// console.log(ndef);
				var tag = new IntentFilter('android.nfc.action.TAG_DISCOVERED'); //NfcAdapter.ACTION_TECH_DISCOVERED
				// console.log(tag);
				var tech = new IntentFilter('android.nfc.action.TECH_DISCOVERED');
				// console.log(tech);
				var intentFiltersArray = [ndef, tag, tech];

				var 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']
				];

				var _intent = new Intent(main, main.getClass());
				// console.log(_intent);
				_intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);

				var pendingIntent = PendingIntent.getActivity(main, 0, _intent, 0);
				// console.log(pendingIntent);

				if (_nfcAdapter == null) {
    
    
				} else if (_nfcAdapter.isEnabled() == false) {
    
    
				} else {
    
    
					_nfcAdapter.enableForegroundDispatch(main, pendingIntent, IntentFilter, techListsArray);
				}
			} catch (e) {
    
    
				//TODO handle the exception
			}
		},
		NFCReadUID() {
    
    
			var main = plus.android.runtimeMainActivity();
			var _intent = main.getIntent();
			var _action = _intent.getAction();
			// console.log("action type:" + _action);

			if (NfcAdapter.ACTION_NDEF_DISCOVERED == _action || NfcAdapter.ACTION_TAG_DISCOVERED == _action || NfcAdapter.ACTION_TECH_DISCOVERED == _action) {
    
    
				var Tag = plus.android.importClass('android.nfc.Tag');
				var tagFromIntent = _intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
				var bind_code = _intent.getByteArrayExtra(NfcAdapter.EXTRA_ID);
				
				bind_code = this.Bytes2HexString(bind_code);
				console.log("bind_code====:",bind_code)
				if (bind_code) {
    
    
					// // 标签没有被绑定进行绑定
					this.dataList = this.$store.state[this.keyCache];

					console.log('this.dataList=========', JSON.stringify(this.dataList));
					if (
						this.dataList.every(item => {
    
    
							return item.bind_code !== bind_code;
						})
					) {
    
    
						this.formData.bind_code = bind_code;
						// this.fromData.old==true;
						if (this.eventName) {
    
    
							this.$EventBus.$emit(this.eventName, JSON.stringify(this.formData));
						}

						setTimeout(() => {
    
    
							uni.navigateBack();
						}, 1500);
					} else {
    
    
						this.$util
							.confirm({
    
    
								title: '提示',
								showCancel: false,
								content: '该标签已经被绑定,请换一个!'
							})
							.then(res => {
    
    
								if (res) {
    
    
									setTimeout(() => {
    
    
										uni.navigateBack();
									}, 1000);
								}
							});
					}
				}
				//console.log(this.UID);
			}
		},
		//将byte[] 转为Hex,
		Bytes2HexString(arrBytes) {
    
    
			var str = '';
			for (var i = 0; i < arrBytes.length; i++) {
    
    
				var tmp;
				var num = arrBytes[i];
				if (num < 0) {
    
    
					//Java中数值是以补码的形式存在的,应用程序展示的十进制是补码对应真值。补码的存在主要为了简化计算机底层的运算,将减法运算直接当加法来做
					tmp = (255 + num + 1).toString(16);
				} else {
    
    
					tmp = num.toString(16);
				}
				if (tmp.length == 1) {
    
    
					tmp = '0' + tmp;
				}
				str += tmp;
			}
			return str;
		}
	}
};
</script>

} else {
				tmp = num.toString(16);
			}
			if (tmp.length == 1) {
				tmp = '0' + tmp;
			}
			str += tmp;
		}
		return str;
	}
}

};


猜你喜欢

转载自blog.csdn.net/weixin_43825389/article/details/109242390