var main = null;
var receiver = null;
//接收广播
export function recive(success, fail) {

	//在所有页面注册监听前 如果当前应用存在监听 则取消广播监听
	if (main && receiver) {
		console.log("取消监听广播");
		try {
			main.unregisterReceiver(receiver);
		} catch (e) {
			console.error("取消广播监听时出错:", e);
		}
	}

	// #ifdef APP
	main = plus.android.runtimeMainActivity(); //获取activity
	//创建自定义广播实例  
	receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
		onReceive: function(context, intent) { //实现onReceiver回调函数  
			plus.android.importClass(intent); //通过intent实例引入intent类，方便以后的‘.’操作  
			var action = intent.getAction();
			console.log('zhanglp', action);
			var data;
			switch (action) {
				case "com.android.server.scannerservice.broadcast":
					data = intent.getExtra("scannerdata");
					break
				case "com.service.scanner.data":
					data = intent.getExtra("ScanCode");
					console.log('zhanglp', data.replace(/\n/, ''))
					break

				case "android.intent.action.SCANRESULT":
					data = intent.getExtra("value");
					break

				case "android.intent.ACTION_DECODE_DATA":
					data = intent.getExtra("barcode_string");
					break

				case "com.android.provider.sdlmessage":
					data = intent.getExtra("msg");
					break
				case "com.neusoft.action.scanner.read":

					data = intent.getStringExtra("scanner_value"); //东软pda
					if (data == null) {
						data = intent.getStringExtra("Scanner_value"); //东软pda
					}
					break
				case "nlscan.action.SCANNER_RESULT":
					data = intent.getExtra("SCAN_BARCODE1");
					break
				case "com.barcode.sendBroadcast":
					data = intent.getExtra("BARCODE");
					break

				case "scan.rcv.message":
					let barocode = intent.getByteArrayExtra("barocode");
					let barocodelen = intent.getIntExtra("length", 0);
					data = new String(barocode, 0, barocodelen);
					break
				case "xlyk.wz":
					data = intent.getExtra("qrcode");
					break

				case "com.android.scanner.broadcast":
					data = intent.getExtra("scandata");
					break
				case "com.honeywell.scan.broadcast":
					data = intent.getExtra("data");
					break


			}
			if (data) {
				// console.log('zhanglp', data.replace(/\n/, ''))
				// console.log('zhanglp', data.replace(/\n/, '').replace(/\t/, ''))
				success({
					type: 'barcode',
					data: data.replace(/\n/, '').replace(/\t/, '').trim() //处理结束符和换行符为空
				})
			} else {

				fail({
					type: 'barcode',
					data: '未识别到扫码数据' //处理结束符和换行符为空
				})
			}


		}
	});
	var IntentFilter = plus.android.importClass('android.content.IntentFilter'); //网络
	var Intent = plus.android.importClass('android.content.Intent');
	var filter = new IntentFilter(); //过滤





	filter.addAction("com.android.provider.sdlMessage"); //海信
	filter.addAction("com.neusoft.action.scanner.read"); //东软
	filter.addAction("nlscan.action.SCANNER_RESULT"); //新大陆  霍尼韦尔
	filter.addAction("scan.rcv.message"); //南方鸿志
	filter.addAction("com.android.server.scannerservice.broadcast"); //得实2 东大集成  扫码工具密码888888
	filter.addAction("android.intent.action.SCANRESULT"); //得实idata
	//            filter.addAction("com.android.server.scannerservice.broadcast");//东大集成  扫码工具密码888888
	filter.addAction("android.intent.ACTION_DECODE_DATA"); //UROVO  优博讯科
	filter.addAction("com.barcode.sendBroadcast"); //西安富立叶微电子 F760
	filter.addAction("xlyk.wz"); //西安富立叶微电子 F760
	filter.addAction("com.android.scanner.broadcast");
	filter.addAction("com.android.scanner.broadcast");
	filter.addAction("com.honeywell.scan.broadcast");
	filter.addAction("com.service.scanner.data");

	main.registerReceiver(receiver, filter); //注册监听
	// #endif
}
//取消监听
export function unregisterReceiver() {
	// #ifdef APP
	console.log("取消监听广播")
	main.unregisterReceiver(receiver);
	// #endif
}