You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.4 KiB
JavaScript

4 weeks ago
/**
* pda红外线扫码
*/
var main = ''
var filter = ''
var receiver = ''
var codeQueryTag = false
let scanSuccess = function(){} // 成功后的操作函数
//开启服务监听
export function initScan(successFun) {
scanSuccess = successFun
console.log('开启了监听');
main = plus.android.runtimeMainActivity();//获取activity
var IntentFilter = plus.android.importClass('android.content.IntentFilter');
filter = new IntentFilter();
filter.addAction("com.android.server.scannerservice.broadcast"); // 换你的广播动作 (设置-扫描设置-输出方式-输出方式改为Intent输出)
receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
onReceive: function (context, intent) {
plus.android.importClass(intent);
let code = intent.getStringExtra("scannerdata");// 换你的广播标签
queryCurrenCode(code);
}
});
console.log('开启监听完成');
}
//这个得有,开启监听用
export function startScan() {
main.registerReceiver(receiver, filter);
}
//这个也得有,关闭监听用
export function stopScan() {
main.unregisterReceiver(receiver);
}
//这个是防止重复用的
async function queryCurrenCode(code) {
//防重复
if (codeQueryTag) return false;
codeQueryTag = true;
setTimeout(() => {
codeQueryTag = false;
}, 150);
console.log('调用接口啦------', code);
scanSuccess(code)
}