1234567891011121314151617181920212223242526272829303132333435363738394041424344 |
- /**
- * 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("android.intent.ACTION_DECODE_DATA"); // 换你的广播动作 (设置-扫描设置-输出方式-输出方式改为Intent输出)
- receiver = plus.android.implements('io.dcloud.android.content.BroadcastReceiver', {
- onReceive: function(context, intent) {
- plus.android.importClass(intent);
- let code = intent.getStringExtra("barcode_string"); // 换你的广播标签
- 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)
- }
|