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

<script lang="ts" setup>
	import {
		onLoad,
		onShow,
		onHide,
		onUnload
	} from '@dcloudio/uni-app'
	import {
		reactive,
		defineProps
	} from "vue";
	let details = reactive({
		activity: null,
		receiver: null,
		intentFilter: null,
		isstart: false,

	})
	const props = defineProps<{ ishidestop ?: boolean }>()
	onLoad(() => {
		initScan()
		startScan();
		// console.log('扫描的--------------onLoad');
	})
	onShow(() => {
		startScan();
		// console.log('扫描的--------------onShow');
	})
	onHide(() => {
		if (props?.ishidestop) return
		stopScan();
		console.log('扫描的--------------onHide');
	})
	onUnload(() => {
		stopScan();
		uni.$off('scancodedate')
		// console.log('扫描的--------------onUnload');
	})

	function initScan() {
		// #ifdef APP
		if (plus) {
			details.activity = plus.android.runtimeMainActivity(); //获取activity
			var IntentFilter = (plus.android.importClass('android.content.IntentFilter') as any);
			details.intentFilter = new IntentFilter();
			details.intentFilter.addAction('com.android.server.scannerservice.broadcast') // 换你的广播动作
			details.receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
				onReceive: function (context, intent) {
					console.log("intent", intent)
					plus.android.importClass(intent);
					let content = intent.getStringExtra('scannerdata'); // 换你的广播标签
					uni.$emit('scancodedate', content.replace(/\n/g, ""))
				}
			});
		}
		// #endif
	}

	function startScan() {
		// #ifdef APP
		if (details.isstart) return
		details.isstart = true
		details.activity.registerReceiver(details.receiver, details.intentFilter);
		// #endif
	}
	function stopScan() {
		// #ifdef APP
		details.isstart = false
		details.activity.unregisterReceiver(details.receiver);
		// #endif
	}
</script>

<style>
</style>