当前位置: 首页 > article >正文

uniapp Native.js原生arr插件服务发送广播到uniapp页面中

前言

最近搞了个设备,需求是读取m1卡,厂家给了个安卓原生demo,接入arr插件如下,接入后发现还是少了一部分代码,设备服务调起后触发刷卡无法发送到uniapp里。
中间是一些踩坑记录,最后面是解决办法,赶时间直接拉到最后

安卓原生广播

demo中的NfcService其中有一个onDataReceived方法,最终调用了LocalBroadcastManager 发送广播

protected void onDataReceived(final ComBean ComRecData) {
	Log.d(TAG, "发送消息!");
	LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
	Intent intent = new Intent(NfcAction.ACTION_NOTIFY_CARD_NUMBER);
	intent.putExtra(NfcAction.KEY_NOTIFY_DECIMAL_CARD_NUMBER, decimal);
	intent.putExtra(NfcAction.KEY_NOTIFY_DECIMAL_REVERSE_CARD_NUMBER, decimalReverse);
	intent.putExtra(NfcAction.KEY_NOTIFY_ORIGINAL_CARD_NUMBER, originHex);
	intent.putExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_CARD_NUMBER, eightHex);
	intent.putExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_REVERSE_CARD_NUMBER, eightHexReverse);
	intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
	localBroadcastManager.sendBroadcast(intent);
	Log.d(TAG, "发送消息成功!");
}

MainActivityonCreate里有接收广播

val nfcCardReceiver = NfcCardReceiver()
val intentFilter = IntentFilter()
intentFilter.addAction(NfcAction.ACTION_NOTIFY_CARD_NUMBER)
LocalBroadcastManager.getInstance(this).registerReceiver(nfcCardReceiver, intentFilter)

NfcCardReceiver

package com.rt.nfclibdemo

import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.util.Log
import com.rt.lib_nfc.NfcAction
import org.greenrobot.eventbus.EventBus

class NfcCardReceiver: BroadcastReceiver() {
    override fun onReceive(p0: Context?, p1: Intent?) {
        if (p1 != null) {
            val card = p1.getStringExtra(NfcAction.KEY_NOTIFY_ORIGINAL_CARD_NUMBER)
            val card2 = p1.getStringExtra(NfcAction.KEY_NOTIFY_DECIMAL_CARD_NUMBER)
            val card3 = p1.getStringExtra(NfcAction.KEY_NOTIFY_DECIMAL_REVERSE_CARD_NUMBER)
            val card4 = p1.getStringExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_CARD_NUMBER)
            val card5 = p1.getStringExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_REVERSE_CARD_NUMBER)
            //跟ui绑定的东西,忽略掉
            EventBus.getDefault().post(CardBean(card!!, card2!!, card3!!, card4!!, card5!!))
            Log.e("TAG", "onReceive: " + card + "---" + card2 + "---" + card3 + "---" + card4 + "---" + card5)
        }
    }
}

根据以往的经验写出如下代码,结果是失败的

const mainActivity = plus.android.runtimeMainActivity();
const NfcAction = plus.android.importClass('com.rt.lib_nfc.NfcAction');
var nfcCardReceiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
	onReceive: function(context, intent) { //实现onReceiver回调函数
		plus.android.importClass(intent);
		console.log('context, intent ->', context, intent);
		console.log('intent.getAction() ->', intent.getAction());
	}
const IntentFilter = plus.android.importClass('android.content.IntentFilter');
const filter = new IntentFilter();
filter.addAction(NfcAction.ACTION_NOTIFY_CARD_NUMBER); //监听
let LocalBroadcastManager = plus.android.importClass('androidx.localbroadcastmanager.content.LocalBroadcastManager');
let localBroadcastManager = LocalBroadcastManager.getInstance(mainActivity);
localBroadcastManager.registerReceiver(nfcCardReceiver, filter);

最终结果是控制台无输出,
adb shell catlog抓到的日志是个null
在这里插入图片描述找了很久很久后,找到了个栗子,据说绑定不成功的原因是io.dcloud.android.content.BroadcastReceiver这个东西是抽象类,无法实现,只能用io.dcloud.feature.internal.reflect.BroadcastReceiver然而替换后还是不行,就是无法监听LocalBroadcastManager,又找了很久资料,找了无数篇垃圾文章后,找了原生安卓的很多文章后,终于找到了个办法,不用LocalBroadcastManager使用另一种方法发送,接收广播

最终方案

不使用LocalBroadcastManager发生监听广播
使用getApplicationContext().sendBroadcast(intent);发送广播
使用mainActivity.registerReceiver(nfcCardReceiver, filter);监听广播
首先将NfcService->onDataReceived改造,在原有基础上增加,更改后记得重新打成arr,然后重打自定义基座才能生效

protected void onDataReceived(final ComBean ComRecData) {
	Log.d(TAG, "发送消息!");
	LocalBroadcastManager localBroadcastManager = LocalBroadcastManager.getInstance(getApplicationContext());
	Intent intent = new Intent(NfcAction.ACTION_NOTIFY_CARD_NUMBER);
	intent.putExtra(NfcAction.KEY_NOTIFY_DECIMAL_CARD_NUMBER, decimal);
	intent.putExtra(NfcAction.KEY_NOTIFY_DECIMAL_REVERSE_CARD_NUMBER, decimalReverse);
	intent.putExtra(NfcAction.KEY_NOTIFY_ORIGINAL_CARD_NUMBER, originHex);
	intent.putExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_CARD_NUMBER, eightHex);
	intent.putExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_REVERSE_CARD_NUMBER, eightHexReverse);
	intent.addFlags(Intent.FLAG_RECEIVER_FOREGROUND);
	Log.d(TAG, "发送localBroadcastManager消息");
	//原来的发送广播语句
	//localBroadcastManager.sendBroadcast(intent);
	Log.d(TAG, "发送getApplicationContext消息");
	//新增的关键语句
	getApplicationContext().sendBroadcast(intent);
	Log.d(TAG, "发送消息成功!");
}

uniapp部分

registerReceiver(){
	const that = this;
	const Log = plus.android.importClass('android.util.Log');
	Log.d('NfcApp', '1');
	const mainActivity = plus.android.runtimeMainActivity();
	const NfcAction = plus.android.importClass('com.rt.lib_nfc.NfcAction');
	const nfcCardReceiverFunc = {  
	  onReceive: function(context, intent) { //实现onReceiver回调函数  
		  plus.android.importClass(intent);
		  console.log('context, intent ->', context, intent);
		  console.log('intent.getAction() ->', intent.getAction());
		  console.log('回调成功!');
		  that.cardData = {};
		  let card1 = intent.getStringExtra(NfcAction.KEY_NOTIFY_ORIGINAL_CARD_NUMBER);
		  let card2 = intent.getStringExtra(NfcAction.KEY_NOTIFY_DECIMAL_CARD_NUMBER);
		  let card3 = intent.getStringExtra(NfcAction.KEY_NOTIFY_DECIMAL_REVERSE_CARD_NUMBER);
		  let card4 = intent.getStringExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_CARD_NUMBER);
		  let card5 = intent.getStringExtra(NfcAction.KEY_NOTIFY_EIGHT_HEX_REVERSE_CARD_NUMBER);
		  console.log('返回数据->',card1, card2, card3, card4, card5);
		  if(card1 == 'AABB0600000001060007'){
			return;
		  }
		  //最终返回的数据
		  that.cardData = { card1, card2, card3, card4, card5 };
		}
	};
	const nfcCardReceiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', nfcCardReceiverFunc);
	const IntentFilter = plus.android.importClass('android.content.IntentFilter');
	const filter = new IntentFilter();
	Log.d('NfcApp', '2');
	filter.addAction(NfcAction.ACTION_NOTIFY_CARD_NUMBER); //监听
	Log.d('NfcApp', '3');
	let res = mainActivity.registerReceiver(nfcCardReceiver, filter);
	Log.d('NfcApp', '4');
	Log.d('NfcApp', '绑定成功???')
	//解除绑定,暂未实现
	//unregisterReceiver
},

重新打包,重新运行后即可接到this.cardData返回的值了


http://www.kler.cn/a/450609.html

相关文章:

  • 【NLP 17、NLP的基础——分词】
  • 后端-redis
  • “檢測到不安全的代理”怎麼修復?
  • 蓝牙协议——音量控制
  • mysql 查询优化之字段建立全文索引
  • DL作业11 LSTM
  • leetcode 面试经典 150 题:螺旋矩阵
  • Spring基础分析13-Spring Security框架
  • Python中zip
  • H3C MPLS跨域optionB
  • MacOS M3源代码编译Qt6.8.1
  • Linux系统文件
  • 前端Python应用指南(二)深入Flask:理解Flask的应用结构与模块化设计
  • 1688商品详情api接口开发返回值说明中skus商品规格和props商品详情
  • leetcode hot100 两两交换链表之中的节点
  • 深度学习从入门到精通——图像分割实战DeeplabV3
  • 基于Springboot的在线问卷调查系统【附源码】
  • JVM执行引擎JIT深度剖析
  • 【MySQL】InnoDB存储引擎中的索引
  • 深入理解C++23的Deducing this特性(下):高级应用与实战技巧
  • mapbox基础,加载mapbox官方地图
  • RGCL:A Review-aware Graph Contrastive Learning Framework for Recommendation
  • 自动驾驶系统研发系列—追尾风险不再隐形:解密后碰撞预警系统(RCW)的技术与应用
  • 交通控制系统中的 Prompt工程:引导LLMs实现高效交叉口管理 !
  • ensp 基于静态NAT发布公司网站服务器,
  • WebGL2示例项目常见问题解决方案