前言
最近搞了个设备,需求是读取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, "发送消息成功!");
}
在MainActivity
的onCreate
里有接收广播
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
返回的值了