<template>
	<view class="content">
		<image class="logo" src="/static/logo.png"></image>
		<view class="text-area">
			<text class="title">{{title}}</text>
		</view>
		<view>收到广播:{{receviceMessage}}</view>
		<input v-model="sendMessage" />

		<button @click="sendBroadcastUHF">发送uhf广播</button>
	</view>
</template>
/*https://www.cnblogs.com/Mr-East/p/13410568.html

**/
<script>
	export default {
		data() {
			return {
				title: 'UniApp Broadcast Demo for UHF',
				receviceMessage: '',
				sendMessage: '',
				intentAction: 'com.rscja.scanner.action.scanner.RFID',
			}
		},
		onLoad() {
			this.initBroadcast()
		},
		methods: {
			initBroadcast() {
				let _this = this;
				var main = plus.android.runtimeMainActivity();
				var receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
					onReceive: function(context, intent) {
						plus.android.importClass(intent);
						let code = intent.getStringExtra("data"); // 换你的广播标签 
						_this.receviceMessage = code
					}
				});
				var IntentFilter = plus.android.importClass('android.content.IntentFilter');
				var filter = new IntentFilter();
				filter.addAction(_this.intentAction); //自定义广播ACTION  
				filter.addAction("com.rscja.scanner.action.scanner.RFID");
				main.registerReceiver(receiver, filter);
			},

			sendBroadcastUHF() {
				var Intent = plus.android.importClass("android.content.Intent");

				//7.1以前用android.intent.action.START_BARCODE_RFID
				var intent = new Intent("com.rscja.scanner.action.START_BARCODE_RFID");
				var keyValue = this.sendMessage;
				var main = plus.android.runtimeMainActivity();
				intent.setPackage("com.rscja.scanner");
				intent.putExtra("function", 11);
				//intent.setFlags(0x08800000);

				console.log(intent, keyValue, main)
				main.sendBroadcast(intent);
			},
		}
	}
</script>

<style>
	.content {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
	}

	.logo {
		height: 200rpx;
		width: 200rpx;
		margin-top: 200rpx;
		margin-left: auto;
		margin-right: auto;
		margin-bottom: 50rpx;
	}

	.text-area {
		display: flex;
		justify-content: center;
	}

	.title {
		font-size: 36rpx;
		color: #8f8f94;
	}
</style>
