lxp_Maxcess/scanInput.js

55 lines
1.6 KiB
JavaScript
Raw Permalink Normal View History

2025-06-03 02:55:51 +08:00
let main, receiver, filter, _codeQueryTag = false, temp = [], init = false, start = false;
export default {
initScan() {
if(init) return
let _this = this;
main = plus.android.runtimeMainActivity(); //获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
//android.intent.ACTION_DECODE_DATA
filter.addAction(uni._qs_scanlistener_action || "com.uniappPda.ACTION"); // 换你的广播动作你的pda设备里面看
receiver = plus.android.implements('io.dcloud.feature.internal.reflect.BroadcastReceiver', {
onReceive: function(context, intent) {
plus.android.importClass(intent);
let code = intent.getStringExtra(uni._qs_scanlistener_label || "com.uniapp.gatedge"); // 换你的广播标签你的pda设备里面看
_this.queryCode(code);
}
});
init = true
},
startScan() {
if(!start) {
start = true
main.registerReceiver(receiver, filter);
}
},
stopScan() {
if(start) {
start = false
main.unregisterReceiver(receiver);
}
},
install(fn) {
if(typeof fn == 'function' && !~temp.indexOf(fn)) temp.push(fn)
},
uninstall(fn) {
if(typeof fn == 'function') {
const index = temp.find(i=>i == fn)
if(~index) temp.splice(index, 1)
}
},
queryCode: function(code) {
//防重复
// if (_codeQueryTag) return false;
// _codeQueryTag = true;
// setTimeout(function() {
// _codeQueryTag = false;
// }, 150);
if(temp && temp.length) {
temp[temp.length - 1](code)
}
uni.vibrateShort()
uni.$emit("qs_scanlistener_handle", code);
}
}