<template>
	<view class="content"></view>
</template>

<script>
	var main, receiver, filter;
	var _codeQueryTag = false;
	export default {
		data() {
			return {
				scanCode: ''
			}
		},
		created(option) {
			this.initScan()
			this.startScan()
		},
		onHide() {
			this.stopScan()
		},
		destroyed() {
			this.stopScan()
		},
		methods: {
			initScan() {
				let _this = this;
				//获取Android主Activity
				main = plus.android.runtimeMainActivity();
				// //获取Android意图类
				// let Intent = plus.android.importClass('android.content.Intent');
				// //实例化意图
				// let intent = new Intent();
				// //定义意图，模拟按下L键，L键实际上是打开激光的物理键映射，由厂商提供
				// intent.setAction("com.android.action.keyevent.KEYCODE_KEYCODE_SCAN_L_DOWN");
				// //广播这个意图
				// that.main.sendBroadcast(intent);
				//获取Android意图过滤类
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				//实例化意图过滤
				filter = new IntentFilter();
				//获取扫码成功的意图广播--下面的addAction内改为自己的广播动作
				// 之前pda广播动作:com.speedata.showdecodedata
				filter.addAction("android.intent.action.BARCODEDATA");
				// android.intent.action.BARCODEDATA
				receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						//下面的getStringExtra内改为自己的广播标签
						// 之前pda广播标签:message
						let code = intent.getStringExtra("barcode_result");
						_this.queryCode(code);
					}
				});
			},
			startScan() {
				main.registerReceiver(receiver, filter);
			},
			stopScan() {
				main.unregisterReceiver(receiver);
			},
			queryCode: function(code) {
				if (_codeQueryTag) return false;
				_codeQueryTag = true;
				setTimeout(function() {
					_codeQueryTag = false;
				}, 150);
				var id = code
				console.log('id:', id)
				uni.$emit('scan', {
					code: id
				})
			},
		}
	}
</script>
